Java Code Examples for org.w3c.dom.Node#getNodeValue()

The following examples show how to use org.w3c.dom.Node#getNodeValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GIFMetadata.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
protected static boolean getBooleanAttribute(Node node, String name,
                                             boolean defaultValue,
                                             boolean required)
  throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    String value = attr.getNodeValue();
    // Allow lower case booleans for backward compatibility, #5082756
    if (value.equals("TRUE") || value.equals("true")) {
        return true;
    } else if (value.equals("FALSE") || value.equals("false")) {
        return false;
    } else {
        fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!");
        return false;
    }
}
 
Example 2
Source File: LegacyJMSConfiguration.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the Queue Configuration node as a QueueConfiguration object
 *
 * @param node
 * @throws Exception
 */
public void parseQueueConfiguration(final Node node) throws Exception {
   Element e = (Element) node;
   NamedNodeMap atts = node.getAttributes();
   String queueName = atts.getNamedItem(NAME_ATTR).getNodeValue();
   String selectorString = null;
   boolean durable = XMLConfigurationUtil.getBoolean(e, "durable", DEFAULT_QUEUE_DURABILITY);
   NodeList children = node.getChildNodes();
   for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);

      if (QUEUE_SELECTOR_NODE_NAME.equals(child.getNodeName())) {
         Node selectorNode = child;
         Node attNode = selectorNode.getAttributes().getNamedItem("string");
         selectorString = attNode.getNodeValue();
      }
   }

   configuration.addAddressConfiguration(new CoreAddressConfiguration()
                                            .setName(queueName)
                                            .addRoutingType(RoutingType.ANYCAST)
                                            .addQueueConfiguration(new QueueConfiguration(queueName)
                                                                      .setFilterString(selectorString)
                                                                      .setDurable(durable)
                                                                      .setRoutingType(RoutingType.ANYCAST)));
}
 
Example 3
Source File: XmlUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Searches the given list of nodes for a tag with the given name and
 * returns the first match as a string.
 *
 * @param tagName the tag name.
 * @param nodes the nodes to search.
 * @return the first matching node as a string.
 */
public String getNodeValue(final String tagName, final NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        final Node node = nodes.item(i);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            final NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                final Node data = childNodes.item(j);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }
            }
        }
    }

    return null;
}
 
Example 4
Source File: GIFMetadata.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
protected static int getEnumeratedAttribute(Node node,
                                            String name,
                                            String[] legalNames,
                                            int defaultValue,
                                            boolean required)
  throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    String value = attr.getNodeValue();
    for (int i = 0; i < legalNames.length; i++) {
        if(value.equals(legalNames[i])) {
            return i;
        }
    }

    fatal(node, "Illegal value for attribute " + name + "!");
    return -1;
}
 
Example 5
Source File: GIFMetadata.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected static String getAttribute(Node node, String name,
                                     String defaultValue, boolean required)
  throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    return attr.getNodeValue();
}
 
Example 6
Source File: ConfigFile.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
protected String getNodeValue(Node _node) {
    if (_node == null) {
        return null;
    }
    Node _firstChild = _node.getFirstChild();
    if (_firstChild == null) {
        return null;
    }
    String _text = _firstChild.getNodeValue();
    if (_text != null) {
        _text = _text.trim();
    }
    return _text;
}
 
Example 7
Source File: PNGMetadata.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String getAttribute(Node node, String name,
                            String defaultValue, boolean required)
    throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    return attr.getNodeValue();
}
 
