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:Main.java

private static String getTextContent(Element element) {
    if (element == null) {
        return null;
    }/* ww  w  .  j  a v a  2  s  .co m*/
    Node textNode = element.getFirstChild();
    if (textNode == null) {
        return "";
    }
    if (textNode.getNodeType() != Node.TEXT_NODE) {
        throw new RuntimeException("Element " + element.getTagName() + "does not have text content");
    }
    return textNode.getNodeValue();
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
* Find persistence units./*from  ww w .j av a 2  s . com*/
* 
* @param url
*            the url
* @param defaultTransactionType
*            the default transaction type
* @return the list
* @throws Exception
*             the exception
*/
public static List<PersistenceMetadata> findPersistenceUnits(URL url,
        PersistenceUnitTransactionType defaultTransactionType) throws Exception {

    Document doc = getDocument(url);
    Element top = doc.getDocumentElement();
    NodeList children = top.getChildNodes();
    ArrayList<PersistenceMetadata> units = new ArrayList<PersistenceMetadata>();

    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) children.item(i);
            String tag = element.getTagName();
            // look for "persistence-unit" element
            if (tag.equals("persistence-unit")) {
                PersistenceMetadata metadata = parsePersistenceUnit(element);
                units.add(metadata);
            }
        }
    }
    return units;
}

From source file:Main.java

public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) {
    NodeList nl = e.getChildNodes();
    Element result = null;
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);/* ww w .  j  av  a 2  s . co  m*/
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (result != null) // more than one Element children 
                throw exc;
            result = (Element) n;
        }
    }

    if (result == null // zero Element children
            || !result.getTagName().equals(tag)) // wrong tag
        throw exc;
    return result;
}

From source file:com.ibm.soatf.tool.ValidateTransferedValues.java

public static boolean validateElementValuesFromFile(File srcMessageFile, File destMessageFile,
        Map<String, String> mappings) throws FileNotFoundException, ParseException, java.text.ParseException {
    if (srcMessageFile == null || destMessageFile == null || !srcMessageFile.exists()
            || !destMessageFile.exists()) {
        throw new FileNotFoundException();
    }/*from w  ww . ja  v  a 2s. c o  m*/

    boolean valid = true;

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(srcMessageFile);

        DocumentTraversal traversal = (DocumentTraversal) doc;

        String srcElementName;
        String destElementName;
        String srcValue;
        String destValue;

        NodeIterator iterator = traversal.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT,
                null, true);
        for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
            Element e = (Element) n;
            if (isLeafElement(e)) {
                srcElementName = e.getTagName();
                if (srcElementName.indexOf(":") >= 0) {
                    srcElementName = srcElementName.substring(srcElementName.indexOf(":") + 1);
                }
                if (mappings != null && mappings.containsKey(srcElementName)) {
                    destElementName = mappings.get(srcElementName);
                    if (destElementName == null || "".equals(destElementName)) {
                        //null v toColumnName - neporovnavam
                        logger.debug("Skipping column " + srcElementName);
                        continue;
                    }
                } else {
                    destElementName = srcElementName;
                }
                logger.debug("Comparing values for element " + srcElementName + " and coresponding element "
                        + destElementName);
                srcValue = getElementFromFile(srcElementName, srcMessageFile, false);
                destValue = getElementFromFile(destElementName, destMessageFile, true);
                boolean differ = ((srcValue == null && destValue != null) || !srcValue.equals(destValue));
                if (differ) {
                    logger.debug("values are different: " + srcValue + " <> " + destValue);
                    valid = false;
                } else {
                    logger.debug("values are equal");
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        valid = false;
    }

    return valid;
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
 * Parses the persistence unit./*from   w w  w  .  j a  v a 2  s .co  m*/
 * 
 * @param top
 *            the top
 * @return the persistence metadata
 * @throws Exception
 *             the exception
 */
private static PersistenceMetadata parsePersistenceUnit(Element top) throws Exception {
    PersistenceMetadata metadata = new PersistenceMetadata();

    String puName = top.getAttribute("name");
    if (!isEmpty(puName)) {
        log.trace("Persistent Unit name from persistence.xml: " + puName);
        metadata.setName(puName);
    }

    NodeList children = top.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) children.item(i);
            String tag = element.getTagName();

            if (tag.equals("provider")) {
                metadata.setProvider(getElementContent(element));
            } else if (tag.equals("properties")) {
                NodeList props = element.getChildNodes();
                for (int j = 0; j < props.getLength(); j++) {
                    if (props.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        Element propElement = (Element) props.item(j);
                        // if element is not "property" then skip
                        if (!"property".equals(propElement.getTagName())) {
                            continue;
                        }

                        String propName = propElement.getAttribute("name").trim();
                        String propValue = propElement.getAttribute("value").trim();
                        if (isEmpty(propValue)) {
                            propValue = getElementContent(propElement, "");
                        }
                        metadata.getProps().put(propName, propValue);
                    }
                }
            }
            // Kundera doesn't support "class", "jar-file" and "excluded-unlisted-classes" for now.. but will someday.
            // let's parse it for now.
            else if (tag.equals("class")) {
                metadata.getClasses().add(getElementContent(element));
            } else if (tag.equals("jar-file")) {
                metadata.getJarFiles().add(getElementContent(element));
            } else if (tag.equals("exclude-unlisted-classes")) {
                metadata.setExcludeUnlistedClasses(true);
            }
        }
    }
    PersistenceUnitTransactionType transactionType = getTransactionType(top.getAttribute("transaction-type"));
    if (transactionType != null) {
        metadata.setTransactionType(transactionType);
    }

    return metadata;
}

