Search in sources :

Example 31 with Node

use of org.cybergarage.xml.Node in project felix by apache.

the class BuildDevice method buildRootDeviceNode.

private static Device buildRootDeviceNode(Node root, ServiceReference sr) {
    Node dev = new Node(Device.ELEM_NAME);
    root.addNode(dev);
    DeviceData dd = new DeviceData();
    dd.setDescriptionURI("/gen-desc.xml");
    dev.setUserData(dd);
    Device devUPnP = new Device(root, dev);
    devUPnP.setDeviceType(extractDeviceType(sr));
    devUPnP.setFriendlyName((String) sr.getProperty(UPnPDevice.FRIENDLY_NAME));
    devUPnP.setManufacture((String) sr.getProperty(UPnPDevice.MANUFACTURER));
    devUPnP.setManufactureURL((String) sr.getProperty(UPnPDevice.MANUFACTURER_URL));
    devUPnP.setModelDescription((String) sr.getProperty(UPnPDevice.MODEL_DESCRIPTION));
    devUPnP.setModelName((String) sr.getProperty(UPnPDevice.MODEL_NAME));
    devUPnP.setModelNumber((String) sr.getProperty(UPnPDevice.MODEL_NUMBER));
    devUPnP.setModelURL((String) sr.getProperty(UPnPDevice.MODEL_URL));
    devUPnP.setSerialNumber((String) sr.getProperty(UPnPDevice.SERIAL_NUMBER));
    devUPnP.setUDN((String) sr.getProperty(UPnPDevice.UDN));
    devUPnP.setUPC((String) sr.getProperty(UPnPDevice.UPC));
    devUPnP.setLocation("/gen-desc.xml");
    addServices("", devUPnP, sr);
    addDevices("", devUPnP, sr);
    devUPnP.setPresentationURL((String) sr.getProperty(UPnPDevice.PRESENTATION_URL));
    return devUPnP;
}
Also used : UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) Node(org.cybergarage.xml.Node) DeviceData(org.cybergarage.upnp.xml.DeviceData)

Example 32 with Node

use of org.cybergarage.xml.Node in project i2p.i2p by i2p.

the class NewsFetcher method processSU3.

/**
 *  Process the fetched su3 news file _tempFile.
 *  Handles 3 types of contained files: xml.gz (preferred), xml, and html (old format fake xml)
 *
 *  @return the temp file contining the HTML-format news.xml
 *  @since 0.9.17
 */
private File processSU3() throws IOException {
    SU3File su3 = new SU3File(_context, _tempFile);
    // real xml, maybe gz, maybe not
    File to1 = new File(_context.getTempDir(), "tmp-" + _context.random().nextInt() + ".xml");
    // real xml
    File to2 = new File(_context.getTempDir(), "tmp2-" + _context.random().nextInt() + ".xml");
    try {
        su3.verifyAndMigrate(to1);
        int type = su3.getFileType();
        if (su3.getContentType() != SU3File.CONTENT_NEWS)
            throw new IOException("bad content type: " + su3.getContentType());
        if (type == SU3File.TYPE_HTML)
            return to1;
        if (type != SU3File.TYPE_XML && type != SU3File.TYPE_XML_GZ)
            throw new IOException("bad file type: " + type);
        File xml;
        if (type == SU3File.TYPE_XML_GZ) {
            gunzip(to1, to2);
            xml = to2;
            to1.delete();
        } else {
            xml = to1;
        }
        NewsXMLParser parser = new NewsXMLParser(_context);
        Node root = parser.parse(xml);
        xml.delete();
        NewsMetadata data = parser.getMetadata();
        List<NewsEntry> entries = parser.getEntries();
        // add entries to the news manager
        ClientAppManager cmgr = _context.clientAppManager();
        if (cmgr != null) {
            NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME);
            if (nmgr != null) {
                nmgr.addEntries(entries);
                List<Node> nodes = NewsXMLParser.getNodes(root, "entry");
                nmgr.storeEntries(nodes);
            }
        }
        // Persist any new CRL entries
        List<CRLEntry> crlEntries = parser.getCRLEntries();
        if (crlEntries != null)
            persistCRLEntries(crlEntries);
        else
            _log.info("No CRL entries found in news feed");
        // Block any new blocklist entries
        BlocklistEntries ble = parser.getBlocklistEntries();
        if (ble != null && ble.isVerified())
            processBlocklistEntries(ble);
        else
            _log.info("No blocklist entries found in news feed");
        // store entries and metadata in old news.xml format
        String sudVersion = su3.getVersionString();
        String signingKeyName = su3.getSignerString();
        File to3 = new File(_context.getTempDir(), "tmp3-" + _context.random().nextInt() + ".xml");
        outputOldNewsXML(data, entries, sudVersion, signingKeyName, to3);
        return to3;
    } finally {
        to2.delete();
    }
}
Also used : NewsMetadata(net.i2p.router.news.NewsMetadata) Node(org.cybergarage.xml.Node) IOException(java.io.IOException) CRLEntry(net.i2p.router.news.CRLEntry) SU3File(net.i2p.crypto.SU3File) NewsEntry(net.i2p.router.news.NewsEntry) ClientAppManager(net.i2p.app.ClientAppManager) NewsXMLParser(net.i2p.router.news.NewsXMLParser) NewsManager(net.i2p.router.news.NewsManager) BlocklistEntries(net.i2p.router.news.BlocklistEntries) SU3File(net.i2p.crypto.SU3File) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Example 33 with Node