Example 8
Source File: ContentComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean sameNode(Node n1, Node n2) {
    //check node name
    if (!n1.getNodeName().equals(n2.getNodeName())) {
        System.err.println("================================================");
        System.err.println("Expected node: " + n1.getNodeName() + ", got: " + n2.getNodeName());
        System.err.println("================================================");
        return false;
    }
    //check node value
    if (!((n1.getNodeValue() != null)
            ? n1.getNodeValue().equals(n2.getNodeValue())
            : (n2.getNodeValue() == null))) {
        System.err.println("================================================");
        System.err.println("Expected node value: " + n1.getNodeValue() + ", got: " + n2.getNodeValue());
        System.err.println("================================================");
        return false;
    }
    //check node attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if ((nnm1 == null && nnm2 != null) || (nnm1 != null && nnm2 == null)) {
        return false;
    }
    if (nnm1 == null && nnm2 == null) {
        return true;
    }
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node x = nnm1.item(i);
        Node y = nnm2.item(i);
        if (!(x.getNodeName().equals(y.getNodeName()) && x.getNodeValue().equals(y.getNodeValue()))) {
            //nodes are not equals - print some info
            System.err.println("================================================");
            System.err.println("Expected attribute: " + x.getNodeName() + "=\'" + x.getNodeValue() + "\'," + " got: " + y.getNodeName() + "=\'" + y.getNodeValue() + "\'");
            System.err.println("================================================");
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: PNGMetadata.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private String getAttribute(Node node, String name,
                            String defaultValue, boolean required)
    throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    return attr.getNodeValue();
}
 
Example 10
Source File: PNGMetadata.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private String getStringAttribute(Node node, String name,
                                  String defaultValue, boolean required)
    throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    return attr.getNodeValue();
}
 
Example 11
Source File: GIFMetadata.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected static String getStringAttribute(Node node, String name,
                                           String defaultValue,
                                           boolean required,
                                           String[] range)
  throws IIOInvalidTreeException {
    Node attr = node.getAttributes().getNamedItem(name);
    if (attr == null) {
        if (!required) {
            return defaultValue;
        } else {
            fatal(node, "Required attribute " + name + " not present!");
        }
    }
    String value = attr.getNodeValue();

    if (range != null) {
        if (value == null) {
            fatal(node,
                  "Null value for "+node.getNodeName()+
                  " attribute "+name+"!");
        }
        boolean validValue = false;
        int len = range.length;
        for (int i = 0; i < len; i++) {
            if (value.equals(range[i])) {
                validValue = true;
                break;
            }
        }
        if (!validValue) {
            fatal(node,
                  "Bad value for "+node.getNodeName()+
                  " attribute "+name+"!");
        }
    }

    return value;
}
 
Example 12
Source File: DOM2DTM.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Given a node handle, return its node value. This is mostly
 * as defined by the DOM, but may ignore some conveniences.
 * <p>
 *
 * @param nodeHandle The node id.
 * @return String Value of this node, or null if not
 * meaningful for this node type.
 */
public String getNodeValue(int nodeHandle)
{
  // The _type(nodeHandle) call was taking the lion's share of our
  // time, and was wrong anyway since it wasn't coverting handle to
  // identity. Inlined it.
  int type = _exptype(makeNodeIdentity(nodeHandle));
  type=(NULL != type) ? getNodeType(nodeHandle) : NULL;

  if(TEXT_NODE!=type && CDATA_SECTION_NODE!=type)
    return getNode(nodeHandle).getNodeValue();

  // If this is a DTM text node, it may be made of multiple DOM text
  // nodes -- including navigating into Entity References. DOM2DTM
  // records the first node in the sequence and requires that we
  // pick up the others when we retrieve the DTM node's value.
  //
  // %REVIEW% DOM Level 3 is expected to add a "whole text"
  // retrieval method which performs this function for us.
  Node node = getNode(nodeHandle);
  Node n=logicalNextDOMTextNode(node);
  if(n==null)
    return node.getNodeValue();

  FastStringBuffer buf = StringBufferPool.get();
      buf.append(node.getNodeValue());
  while(n!=null)
  {
    buf.append(n.getNodeValue());
    n=logicalNextDOMTextNode(n);
  }
  String s = (buf.length() > 0) ? buf.toString() : "";
  StringBufferPool.free(buf);
  return s;
}
 
Example 13
Source File: NodeUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * get the attribute with the given name from the given node
 * @param node - the node to look in
 * @param attributeName - the attribute's name to look for
 * @param defaultValue
 * @return - the value - defaultValue as default
 */
public static String getNodeAttribute( Node node, String attributeName, String defaultValue ) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null) return defaultValue;

    Node attribute = attributes.getNamedItem( attributeName );
    return (attribute == null) ? defaultValue : attribute.getNodeValue();
}
 