From source file:Main.java

private static int getLocationInParent(Element expectedChild, String[] identifyingAttributes,
        String[] identifyingValues) {
    Element parent = (Element) expectedChild.getParentNode();
    List<Element> allSiblingsWithMyName = getDirectChildren(parent, expectedChild.getTagName());
    int location = -1;
    for (Element sibling : allSiblingsWithMyName) {
        if (hasTheseAttributeValues(sibling, identifyingAttributes, identifyingValues)) {
            location++;/*  w  ww.j  a v  a2s. c o m*/
        }
        if (sibling == expectedChild) {
            return location;
        }
    }
    return location;
}

From source file:org.zaizi.oauth2utils.OAuthUtils.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;//from w w w .j a  va  2s  .  c o m
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            org.w3c.dom.Element childElement = (org.w3c.dom.Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:com.git.original.common.config.XMLFileConfigDocument.java

/**
 * XML??/*  w ww  .  j  a  v a 2s  .  c o  m*/
 * 
 * @param elem
 *            XML
 * @return ?
 */
static ConfigNode convertElement(Element elem) {
    if (elem == null) {
        return null;
    }

    ConfigNode cn = new ConfigNode(elem.getTagName(), null);

    NamedNodeMap attrNodeMap = elem.getAttributes();
    if (attrNodeMap != null) {
        for (int i = 0; i < attrNodeMap.getLength(); i++) {
            Node node = attrNodeMap.item(i);
            cn.addAttribute(node.getNodeName(), node.getNodeValue());
        }
    }

    NodeList nodeList = elem.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            switch (node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                cn.addAttribute(node.getNodeName(), node.getNodeValue());
                break;
            case Node.ELEMENT_NODE:
                ConfigNode child = convertElement((Element) node);
                cn.addChild(child);
                break;
            case Node.TEXT_NODE:
                cn.value = node.getNodeValue();
                break;
            default:
                continue;
            }
        }
    }

    return cn;
}

From source file:com.iflytek.spider.protocol.httpclient.Http.java

/**
 * Reads authentication configuration file (defined as 'http.auth.file' in
 * Nutch configuration file) and sets the credentials for the configured
 * authentication scopes in the HTTP client object.
 * /*w  w  w.j ava  2 s. c om*/
 * @throws ParserConfigurationException
 *             If a document builder can not be created.
 * @throws SAXException
 *             If any parsing error occurs.
 * @throws IOException
 *             If any I/O error occurs.
 */
private static synchronized void setCredentials()
        throws ParserConfigurationException, SAXException, IOException {
    if (authFile == null || authFile.equals(""))
        authRulesRead = true;
    if (authRulesRead)
        return;

    authRulesRead = true; // Avoid re-attempting to read

    InputStream is = conf.getConfResourceAsInputStream(authFile);
    if (is != null) {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

        Element rootElement = doc.getDocumentElement();
        if (!"auth-configuration".equals(rootElement.getTagName())) {
            if (LOG.isWarnEnabled())
                LOG.warn("Bad auth conf file: root element <" + rootElement.getTagName() + "> found in "
                        + authFile + " - must be <auth-configuration>");
        }

        // For each set of credentials
        NodeList credList = rootElement.getChildNodes();
        for (int i = 0; i < credList.getLength(); i++) {
            Node credNode = credList.item(i);
            if (!(credNode instanceof Element))
                continue;

            Element credElement = (Element) credNode;
            if (!"credentials".equals(credElement.getTagName())) {
                if (LOG.isWarnEnabled())
                    LOG.warn("Bad auth conf file: Element <" + credElement.getTagName() + "> not recognized in "
                            + authFile + " - expected <credentials>");
                continue;
            }

            String username = credElement.getAttribute("username");
            String password = credElement.getAttribute("password");

            // For each authentication scope
            NodeList scopeList = credElement.getChildNodes();
            for (int j = 0; j < scopeList.getLength(); j++) {
                Node scopeNode = scopeList.item(j);
                if (!(scopeNode instanceof Element))
                    continue;

                Element scopeElement = (Element) scopeNode;

                if ("default".equals(scopeElement.getTagName())) {

                    // Determine realm and scheme, if any
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set default credentials
                    defaultUsername = username;
                    defaultPassword = password;
                    defaultRealm = realm;
                    defaultScheme = scheme;

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set as default" + " for realm: "
                                + realm + "; scheme: " + scheme);
                    }

                } else if ("authscope".equals(scopeElement.getTagName())) {

                    // Determine authentication scope details
                    String host = scopeElement.getAttribute("host");
                    int port = -1; // For setting port to AuthScope.ANY_PORT
                    try {
                        port = Integer.parseInt(scopeElement.getAttribute("port"));
                    } catch (Exception ex) {
                        // do nothing, port is already set to any port
                    }
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set credentials for the determined scope
                    AuthScope authScope = getAuthScope(host, port, realm, scheme);
                    NTCredentials credentials = new NTCredentials(username, password, agentHost, realm);

                    client.getState().setCredentials(authScope, credentials);

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set for AuthScope - " + "host: "
                                + host + "; port: " + port + "; realm: " + realm + "; scheme: " + scheme);
                    }

                } else {
                    if (LOG.isWarnEnabled())
                        LOG.warn("Bad auth conf file: Element <" + scopeElement.getTagName()
                                + "> not recognized in " + authFile + " - expected <authscope>");
                }
            }
            is.close();
        }
    }
}

From source file:com.bluexml.side.form.utils.DOMUtil.java

/**
 * Gets the first element that matches the tag name.
 * //from   ww w  .  j a  v  a 2s.c om
 * @param elements
 *            the elements
 * @param tagName
 *            the tag name
 * 
 * @return the unique children by tag name
 * @since 1.0.1
 */
public static Element getOneElementByTagName(List<Element> elements, String tagName) {
    for (Element element : elements) {
        if (StringUtils.equalsIgnoreCase(element.getTagName(), tagName)) {
            return element;
        }
    }
    return null;
}