Example usage for org.w3c.dom Element getTagName

List of usage examples for org.w3c.dom Element getTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getTagName.

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:org.apache.cocoon.forms.util.DomHelper.java

public static int getAttributeAsInteger(Element element, String attributeName) throws Exception {
    String attrValue = getAttribute(element, attributeName);
    try {/*from   w ww  .  j  a  v  a  2  s.c  om*/
        return Integer.parseInt(attrValue);
    } catch (NumberFormatException e) {
        throw new Exception(
                "Cannot parse the value \"" + attrValue + "\" as an integer in the attribute \"" + attributeName
                        + "\" on the element \"" + element.getTagName() + "\" at " + getLocation(element));
    }
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

public static int getAttributeAsInteger(Element element, String attributeName, int defaultValue)
        throws Exception {
    String attrValue = element.getAttribute(attributeName);
    if (attrValue.length() == 0) {
        return defaultValue;
    } else {//from  w ww  .  j  a v  a 2  s.  com
        try {
            return Integer.parseInt(attrValue);
        } catch (NumberFormatException e) {
            throw new Exception("Cannot parse the value \"" + attrValue + "\" as an integer in the attribute \""
                    + attributeName + "\" on the element \"" + element.getTagName() + "\" at "
                    + getLocation(element));
        }
    }
}

From source file:org.apache.ctakes.jdl.data.xml.DomUtilTest.java

@Test
public void strToDocument() {
    Document document = DomUtil.strToDocument("<root />");
    Element element = document.getDocumentElement();
    assertThat(element.getTagName(), is("root"));
}

From source file:org.apache.ctakes.jdl.data.xml.DomUtilTest.java

@Theory
public void srcToDocument(String xml) {
    xml = FileUtil.getFile(xml).toString();
    Document document = DomUtil.srcToDocument(xml);
    Element element = document.getDocumentElement();
    assertThat(element.getTagName(), is(Resources.ROOT_LOAD));
}

From source file:org.apache.hadoop.conf.Configuration.java

private void loadResource(Properties properties, Object name, boolean quiet) {
    try {//from  w w  w  . j ava  2s .  c  o  m
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        //ignore all comments inside the xml file
        docBuilderFactory.setIgnoringComments(true);

        //allow includes in the xml file
        docBuilderFactory.setNamespaceAware(true);
        try {
            docBuilderFactory.setXIncludeAware(true);
        } catch (UnsupportedOperationException e) {
            LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e);
        }
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = null;
        Element root = null;

        if (name instanceof URL) { // an URL resource
            URL url = (URL) name;
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof String) { // a CLASSPATH resource
            URL url = getResource((String) name);
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof Path) { // a file resource
            // Can't use FileSystem API or we get an infinite loop
            // since FileSystem uses Configuration API.  Use java.io.File instead.
            File file = new File(((Path) name).toUri().getPath()).getAbsoluteFile();
            if (file.exists()) {
                if (!quiet) {
                    LOG.info("parsing " + file);
                }
                InputStream in = new BufferedInputStream(new FileInputStream(file));
                try {
                    doc = builder.parse(in);
                } finally {
                    in.close();
                }
            }
        } else if (name instanceof InputStream) {
            try {
                doc = builder.parse((InputStream) name);
            } finally {
                ((InputStream) name).close();
            }
        } else if (name instanceof Element) {
            root = (Element) name;
        }

        if (doc == null && root == null) {
            if (quiet)
                return;
            throw new RuntimeException(name + " not found");
        }

        if (root == null) {
            root = doc.getDocumentElement();
        }
        if (!"configuration".equals(root.getTagName()))
            LOG.fatal("bad conf file: top-level element not <configuration>");
        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); i++) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element))
                continue;
            Element prop = (Element) propNode;
            if ("configuration".equals(prop.getTagName())) {
                loadResource(properties, prop, quiet);
                continue;
            }
            if (!"property".equals(prop.getTagName()))
                LOG.warn("bad conf file: element not <property>");
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            boolean finalParameter = false;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element))
                    continue;
                Element field = (Element) fieldNode;
                if ("name".equals(field.getTagName()) && field.hasChildNodes())
                    attr = ((Text) field.getFirstChild()).getData().trim();
                if ("value".equals(field.getTagName()) && field.hasChildNodes())
                    value = ((Text) field.getFirstChild()).getData();
                if ("final".equals(field.getTagName()) && field.hasChildNodes())
                    finalParameter = "true".equals(((Text) field.getFirstChild()).getData());
            }

            // Ignore this parameter if it has already been marked as 'final'
            if (attr != null) {
                if (value != null) {
                    if (!finalParameters.contains(attr)) {
                        properties.setProperty(attr, value);
                        if (storeResource) {
                            updatingResource.put(attr, name.toString());
                        }
                    } else if (!value.equals(properties.getProperty(attr))) {
                        LOG.warn(name + ":a attempt to override final parameter: " + attr + ";  Ignoring.");
                    }
                }
                if (finalParameter) {
                    finalParameters.add(attr);
                }
            }
        }

    } catch (IOException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (DOMException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (SAXException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.corona.ConfigManager.java

/**
 * Check if the element name matches the tagName provided
 * @param element the xml element//  w  ww.j ava2s. c  om
 * @param tagName the name to check against
 * @return true if the name of the element matches tagName, false otherwise
 */
private static boolean matched(Element element, String tagName) {
    return tagName.equals(element.getTagName());
}

From source file:org.apache.hadoop.hbase.mapred.IndexConfiguration.java

public void addFromXML(String content) {
    try {/*from  w  w  w  .ja  v  a2 s.  com*/
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document doc = builder.parse(new ByteArrayInputStream(content.getBytes()));

        Element root = doc.getDocumentElement();
        if (!"configuration".equals(root.getTagName())) {
            LOG.fatal("bad conf file: top-level element not <configuration>");
        }

        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); i++) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element)) {
                continue;
            }

            Element prop = (Element) propNode;
            if ("property".equals(prop.getTagName())) {
                propertyFromXML(prop, null);
            } else if ("column".equals(prop.getTagName())) {
                columnConfFromXML(prop);
            } else {
                LOG.warn("bad conf content: element neither <property> nor <column>");
            }
        }
    } catch (Exception e) {
        LOG.fatal("error parsing conf content: " + e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.hbase.mapred.IndexConfiguration.java

private void propertyFromXML(Element prop, Properties properties) {
    NodeList fields = prop.getChildNodes();
    String attr = null;/*from  ww w.j a  va  2 s  . co  m*/
    String value = null;

    for (int j = 0; j < fields.getLength(); j++) {
        Node fieldNode = fields.item(j);
        if (!(fieldNode instanceof Element)) {
            continue;
        }

        Element field = (Element) fieldNode;
        if ("name".equals(field.getTagName())) {
            attr = ((Text) field.getFirstChild()).getData();
        }
        if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
            value = ((Text) field.getFirstChild()).getData();
        }
    }

    if (attr != null && value != null) {
        if (properties == null) {
            set(attr, value);
        } else {
            properties.setProperty(attr, value);
        }
    }
}

From source file:org.apache.hadoop.hbase.mapred.IndexConfiguration.java

private void columnConfFromXML(Element column) {
    ColumnConf columnConf = new ColumnConf();
    NodeList props = column.getChildNodes();
    for (int i = 0; i < props.getLength(); i++) {
        Node propNode = props.item(i);
        if (!(propNode instanceof Element)) {
            continue;
        }//from   w  w w.ja  v a  2 s.  c  om

        Element prop = (Element) propNode;
        if ("property".equals(prop.getTagName())) {
            propertyFromXML(prop, columnConf);
        } else {
            LOG.warn("bad conf content: element not <property>");
        }
    }

    if (columnConf.getProperty(HBASE_COLUMN_NAME) != null) {
        columnMap.put(columnConf.getProperty(HBASE_COLUMN_NAME), columnConf);
    } else {
        LOG.warn("bad column conf: name not specified");
    }
}

From source file:org.apache.hadoop.hdfs.server.hightidenode.ConfigManager.java

/**
 * Updates the in-memory data structures from the config file. This file is
 * expected to be in the following whitespace-separated format:
 * Blank lines and lines starting with # are ignored.
 *  /* ww w  . j ava2s  .  c  o  m*/
 * @throws IOException if the config file cannot be read.
 * @throws HighTideConfigurationException if configuration entries are invalid.
 * @throws ClassNotFoundException if user-defined policy classes cannot be loaded
 * @throws ParserConfigurationException if XML parser is misconfigured.
 * @throws SAXException if config file is malformed.
 * @returns A new set of policy categories.
 */
void reloadConfigs() throws IOException, ParserConfigurationException, SAXException, ClassNotFoundException,
        HighTideConfigurationException {

    if (configFileName == null) {
        return;
    }

    File file = new File(configFileName);
    if (!file.exists()) {
        throw new HighTideConfigurationException("Configuration file " + configFileName + " does not exist.");
    }

    // Read and parse the configuration file.
    // allow include files in configuration file
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringComments(true);
    docBuilderFactory.setNamespaceAware(true);
    try {
        docBuilderFactory.setXIncludeAware(true);
    } catch (UnsupportedOperationException e) {
        LOG.error("Failed to set setXIncludeAware(true) for raid parser " + docBuilderFactory + ":" + e, e);
    }
    LOG.error("Reloading config file " + file);

    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document doc = builder.parse(file);
    Element root = doc.getDocumentElement();
    if (!"configuration".equalsIgnoreCase(root.getTagName()))
        throw new HighTideConfigurationException(
                "Bad configuration file: " + "top-level element not <configuration>");
    NodeList elements = root.getChildNodes();

    Set<PolicyInfo> existingPolicies = new HashSet<PolicyInfo>();

    // loop through all the configured source paths.
    for (int i = 0; i < elements.getLength(); i++) {
        Node node = elements.item(i);
        if (!(node instanceof Element)) {
            continue;
        }
        Element element = (Element) node;
        String elementTagName = element.getTagName();
        String policyName = null;
        if ("srcPath".equalsIgnoreCase(elementTagName)) {
            String srcPathPrefix = element.getAttribute("name");

            if (srcPathPrefix == null || srcPathPrefix.length() == 0) {
                throw new HighTideConfigurationException(
                        "Bad configuration file: " + "srcPath node does not have a path.");
            }
            PolicyInfo policyInfo = new PolicyInfo(srcPathPrefix, conf);
            policyName = srcPathPrefix;
            Properties policyProperties;

            // loop through all elements of this policy
            NodeList policies = element.getChildNodes();
            for (int j = 0; j < policies.getLength(); j++) {
                Node node1 = policies.item(j);
                if (!(node1 instanceof Element)) {
                    continue;
                }
                Element policy = (Element) node1;
                if ((!"property".equalsIgnoreCase(policy.getTagName()))
                        && (!"destPath".equalsIgnoreCase(policy.getTagName()))) {
                    throw new HighTideConfigurationException(
                            "Bad configuration file: " + "Expecting <property> or <destPath> for srcPath "
                                    + srcPathPrefix + " but found " + policy.getTagName());
                }

                // parse the <destPath> items
                if ("destPath".equalsIgnoreCase(policy.getTagName())) {
                    String destPath = policy.getAttribute("name");
                    if (destPath == null) {
                        throw new HighTideConfigurationException("Bad configuration file: "
                                + "<destPath> tag should have an attribute named 'name'.");
                    }
                    NodeList properties = policy.getChildNodes();
                    Properties destProperties = new Properties();
                    for (int k = 0; k < properties.getLength(); k++) {
                        Node node2 = properties.item(k);
                        if (!(node2 instanceof Element)) {
                            continue;
                        }
                        Element property = (Element) node2;
                        String propertyName = property.getTagName();
                        if (!("property".equalsIgnoreCase(propertyName))) {
                            throw new HighTideConfigurationException(
                                    "Bad configuration file: " + "<destPath> can have only <property> children."
                                            + " but found " + propertyName);
                        }
                        NodeList nl = property.getChildNodes();
                        String pname = null, pvalue = null;
                        for (int l = 0; l < nl.getLength(); l++) {
                            Node node3 = nl.item(l);
                            if (!(node3 instanceof Element)) {
                                continue;
                            }
                            Element item = (Element) node3;
                            String itemName = item.getTagName();
                            if ("name".equalsIgnoreCase(itemName)) {
                                pname = ((Text) item.getFirstChild()).getData().trim();
                            } else if ("value".equalsIgnoreCase(itemName)) {
                                pvalue = ((Text) item.getFirstChild()).getData().trim();
                            }
                        }
                        if (pname == null || pvalue == null) {
                            throw new HighTideConfigurationException("Bad configuration file: "
                                    + "All property for destPath " + destPath + "  must have name and value ");
                        }
                        LOG.info(policyName + "." + pname + " = " + pvalue);
                        destProperties.setProperty(pname, pvalue);
                    }
                    policyInfo.addDestPath(destPath, destProperties);

                } else if ("property".equalsIgnoreCase(policy.getTagName())) {
                    Element property = (Element) node1;
                    NodeList nl = property.getChildNodes();
                    String pname = null, pvalue = null;
                    for (int l = 0; l < nl.getLength(); l++) {
                        Node node3 = nl.item(l);
                        if (!(node3 instanceof Element)) {
                            continue;
                        }
                        Element item = (Element) node3;
                        String itemName = item.getTagName();
                        if ("name".equalsIgnoreCase(itemName)) {
                            pname = ((Text) item.getFirstChild()).getData().trim();
                        } else if ("value".equalsIgnoreCase(itemName)) {
                            pvalue = ((Text) item.getFirstChild()).getData().trim();
                        }
                    }
                    if (pname == null || pvalue == null) {
                        throw new HighTideConfigurationException("Bad configuration file: "
                                + "All property for srcPath " + srcPathPrefix + " must have name and value ");
                    }
                    LOG.info(policyName + "." + pname + " = " + pvalue);
                    policyInfo.setProperty(pname, pvalue);
                }
            }
            existingPolicies.add(policyInfo);
        } else {
            throw new HighTideConfigurationException("Bad configuration file: "
                    + "The top level item must be srcPath but found " + elementTagName);
        }
    }
    validateAllPolicies(existingPolicies);
    setAllPolicies(existingPolicies);
    return;
}