use of org.cybergarage.xml.Node in project i2p.i2p by i2p.

the class NewsXMLParser method parse.

/**
 *  Process the XML input stream.
 *
 *  @param in XML content only. Any su3 or gunzip handling must have
 *            already happened.
 *  @return the root node
 *  @throws IOException on any parse error
 */
public Node parse(InputStream in) throws IOException {
    _entries = null;
    _metadata = null;
    XMLParser parser = new XMLParser(_context);
    try {
        Node root = parser.parse(in);
        extract(root);
        return root;
    } catch (ParserException pe) {
        throw new I2PParserException(pe);
    }
}
Also used : ParserException(org.cybergarage.xml.ParserException) Node(org.cybergarage.xml.Node)

Example 34 with Node

use of org.cybergarage.xml.Node in project i2p.i2p by i2p.

the class NewsXMLParser method getNodes.

/**
 *  Helper to get all Nodes matching the name
 *
 *  @return non-null
 */
public static List<Node> getNodes(Node node, String name) {
    List<Node> rv = new ArrayList<Node>();
    int count = node.getNNodes();
    for (int i = 0; i < count; i++) {
        Node n = node.getNode(i);
        if (n.getName().equals(name))
            rv.add(n);
    }
    return rv;
}
Also used : Node(org.cybergarage.xml.Node) ArrayList(java.util.ArrayList)

Example 35 with Node

use of org.cybergarage.xml.Node in project i2p.i2p by i2p.

the class NewsXMLParser method extractNewsEntries.

/**
 *  This does not check for any missing values.
 *  Any field in any NewsEntry may be null.
 */
private List<NewsEntry> extractNewsEntries(Node feed) throws I2PParserException {
    List<NewsEntry> rv = new ArrayList<NewsEntry>();
    List<Node> entries = getNodes(feed, "entry");
    for (Node entry : entries) {
        NewsEntry e = new NewsEntry();
        Node n = entry.getNode("title");
        if (n != null) {
            e.title = n.getValue();
            if (e.title != null)
                e.title = e.title.trim();
        }
        n = entry.getNode("link");
        if (n != null) {
            String a = n.getAttributeValue("href");
            if (a.length() > 0)
                e.link = a.trim();
        }
        n = entry.getNode("id");
        if (n != null) {
            e.id = n.getValue();
            if (e.id != null)
                e.id = e.id.trim();
        }
        n = entry.getNode("updated");
        if (n != null) {
            String v = n.getValue();
            if (v != null) {
                long time = RFC3339Date.parse3339Date(v.trim());
                if (time > 0)
                    e.updated = time;
            }
        }
        n = entry.getNode("summary");
        if (n != null) {
            e.summary = n.getValue();
            if (e.summary != null)
                e.summary = e.summary.trim();
        }
        n = entry.getNode("author");
        if (n != null) {
            n = n.getNode("name");
            if (n != null) {
                e.authorName = n.getValue();
                if (e.authorName != null)
                    e.authorName = e.authorName.trim();
            }
        }
        n = entry.getNode("content");
        if (n != null) {
            String a = n.getAttributeValue("type");
            if (a.length() > 0)
                e.contentType = a;
            // now recursively sanitize
            // and convert everything in the content to string
            StringBuilder buf = new StringBuilder(256);
            for (int i = 0; i < n.getNNodes(); i++) {
                Node sn = n.getNode(i);
                try {
                    boolean removed = validate(sn);
                    if (removed) {
                        i--;
                        continue;
                    }
                } catch (I2PParserException ipe) {
                    switch(_mode) {
                        case ABORT:
                            throw ipe;
                        case SKIP_ENTRY:
                            if (_log.shouldLog(Log.WARN))
                                _log.warn("Skipping entry", ipe);
                            e = null;
                            break;
                        case REMOVE_ATTRIBUTE:
                        case REMOVE_ELEMENT:
                            if (_log.shouldLog(Log.WARN))
                                _log.warn("Removing element", ipe);
                            continue;
                        case ALLOW_ALL:
                        default:
                            break;
                    }
                }
                if (e == null)
                    break;
                XMLParser.toString(buf, sn);
            }
            if (e == null)
                continue;
            e.content = buf.toString();
        }
        rv.add(e);
    }
    Collections.sort(rv);
    return rv;
}
Also used : Node(org.cybergarage.xml.Node) ArrayList(java.util.ArrayList)

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