Search in sources :

Example 36 with Node

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;
}
Also used : ParserException(org.cybergarage.xml.ParserException) Log(net.i2p.util.Log) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.cybergarage.xml.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileSuffixFilter(net.i2p.util.FileSuffixFilter) File(java.io.File)

Example 37 with Node

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;
}
Also used : Log(net.i2p.util.Log) SecureDirectory(net.i2p.util.SecureDirectory) GZIPOutputStream(java.util.zip.GZIPOutputStream) Node(org.cybergarage.xml.Node) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 38 with Node

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;
}
Also used : Node(org.cybergarage.xml.Node)

Example 39 with Node

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;
}
Also used : Node(org.cybergarage.xml.Node) ServiceData(org.cybergarage.upnp.xml.ServiceData)

Example 40 with Node

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;
}
Also used : Node(org.cybergarage.xml.Node)

Aggregations

Node (org.cybergarage.xml.Node)58 ParserException (org.cybergarage.xml.ParserException)7 ArrayList (java.util.ArrayList)5 ServiceData (org.cybergarage.upnp.xml.ServiceData)5 Parser (org.cybergarage.xml.Parser)5 IOException (java.io.IOException)4 File (java.io.File)3 Device (org.cybergarage.upnp.Device)3 DeviceData (org.cybergarage.upnp.xml.DeviceData)3 UPnPDevice (org.osgi.service.upnp.UPnPDevice)3 URL (java.net.URL)2 Log (net.i2p.util.Log)2 InvalidDescriptionException (org.cybergarage.upnp.device.InvalidDescriptionException)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1