Example 14
Source File: NodeImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests whether two nodes are equal.
 * <br>This method tests for equality of nodes, not sameness (i.e.,
 * whether the two nodes are references to the same object) which can be
 * tested with <code>Node.isSameNode</code>. All nodes that are the same
 * will also be equal, though the reverse may not be true.
 * <br>Two nodes are equal if and only if the following conditions are
 * satisfied: The two nodes are of the same type.The following string
 * attributes are equal: <code>nodeName</code>, <code>localName</code>,
 * <code>namespaceURI</code>, <code>prefix</code>, <code>nodeValue</code>
 * , <code>baseURI</code>. This is: they are both <code>null</code>, or
 * they have the same length and are character for character identical.
 * The <code>attributes</code> <code>NamedNodeMaps</code> are equal.
 * This is: they are both <code>null</code>, or they have the same
 * length and for each node that exists in one map there is a node that
 * exists in the other map and is equal, although not necessarily at the
 * same index.The <code>childNodes</code> <code>NodeLists</code> are
 * equal. This is: they are both <code>null</code>, or they have the
 * same length and contain equal nodes at the same index. This is true
 * for <code>Attr</code> nodes as for any other type of node. Note that
 * normalization can affect equality; to avoid this, nodes should be
 * normalized before being compared.
 * <br>For two <code>DocumentType</code> nodes to be equal, the following
 * conditions must also be satisfied: The following string attributes
 * are equal: <code>publicId</code>, <code>systemId</code>,
 * <code>internalSubset</code>.The <code>entities</code>
 * <code>NamedNodeMaps</code> are equal.The <code>notations</code>
 * <code>NamedNodeMaps</code> are equal.
 * <br>On the other hand, the following do not affect equality: the
 * <code>ownerDocument</code> attribute, the <code>specified</code>
 * attribute for <code>Attr</code> nodes, the
 * <code>isWhitespaceInElementContent</code> attribute for
 * <code>Text</code> nodes, as well as any user data or event listeners
 * registered on the nodes.
 * @param arg The node to compare equality with.
 * @param deep If <code>true</code>, recursively compare the subtrees; if
 *   <code>false</code>, compare only the nodes themselves (and its
 *   attributes, if it is an <code>Element</code>).
 * @return If the nodes, and possibly subtrees are equal,
 *   <code>true</code> otherwise <code>false</code>.
 * @since DOM Level 3
 */
public boolean isEqualNode(Node arg) {
    if (arg == this) {
        return true;
    }
    if (arg.getNodeType() != getNodeType()) {
        return false;
    }
    // in theory nodeName can't be null but better be careful
    // who knows what other implementations may be doing?...
    if (getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    }
    else if (!getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    }
    else if (!getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    }
    else if (!getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    }
    else if (!getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    }
    else if (!getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }


    return true;
}
 
Example 15
Source File: ResourceParser.java    From jqm with Apache License 2.0 4 votes vote down vote up
private static void importXml() throws NamingException
{
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try (InputStream is = ResourceParser.class.getClassLoader().getResourceAsStream(Helpers.resourceFile))
    {
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("resource");

        String jndiAlias = null, resourceClass = null, description = "no description", scope = null, auth = "Container", factory = null;
        boolean singleton = false;

        for (int i = 0; i < nList.getLength(); i++)
        {
            Node n = nList.item(i);
            Map<String, String> otherParams = new HashMap<>();

            NamedNodeMap attrs = n.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++)
            {
                Node attr = attrs.item(j);
                String key = attr.getNodeName();
                String value = attr.getNodeValue();

                if ("name".equals(key))
                {
                    jndiAlias = value;
                }
                else if ("type".equals(key))
                {
                    resourceClass = value;
                }
                else if ("description".equals(key))
                {
                    description = value;
                }
                else if ("factory".equals(key))
                {
                    factory = value;
                }
                else if ("auth".equals(key))
                {
                    auth = value;
                }
                else if ("singleton".equals(key))
                {
                    singleton = Boolean.parseBoolean(value);
                }
                else
                {
                    otherParams.put(key, value);
                }
            }

            if (resourceClass == null || jndiAlias == null || factory == null)
            {
                throw new NamingException("could not load the resource.xml file");
            }

            JndiResourceDescriptor jrd = new JndiResourceDescriptor(resourceClass, description, scope, auth, factory, singleton);
            for (Map.Entry<String, String> prm : otherParams.entrySet())
            {
                jrd.add(new StringRefAddr(prm.getKey(), prm.getValue()));
            }
            xml.put(jndiAlias, jrd);
        }
    }
    catch (Exception e)
    {
        NamingException pp = new NamingException("could not initialize the JNDI local resources");
        pp.setRootCause(e);
        throw pp;
    }
}
 
Example 16
Source File: MCRValidator.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public String getAttributeValue(String name) {
    NamedNodeMap attributes = ruleElement.getAttributes();
    Node attribute = attributes.getNamedItem(name);
    return attribute == null ? null : attribute.getNodeValue();
}
 
Example 17
Source File: DTMNodeProxy.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
     * DOM Level 3:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
		/*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
		*/
                return null;
            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);
                }
                return null;
            }
        default:{
	   /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 18
Source File: results.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 4 votes vote down vote up
private static String getValue(String tag, Element element) {
    NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
    Node node = nodeList.item(0);
    return node.getNodeValue();
}
 
Example 19
Source File: SvgFontProcessor.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void processFontAttributes(Element styleElement)
{
	String fontFamilyAttrValue = null;

	NamedNodeMap attributes = styleElement.getAttributes();
	
	Node fontFamilyAttrNode = attributes.getNamedItem(SVG_ATTRIBUTE_fontFamily);
	if (fontFamilyAttrNode != null)
	{
		fontFamilyAttrValue = fontFamilyAttrNode.getNodeValue();
	}
	
	// values from style property takes precedence over values from attributes
	
	Node styleAttrNode = attributes.getNamedItem(SVG_ATTRIBUTE_style);
	if (styleAttrNode != null)
	{
		String styleFontFamily = getFontFamily(styleAttrNode.getNodeValue());
		if (styleFontFamily != null)
		{
			fontFamilyAttrValue = styleFontFamily;
		}
	}
	
	if (fontFamilyAttrValue != null)
	{
		StringBuilder newFamilyNameAttrValue = new StringBuilder();
		
		boolean firstToken = true;
		String[] fontFamilyTokens = fontFamilyAttrValue.split(",");
		for (String fontFamilyToken : fontFamilyTokens)
		{
			if (!firstToken)
			{
				newFamilyNameAttrValue.append(",");
			}
			
			String fontFamily = fontFamilyToken.trim();
			
			if (
				fontFamily.startsWith("'")
				&& fontFamily.endsWith("'") 
				)
			{
				fontFamily = fontFamily.substring(1, fontFamily.length() - 1);
			}
			
			// svg font-family could have locale suffix because it is needed in svg measured by phantomjs
			int localeSeparatorPos = fontFamily.lastIndexOf(HtmlFontFamily.LOCALE_SEPARATOR);
			if (localeSeparatorPos > 0)
			{
				fontFamily = fontFamily.substring(0, localeSeparatorPos);
			}

			fontFamily = 
				getFontFamily(
					fontFamily, 
					locale //FIXMEBATIK could use locale from svg above
					);
					
			newFamilyNameAttrValue.append(" " + fontFamily);

			firstToken = false;
		}

		if (styleAttrNode == null)
		{
			// do not put single quotes around family name here because the value might already contain quotes, 
			// especially if it is coming from font extension export configuration
			styleElement.setAttribute(SVG_ATTRIBUTE_fontFamily, newFamilyNameAttrValue.toString());
		}
		else
		{
			String newStyleAttr = replaceOrAddFontFamilyInStyle(styleAttrNode.getNodeValue(), newFamilyNameAttrValue.toString());
			styleElement.setAttribute(SVG_ATTRIBUTE_style, newStyleAttr);
		}
	}
}
 
Example 20
Source File: AttList.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Look up an attribute's value by Namespace name.
 *
 * @param uri The Namespace URI, or the empty String if the
 *        name has no Namespace URI.
 * @param localName The local name of the attribute.
 * @return The attribute value as a string, or null if the
 *         attribute is not in the list.
 */
public String getValue(String uri, String localName)
{
              Node a=m_attrs.getNamedItemNS(uri,localName);
              return (a==null) ? null : a.getNodeValue();
}