Search in sources :

Example 1 with Attribute

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

the class XMLParser method output.

/**
 *  A replacement for Node.output(), which does not recognize #text.
 *  Also, we use the empty entity, so <br /> does not turn into <br></br>.
 */
private static void output(StringBuilder buf, Node node, int indentLevel) {
    String name = node.getName();
    String value = XML.escapeXMLChars(node.getValue());
    if (name.equals(TEXT_NAME)) {
        buf.append(value);
        return;
    }
    buf.append('<').append(name);
    int nAttributes = node.getNAttributes();
    for (int n = 0; n < nAttributes; n++) {
        Attribute attr = node.getAttribute(n);
        buf.append(' ').append(attr.getName()).append("=\"").append(XML.escapeXMLChars(attr.getValue())).append('"');
    }
    // If mixed values and nodes, the values must be text nodes. See parser above.
    if (node.hasNodes()) {
        buf.append('>');
        int nChildNodes = node.getNNodes();
        for (int n = 0; n < nChildNodes; n++) {
            Node cnode = node.getNode(n);
            output(buf, cnode, indentLevel + 1);
        }
        buf.append("</").append(name).append('>');
    } else {
        if (value == null || value.length() == 0) {
            // space for <br />
            buf.append(" />");
        } else {
            buf.append('>').append(value).append("</").append(name).append('>');
        }
    }
}
Also used : Attribute(org.cybergarage.xml.Attribute) Node(org.cybergarage.xml.Node)

Example 2 with Attribute

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

the class NewsXMLParser method validate.

/**
 *  @throws I2PParserException if any node not in whitelist (depends on mode)
 *  @return true if node was removed from parent (only for REMOVE_ELEMENT mode)
 */
private boolean validate(Node node) throws I2PParserException {
    String name = node.getName();
    // _log.debug("Validating element: " + name);
    if (!xhtmlWhitelist.contains(name.toLowerCase(Locale.US))) {
        switch(_mode) {
            case ABORT:
            case SKIP_ENTRY:
                throw new I2PParserException("Invalid XHTML element \"" + name + '"');
            case REMOVE_ATTRIBUTE:
            case REMOVE_ELEMENT:
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Removing element: " + node);
                node.getParentNode().removeNode(node);
                return true;
            case ALLOW_ALL:
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Allowing non-whitelisted element by configuration: " + node);
                break;
        }
    }
    for (int i = 0; i < node.getNAttributes(); i++) {
        Attribute attr = node.getAttribute(i);
        String aname = attr.getName();
        if (attributeBlacklist.contains(aname.toLowerCase(Locale.US))) {
            switch(_mode) {
                case ABORT:
                case SKIP_ENTRY:
                    throw new I2PParserException("Invalid XHTML element \"" + name + "\" due to attribute " + aname);
                case REMOVE_ELEMENT:
                    if (_log.shouldLog(Log.WARN))
                        _log.warn("Removing element: " + node + " due to attribute " + aname);
                    node.getParentNode().removeNode(node);
                    return true;
                case REMOVE_ATTRIBUTE:
                    if (_log.shouldLog(Log.WARN))
                        _log.warn("Removing attribute: " + aname + " from " + node);
                    // sadly, no removeAttribute(int)
                    if (node.removeAttribute(attr))
                        i--;
                    break;
                case ALLOW_ALL:
                    if (_log.shouldLog(Log.WARN))
                        _log.warn("Allowing blacklisted attribute by configuration: " + node);
                    break;
            }
        }
    }
    int count = node.getNNodes();
    for (int i = 0; i < node.getNNodes(); i++) {
        boolean removed = validate(node.getNode(i));
        if (removed)
            i--;
    }
    return false;
}
Also used : Attribute(org.cybergarage.xml.Attribute)

Aggregations

Attribute (org.cybergarage.xml.Attribute)2 Node (org.cybergarage.xml.Node)1