use of org.cybergarage.xml.Node in project i2p.i2p by i2p.
the class PersistNews method load.
/**
* This does not check for any missing values.
* Any fields in any NewsEntry may be null.
* Content is not sanitized by NewsXMLParser here, do that before storing.
*
* @return non-null, sorted by updated date, newest first
*/
public static List<NewsEntry> load(I2PAppContext ctx) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new File(ctx.getConfigDir(), DIR);
List<NewsEntry> rv = new ArrayList<NewsEntry>();
File[] files = dir.listFiles(new FileSuffixFilter(PFX, SFX));
if (files == null)
return rv;
for (File file : files) {
String name = file.getName();
XMLParser parser = new XMLParser(ctx);
InputStream in = null;
Node node;
boolean error = false;
try {
in = new GZIPInputStream(new FileInputStream(file));
node = parser.parse(in);
NewsEntry entry = extract(node);
if (entry != null) {
rv.add(entry);
} else {
if (log.shouldWarn())
log.warn("load error from " + file);
error = true;
}
} catch (ParserException pe) {
if (log.shouldWarn())
log.warn("load error from " + file, pe);
error = true;
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("load error from " + file, ioe);
error = true;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
}
if (error)
file.delete();
}
Collections.sort(rv);
return rv;
}
use of org.cybergarage.xml.Node in project i2p.i2p by i2p.
the class PersistNews method store.
/**
* Store each entry.
* Old entries are always overwritten, as they may change even without the updated date changing.
*
* @param entries each one should be "entry" at the root
* @return success
*/
public static boolean store(I2PAppContext ctx, List<Node> entries) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new SecureDirectory(ctx.getConfigDir(), DIR);
if (!dir.exists())
dir.mkdirs();
StringBuilder buf = new StringBuilder();
boolean rv = true;
for (Node entry : entries) {
Node nid = entry.getNode("id");
if (nid == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String id = nid.getValue();
if (id == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String name = idToName(ctx, id);
File file = new File(dir, name);
Writer out = null;
try {
out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file)));
out.write(XML_START);
XMLParser.toString(buf, entry);
out.write(buf.toString());
buf.setLength(0);
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("failed store to " + file, ioe);
rv = false;
} finally {
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
return rv;
}
use of org.cybergarage.xml.Node in project i2p.i2p by i2p.
the class Device method getDeviceList.
// //////////////////////////////////////////////
// deviceList
// //////////////////////////////////////////////
public DeviceList getDeviceList() {
DeviceList devList = new DeviceList();
Node devListNode = getDeviceNode().getNode(DeviceList.ELEM_NAME);
if (devListNode == null)
return devList;
int nNode = devListNode.getNNodes();
for (int n = 0; n < nNode; n++) {
Node node = devListNode.getNode(n);
if (Device.isDeviceNode(node) == false)
continue;
Device dev = new Device(node);
devList.add(dev);
}
return devList;
}
use of org.cybergarage.xml.Node in project i2p.i2p by i2p.
the class Service method getServiceData.
// //////////////////////////////////////////////
// UserData
// //////////////////////////////////////////////
private ServiceData getServiceData() {
Node node = getServiceNode();
ServiceData userData = (ServiceData) node.getUserData();
if (userData == null) {
userData = new ServiceData();
node.setUserData(userData);
userData.setNode(node);
}
return userData;
}
use of org.cybergarage.xml.Node in project i2p.i2p by i2p.
the class Service method getServiceStateTable.
// //////////////////////////////////////////////
// serviceStateTable
// //////////////////////////////////////////////
public ServiceStateTable getServiceStateTable() {
ServiceStateTable stateTable = new ServiceStateTable();
Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME);
if (stateTableNode == null)
return stateTable;
Node serviceNode = getServiceNode();
int nNode = stateTableNode.getNNodes();
for (int n = 0; n < nNode; n++) {
Node node = stateTableNode.getNode(n);
if (StateVariable.isStateVariableNode(node) == false)
continue;
StateVariable serviceVar = new StateVariable(serviceNode, node);
stateTable.add(serviceVar);
}
return stateTable;
}
Aggregations