📄 storage.java
字号:
/*
* Copyright 2006 Marcel Schoffelmeer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.s10r.manager.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import com.s10r.io.EncryptedInputStream;
import com.s10r.io.EncryptedOutputStream;
import com.s10r.util.DateUtil;
import com.s10r.util.S10RException;
/**
* @author schoffem
*/
public class Storage
{
public static final String DESCRIPTION_TAG = "description";
public static final String ID_TAG = "id";
public static final String PASSWORD_TAG = "password";
public static final String NAME_TAG = "name";
public static final String USAGECNT_TAG = "usagecnt";
public static final String CREATED_TAG = "created";
public static final String LASTUSED_TAG = "lastused";
public static final String LASTUPDATED_TAG = "lastupdated";
public static final String LASTUPDATEDONSITE_TAG = "lastupdatedonsite";
public static final String URL_TAG = "url";
public static final String LABEL_TAG = "label";
public static ItemContainer load(File filePath,
String password)
{
try
{
return readItems(new EncryptedInputStream(new FileInputStream(
filePath), password));
}
catch (IOException e)
{
throw new S10RException(e);
}
}
private static ItemContainer readItems(InputStream is)
{
SAXBuilder builder = new SAXBuilder();
try
{
ItemContainer entries = new ItemContainer();
Document doc = builder.build(is);
Element root = doc.getRootElement();
Element entriesElem = null;
// Upgrade old style files with entries as root element
if (root.getName() == "entries")
{
System.out.println("Storage.readEntries: Upgrading..");
entriesElem = root;
}
else
{
entriesElem = root.getChild("entries");
}
// read the labels first so we can hook them up with the entries
// if necessary
Element labelsElem = root.getChild("labels");
readLabels(entries, labelsElem);
readEntries(entries, entriesElem);
return entries;
}
catch (JDOMException e)
{
throw new S10RException(e);
}
catch (IOException e)
{
throw new S10RException(e);
}
}
private static void readLabels(ItemContainer entries,
Element labelsElem)
{
if (labelsElem != null)
{
List<Element> labelElems = labelsElem
.getChildren("label");
System.out.println("loaded: " + labelElems.size() + " labels");
for (Element labelElem : labelElems)
{
ItemLabel entry = loadLabel(labelElem);
entries.add(entry);
}
}
}
private static void readEntries(ItemContainer entries,
Element entriesElem)
{
List<Element> taskElems = entriesElem
.getChildren("entry");
System.out.println("loaded: " + taskElems.size() + " tasks");
for (Element taskElem : taskElems)
{
PasswordEntry entry = loadEntry(taskElem, entries);
entries.add(entry);
}
}
private static ItemLabel loadLabel(Element labelElem)
{
String name = labelElem.getChildText(NAME_TAG);
ItemLabel label = new ItemLabel(name);
if (labelElem.getChildText(CREATED_TAG) != null)
{
label.setCreated(DateUtil
.parse(labelElem.getChildText(CREATED_TAG)));
}
if (labelElem.getChildText(LASTUPDATED_TAG) != null)
{
label.setLastUpdated(DateUtil.parse(labelElem
.getChildText(LASTUPDATED_TAG)));
}
label.setDescription(labelElem.getChildText(DESCRIPTION_TAG));
return label;
}
private static PasswordEntry loadEntry(Element taskElem, ItemContainer entries)
{
String name = taskElem.getChildText(NAME_TAG);
String password = taskElem.getChildText(PASSWORD_TAG);
PasswordEntry entry = new PasswordEntry(name, password);
if (taskElem.getChildText(USAGECNT_TAG) != null)
{
entry.setUsageCnt(Integer.parseInt(taskElem
.getChildText(USAGECNT_TAG)));
}
if (taskElem.getChildText(CREATED_TAG) != null)
{
entry
.setCreated(DateUtil.parse(taskElem
.getChildText(CREATED_TAG)));
}
if (taskElem.getChildText(LASTUSED_TAG) != null)
{
entry.setLastUsed(DateUtil.parse(taskElem
.getChildText(LASTUSED_TAG)));
}
if (taskElem.getChildText(LASTUPDATED_TAG) != null)
{
entry.setLastUpdated(DateUtil.parse(taskElem
.getChildText(LASTUPDATED_TAG)));
}
if (taskElem.getChildText(LASTUPDATEDONSITE_TAG) != null)
{
entry.setLastUpdatedOnSite(DateUtil.parse(taskElem
.getChildText(LASTUPDATEDONSITE_TAG)));
}
if (taskElem.getChildText(URL_TAG) != null)
{
entry.setUrl(taskElem.getChildText(URL_TAG));
}
entry.setId(taskElem.getChildText(ID_TAG));
entry.setDescription(taskElem.getChildText(DESCRIPTION_TAG));
List<Element> labelElems = taskElem.getChildren(LABEL_TAG);
for (Object labelElement: labelElems)
{
Element labelElem = (Element)labelElement;
String labelName = labelElem.getText();
ItemLabel label = entries.getLabel(labelName);
if (label == null)
{
System.err.println("loadEnty: item(" + taskElem.getName() + ") " +
"refers to non-existing label " + labelName + ". Label " +
"reference ignored.");
}
else
{
entry.addLabel(label);
}
}
return entry;
}
public static void save(ItemContainer entries, String filePath,
String password)
{
try
{
writeEntries(entries, new EncryptedOutputStream(
new FileOutputStream(filePath), password));
System.out.println("Storage.save: entries saved to: " + filePath);
}
catch (FileNotFoundException e)
{
throw new S10RException(e);
}
}
public static void writeEntries(ItemContainer entries,
OutputStream target)
{
XMLStorageVisitor v = new XMLStorageVisitor();
for (ManagerItem item : entries.getItems())
{
item.accept(v);
}
Document doc = v.getDocument();
Format format = Format.getPrettyFormat();
XMLOutputter out = new XMLOutputter();
out.setFormat(format);
try
{
Writer writer = new OutputStreamWriter(target);
// out.output(doc, System.out);
out.output(doc, writer);
writer.close();
}
catch (IOException e)
{
// todo: create error dialog
// todo: create general exception error dialog that allows user to
// view
// stack trace.
throw new RuntimeException(e);
}
}
public static void importEntries(String filePath)
{
try
{
readItems(new FileInputStream(filePath));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public static void exportEntries(ItemContainer entries, File destination)
{
try
{
writeEntries(entries, new FileOutputStream(destination));
}
catch (FileNotFoundException e)
{
throw new S10RException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -