Example usage for org.w3c.dom NamedNodeMap getNamedItem

List of usage examples for org.w3c.dom NamedNodeMap getNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getNamedItem.

Prototype

public Node getNamedItem(String name);

Source Link

Document

Retrieves a node specified by name.

Usage

From source file:org.apache.poi.poifs.crypt.EncryptionVerifier.java

public EncryptionVerifier(String descriptor) {
    NamedNodeMap keyData = null;
    try {//from ww  w. j a v a  2  s . c  om
        ByteArrayInputStream is;
        is = new ByteArrayInputStream(descriptor.getBytes());
        NodeList keyEncryptor = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is)
                .getElementsByTagName("keyEncryptor").item(0).getChildNodes();
        for (int i = 0; i < keyEncryptor.getLength(); i++) {
            Node node = keyEncryptor.item(i);
            if (node.getNodeName().equals("p:encryptedKey")) {
                keyData = node.getAttributes();
                break;
            }
        }
        if (keyData == null)
            throw new EncryptedDocumentException("");
    } catch (Exception e) {
        throw new EncryptedDocumentException("Unable to parse keyEncryptor");
    }

    spinCount = Integer.parseInt(keyData.getNamedItem("spinCount").getNodeValue());
    verifier = Base64
            .decodeBase64(keyData.getNamedItem("encryptedVerifierHashInput").getNodeValue().getBytes());
    salt = Base64.decodeBase64(keyData.getNamedItem("saltValue").getNodeValue().getBytes());

    encryptedKey = Base64.decodeBase64(keyData.getNamedItem("encryptedKeyValue").getNodeValue().getBytes());

    int saltSize = Integer.parseInt(keyData.getNamedItem("saltSize").getNodeValue());
    if (saltSize != salt.length)
        throw new EncryptedDocumentException("Invalid salt size");

    verifierHash = Base64
            .decodeBase64(keyData.getNamedItem("encryptedVerifierHashValue").getNodeValue().getBytes());

    int blockSize = Integer.parseInt(keyData.getNamedItem("blockSize").getNodeValue());

    String alg = keyData.getNamedItem("cipherAlgorithm").getNodeValue();

    if ("AES".equals(alg)) {
        if (blockSize == 16)
            algorithm = EncryptionHeader.ALGORITHM_AES_128;
        else if (blockSize == 24)
            algorithm = EncryptionHeader.ALGORITHM_AES_192;
        else if (blockSize == 32)
            algorithm = EncryptionHeader.ALGORITHM_AES_256;
        else
            throw new EncryptedDocumentException("Unsupported block size");
    } else {
        throw new EncryptedDocumentException("Unsupported cipher");
    }

    String chain = keyData.getNamedItem("cipherChaining").getNodeValue();
    if ("ChainingModeCBC".equals(chain))
        cipherMode = EncryptionHeader.MODE_CBC;
    else if ("ChainingModeCFB".equals(chain))
        cipherMode = EncryptionHeader.MODE_CFB;
    else
        throw new EncryptedDocumentException("Unsupported chaining mode");

    verifierHashSize = Integer.parseInt(keyData.getNamedItem("hashSize").getNodeValue());
}

From source file:org.apache.shindig.gadgets.rewrite.AbsolutePathReferenceVisitor.java

/**
 * Returns href value of the base tag.// ww w  . j av a2s  .  c  om
 * @param doc The document to process.
 * @return Value of href attribute of the base tag.
 */
@VisibleForTesting
String getBaseHref(Document doc) {
    NodeList list = doc.getElementsByTagName("base");
    if (list.getLength() == 0) {
        return null;
    }

    NamedNodeMap nodeMap = list.item(0).getAttributes();
    if (nodeMap == null) {
        return null;
    }
    Attr attr = (Attr) nodeMap.getNamedItem("href");
    return attr != null ? attr.getValue() : null;
}

From source file:org.apache.shindig.social.opensocial.util.ApiValidator.java

/**
 * Get an ordered list of javascript resources from a feature sets.
 *
 * @param spec//from  w  w  w.j av  a2  s  .  co  m
 *                The spec location
 * @return An ordered list of javascript resources, these are relative to
 *         specification file.
 * @throws IOException
 *                 If any of the resources can't be loaded.
 * @throws SAXException
 *                 Where the feature.xml file is not parsable
 * @throws ParserConfigurationException
 *                 where the parser can't be constructed.
 * @return An ordered list of script that need to be loaded and executed to
 *         make the feature available in the context.
 */
private List<String> getScripts(String spec) throws SAXException, IOException, ParserConfigurationException {
    String features = spec + "/feature.xml";
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(features);
    if (in == null) {
        in = this.getClass().getClassLoader().getResourceAsStream("features/" + features);
        if (in == null) {
            throw new IOException("Cant find " + features + " or features/" + features + " in classpath ");
        }
    }
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    Document doc = documentBuilder.parse(in);
    NodeList nl = doc.getElementsByTagName("script");
    List<String> scripts = Lists.newArrayList();
    for (int i = 0; i < nl.getLength(); i++) {
        Node scriptNode = nl.item(i);
        NamedNodeMap attributes = scriptNode.getAttributes();
        Node scriptAttr = attributes.getNamedItem("src");
        String script = scriptAttr.getNodeValue();
        scripts.add(script);
    }
    return scripts;
}

From source file:org.apache.sling.its.servlets.ItsImportServlet.java

/**
 * Get the attributes and call output to set the attributes into
 * jackrabbit./*from   w w w  .jav a2s.  c  om*/
 *
 * @param element
 *           an Element from the Document object.
 * @param path
 *           jackrabbit path to store the attributes
 */
private void setAttributes(final Element element, final String path) {
    if (element.hasAttributes()) {
        final NamedNodeMap map = element.getAttributes();

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < map.getLength(); i++) {
            list.add(((Attr) map.item(i)).getNodeName());
        }
        Collections.sort(list);

        for (final String attrName : list) {
            final Attr attr = (Attr) map.getNamedItem(attrName);
            output(path, attr, null);
        }
    }
}

From source file:org.apache.tika.batch.builders.CommandLineParserBuilder.java

private boolean getBoolean(NamedNodeMap map, String opt, boolean defaultValue) {
    Node n = map.getNamedItem(opt);
    if (n == null) {
        return defaultValue;
    }// w  w  w.  j  a  v  a 2s.c o m

    if (n.getNodeValue() == null) {
        return defaultValue;
    }

    if (n.getNodeValue().toLowerCase(Locale.ROOT).equals("true")) {
        return true;
    } else if (n.getNodeValue().toLowerCase(Locale.ROOT).equals("false")) {
        return false;
    }
    return defaultValue;
}

From source file:org.apache.tika.batch.builders.CommandLineParserBuilder.java

private String getString(NamedNodeMap map, String opt, String defaultVal) {
    Node n = map.getNamedItem(opt);
    if (n == null) {
        return defaultVal;
    }//from ww w. j ava  2s . c o  m
    String value = n.getNodeValue();

    if (value == null) {
        return defaultVal;
    }
    return value;
}

From source file:org.apache.tika.eval.reports.ResultsReporter.java

private static Report buildReport(Node n) {
    NodeList children = n.getChildNodes();
    Report r = new Report();
    NamedNodeMap attrs = n.getAttributes();

    r.includeSql = Boolean.parseBoolean(attrs.getNamedItem("includeSql").getNodeValue());
    r.reportFilename = attrs.getNamedItem("reportFilename").getNodeValue();
    r.reportName = attrs.getNamedItem("reportName").getNodeValue();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != 1) {
            continue;
        }//from   w ww. j a va 2s . c  o m
        if ("sql".equals(child.getNodeName())) {
            if (r.sql != null) {
                throw new IllegalArgumentException("Can only have one sql statement per report");
            }
            r.sql = child.getTextContent();
        } else if ("colformats".equals(child.getNodeName())) {
            r.cellFormatters = getCellFormatters(child);
        } else {
            throw new IllegalArgumentException("Not expecting to see:" + child.getNodeName());
        }
    }
    return r;
}

From source file:org.apache.tika.eval.reports.ResultsReporter.java

private static Map<String, XSLXCellFormatter> getCellFormatters(Node n) {
    NodeList children = n.getChildNodes();
    Map<String, XSLXCellFormatter> ret = new HashMap<>();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != 1) {
            continue;
        }//from   w  ww. j ava  2  s .c o m
        NamedNodeMap attrs = child.getAttributes();
        String columnName = attrs.getNamedItem("name").getNodeValue();
        assert (!ret.containsKey(columnName));
        String type = attrs.getNamedItem("type").getNodeValue();
        if ("numberFormatter".equals(type)) {
            String format = attrs.getNamedItem("format").getNodeValue();
            XSLXCellFormatter f = new XLSXNumFormatter(format);
            ret.put(columnName, f);
        } else if ("urlLink".equals(type)) {
            String base = "";
            Node baseNode = attrs.getNamedItem("base");
            if (baseNode != null) {
                base = baseNode.getNodeValue();
            }
            XLSXHREFFormatter f = new XLSXHREFFormatter(base, Hyperlink.LINK_URL);
            ret.put(columnName, f);
        } else if ("fileLink".equals(type)) {
            String base = "";
            Node baseNode = attrs.getNamedItem("base");
            if (baseNode != null) {
                base = baseNode.getNodeValue();
            }
            XLSXHREFFormatter f = new XLSXHREFFormatter(base, Hyperlink.LINK_FILE);
            ret.put(columnName, f);
        }
    }
    return ret;
}

From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractRunMojo.java

/**
 * Determine whether the passed context.xml file declares the context as reloadable or not.
 *
 * @return false by default, true if  reloadable="true" in context.xml.
 */// ww  w  .  jav  a  2  s.c o m
protected boolean isContextReloadable() throws MojoExecutionException {
    if (contextReloadable || backgroundProcessorDelay > 0) {
        return true;
    }
    // determine whether to use a reloadable Loader or not (default is false).
    boolean reloadable = false;
    try {
        if (contextFile != null && contextFile.exists()) {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document contextDoc = builder.parse(contextFile);
            contextDoc.getDocumentElement().normalize();

            NamedNodeMap nodeMap = contextDoc.getDocumentElement().getAttributes();
            Node reloadableAttribute = nodeMap.getNamedItem("reloadable");

            reloadable = (reloadableAttribute != null) ? Boolean.valueOf(reloadableAttribute.getNodeValue())
                    : false;
        }
        getLog().debug("context reloadable: " + reloadable);
    } catch (IOException ioe) {
        getLog().error("Could not parse file: [" + contextFile.getAbsolutePath() + "]", ioe);
    } catch (ParserConfigurationException pce) {
        getLog().error("Could not configure XML parser", pce);
    } catch (SAXException se) {
        getLog().error("Could not parse file: [" + contextFile.getAbsolutePath() + "]", se);
    }

    return reloadable;
}

From source file:org.apereo.portal.io.xml.PortalDataKey.java

public PortalDataKey(Node rootElement) {
    if (rootElement.getNodeType() == Node.DOCUMENT_NODE) {
        rootElement = ((Document) rootElement).getDocumentElement();
    }//from   w  ww .j  av  a  2 s . co  m

    final String nodeName = rootElement.getNodeName();
    final String namespaceURI = rootElement.getNamespaceURI();

    if (namespaceURI != null) {
        this.name = new QName(namespaceURI, nodeName);
    } else {
        this.name = new QName(nodeName);
    }

    final NamedNodeMap attributes = rootElement.getAttributes();
    if (attributes != null) {
        final Node scriptAttr = attributes.getNamedItem(SCRIPT_ATTRIBUTE_NAME.getLocalPart());
        if (scriptAttr != null) {
            this.script = scriptAttr.getTextContent();
        } else {
            this.script = null;
        }

        final Node versionAttr = attributes.getNamedItem(VERSION_ATTRIBUTE_NAME.getLocalPart());
        if (versionAttr != null) {
            this.version = versionAttr.getTextContent();
        } else {
            this.version = null;
        }
    } else {
        this.script = null;
        this.version = null;
    }
}