protected static void copyAndReplaceElement(Element oldElement, Element newElement) { while(oldElement.getChildNodes().getLength() > 0) { newElement.appendChild(oldElement.getChildNodes().item(0)); } NamedNodeMap attrs = oldElement.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); newElement.setAttribute(ns.getNodeName(), ns.getNodeValue()); } ((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement); } }
private List<String> extractUsesPermissionNames(NodeList usesPermissionNodes) { List<String> usesPermissionQualifiedNames = new ArrayList<>(); for (int i = 0; i < usesPermissionNodes.getLength(); i++) { Node usesPermissionNode = usesPermissionNodes.item(i); Node nameAttribute = usesPermissionNode.getAttributes().getNamedItem("android:name"); if (nameAttribute == null) { return null; } usesPermissionQualifiedNames.add(nameAttribute.getNodeValue()); } return usesPermissionQualifiedNames; }
private boolean namedNodeMapsEqual(NamedNodeMap a, NamedNodeMap b) { if (a.getLength() != b.getLength()) { return false; } for (int i = 0; i < a.getLength(); i++) { Node aNode = a.item(i); Node bNode = aNode.getLocalName() == null ? b.getNamedItem(aNode.getNodeName()) : b.getNamedItemNS(aNode.getNamespaceURI(), aNode.getLocalName()); if (bNode == null || !aNode.isEqualNode(bNode)) { return false; } } return true; }
private int extractAttributeIntValue(Node node, String attribute, int defaultValue) { try { NamedNodeMap attributes = node.getAttributes(); if (attributes.getLength() > 0) { Node attributeNode = attributes.getNamedItem(attribute); if (attributeNode != null) { return Integer.parseInt(attributeNode.getNodeValue()); } } } catch (NumberFormatException ignored) { // we assume the manifest is well-formed } return defaultValue; }
private static HashMap<String, String> parseNodeAttributes(Node node) { final NamedNodeMap attributes = node.getAttributes(); final int attrCount = attributes.getLength(); final HashMap<String, String> receiverAttrs = new HashMap<>(attributes.getLength()); for (int i = 0; i < attrCount; i++) { Node attribute = attributes.item(i); String value = attribute.getNodeValue(); if (value != null) { receiverAttrs.put(attribute.getNodeName(), value); } } return receiverAttrs; }
public BeanDefinitionHolder decorateBeanDefinitionIfRequired( Element ele, BeanDefinitionHolder definitionHolder, @Nullable BeanDefinition containingBd) { BeanDefinitionHolder finalDefinition = definitionHolder; // Decorate based on custom attributes first. NamedNodeMap attributes = ele.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node node = attributes.item(i); finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } // Decorate based on custom nested elements. NodeList children = ele.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } } return finalDefinition; }
static class LoggingErrorListener implements ErrorListener { // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html boolean strict; public LoggingErrorListener(boolean strict) { } public void warning(TransformerException exception) { log.warn(exception.getMessage(), exception); // Don't throw an exception and stop the processor // just for a warning; but do log the problem } public void error(TransformerException exception) throws TransformerException { log.error(exception.getMessage(), exception); // XSLT is not as draconian as XML. There are numerous errors // which the processor may but does not have to recover from; // e.g. multiple templates that match a node with the same // priority. If I do not want to allow that, I'd throw this // exception here. if (strict) { throw exception; }
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) { String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs; NodeList nl = dom.getElementsByTagName( "jsFunction" ); for ( int i = 0; i < nl.getLength(); i++ ) { if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) { Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 ); if ( elSample.hasChildNodes() ) { return ( elSample.getFirstChild().getNodeValue() ); } } } return sRC; }
protected static void parseBridges(SiteConfig site_config, Node root) throws Exception { NodeList children=root.getChildNodes(); if(children == null || children.getLength() == 0) return; for(int i=0; i < children.getLength(); i++) { Node node=children.item(i); if(node.getNodeType() != Node.ELEMENT_NODE) continue; String node_name=node.getNodeName(); match(BRIDGE, node_name, true); NamedNodeMap attrs=node.getAttributes(); if(attrs == null || attrs.getLength() == 0) continue; Attr name_attr=(Attr)attrs.getNamedItem("name"); Attr config_attr=(Attr)attrs.getNamedItem("config"); String name=name_attr != null? name_attr.getValue() : null; String config=config_attr.getValue(); BridgeConfig bridge_config=new PropertiesBridgeConfig(name, config); site_config.addBridge(bridge_config); } }
private void writeElement(Element element, Writer writer, int depth) throws IOException { for (int i = 0; i < depth; ++i) { writer.append(' '); } writer.append('<').append(element.getTagName()); NamedNodeMap attrs = element.getAttributes(); for (int i = 0, imax = attrs.getLength(); i < imax; ++i) { Attr attr = (Attr) attrs.item(i); writer.append(' ').append(attr.getName()).append("='").append(attr.getValue()).append("'"); } NodeList nodes = element.getChildNodes(); if (nodes.getLength() == 0) { // no children, so just close off the element and return writer.append("/>").append(lineSeparator); return; } writer.append('>').append(lineSeparator); for (int i = 0, imax = nodes.getLength(); i < imax; ++i) { Node n = nodes.item(i); if (n.getNodeType() == Node.ATTRIBUTE_NODE) { continue; } write(n, writer, depth + indent); } for (int i = 0; i < depth; ++i) { writer.append(' '); } writer.append("</").append(element.getTagName()).append('>').append(lineSeparator); }
/** If the child node or attribute doesn't exist, it is created. Usage example: Node property = * getFirstChildByAttrValue(properties, "property", "name"); */ private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(childName)) { NamedNodeMap attributes = childNodes.item(i).getAttributes(); Node attribute = attributes.getNamedItem(attr); if (attribute.getNodeValue().equals(value)) return childNodes.item(i); } } Node newNode = node.getOwnerDocument().createElement(childName); NamedNodeMap attributes = newNode.getAttributes(); Attr nodeAttr = node.getOwnerDocument().createAttribute(attr); nodeAttr.setNodeValue(value); attributes.setNamedItem(nodeAttr); if (childNodes.item(0) != null) { return node.insertBefore(newNode, childNodes.item(0)); } else { return node.appendChild(newNode); } }
public static List<Person> readXMLCharacterList(Document doc) { List<Person> personList = new ArrayList<>(); NodeList characters = doc.getDocumentElement().getElementsByTagName("characters").item(0).getChildNodes(); for(int i = 0; i < characters.getLength(); i++) { Node child = characters.item(i); if(child.getNodeName().equals("character")) { String name = child.getAttributes().getNamedItem("name").getNodeValue(); char[] cName = name.toCharArray(); cName[0] = Character.toUpperCase(cName[0]); name = new String(cName); List<String> aliases = Arrays.asList(child.getAttributes().getNamedItem("aliases").getNodeValue().split(";")); String gender = (child.getAttributes().getNamedItem("gender") == null) ? "" : child.getAttributes().getNamedItem("gender").getNodeValue(); personList.add(new Person(child.getAttributes().getNamedItem("name").getNodeValue(), gender, aliases)); } } return personList; } //write the character list to a file to work with the annotator
public static void combineAllApplyTags(Document document) { NodeList nl = document.getElementsByTagName("apply"); while(nl.getLength() > 0) { Element app = (Element) nl.item(0); Element parent = (Element) app.getParentNode(); NamedNodeMap attrs = app.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); parent.setAttribute(ns.getNodeName(), ns.getNodeValue()); } while(app.getChildNodes().getLength() > 0) { Node ni = app.getChildNodes().item(0); app.getParentNode().insertBefore(ni, app); } app.getParentNode().removeChild(app); } }
private void parseAttrList(NodeList nodeList) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); if (tempNode.getNodeType() == Node.ELEMENT_NODE && tempNode.hasAttributes() && tempNode.hasChildNodes()) { String name = null; NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); if (node.getNodeName().equals("name")) { name = node.getNodeValue(); break; } } if (name != null && tempNode.getNodeName().equals("attr")) { parseValues(name, tempNode.getChildNodes()); } else { parseAttrList(tempNode.getChildNodes()); } } } }
Element documentElement = doc.getDocumentElement(); documentElement.normalize(); String applicationPackage = documentElement.getAttribute("package"); NodeList sdkNodes = documentElement.getElementsByTagName("uses-sdk"); if (sdkNodes.getLength() > 0) { Node sdkNode = sdkNodes.item(0); minSdkVersion = extractAttributeIntValue(sdkNode, "android:minSdkVersion", -1); maxSdkVersion = extractAttributeIntValue(sdkNode, "android:maxSdkVersion", -1); boolean applicationDebuggableMode = false; if (applicationNodes.getLength() > 0) { Node applicationNode = applicationNodes.item(0); Node nameAttribute = applicationNode.getAttributes().getNamedItem("android:name"); LOGGER.warn("The class application declared in the AndroidManifest.xml cannot be found in the compile path: [{}]", nameAttribute.getNodeValue()); Node debuggableAttribute = applicationNode.getAttributes().getNamedItem("android:debuggable"); if (debuggableAttribute != null) { applicationDebuggableMode = debuggableAttribute.getNodeValue().equalsIgnoreCase("true");
String typeId = propertyElement.getAttribute(PropertyDescriptorField.TYPE.attributeName()); NamedNodeMap atts = propertyElement.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Attr a = (Attr) atts.item(i); values.put(PropertyDescriptorField.getConstant(a.getName()), a.getValue()); NodeList children = propertyElement.getElementsByTagName(DEFAULT_VALUE.attributeName()); if (children.getLength() == 1) { values.put(DEFAULT_VALUE, children.item(0).getTextContent()); } else { throw new IllegalArgumentException("No value defined!");
private void parseValues(String name, NodeList nodeList) { MAttr attr = null; for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); if (tempNode.getNodeType() == Node.ELEMENT_NODE && tempNode.hasAttributes()) { if (attr == null) { if (tempNode.getNodeName().equals("enum")) { attr = new MAttr(MAttrType.ENUM); } else if (tempNode.getNodeName().equals("flag")) { NamedNodeMap attributes = tempNode.getAttributes(); Node nameNode = attributes.getNamedItem("name"); if (nameNode != null) { Node valueNode = attributes.getNamedItem("value"); if (valueNode != null) { try {
for (int i = 0; i < nodes.getLength(); ++i) { Node node = nodes.item(i); if (!(node instanceof Element)) { continue; Node child = node.getFirstChild(); NamedNodeMap attrs = node.getAttributes(); int lastAttr = attrs.getLength() - 1; for (int j = lastAttr; j--> 0; ) { Attr attr = (Attr) attrs.item(j); attributes.put(attr.getNodeName(), attr.getNodeValue()); child = child.getNextSibling(); continue;
private String getToolkit(Document document) { NodeList xmpmeta = document.getElementsByTagNameNS(XMP.NS_X, "xmpmeta"); if (xmpmeta == null || xmpmeta.getLength() <= 0) { return null; } Node toolkit = xmpmeta.item(0).getAttributes().getNamedItemNS(XMP.NS_X, "xmptk"); return toolkit != null ? toolkit.getNodeValue() : null; }
private Properties initJobProperties(Element root) { Properties properties = new Properties(); Node propertiesNode = root.getElementsByTagName(JOB_PROPERTIES_ELEMENT_NAME).item(0); if(propertiesNode != null) { NodeList children = propertiesNode.getChildNodes(); for(int i=0; i < children.getLength(); i++) { Node child = children.item(i); if(JOB_PROPERTY_ELEMENT_NAME.equals(child.getLocalName())) { NamedNodeMap attributes = child.getAttributes(); Node name = attributes.getNamedItem(JOB_PROPERTY_ELEMENT_NAME_ATTRIBUTE); Node value = attributes.getNamedItem(JOB_PROPERTY_ELEMENT_VALUE_ATTRIBUTE); properties.setProperty(name.getNodeValue(), value.getNodeValue()); } } } return properties; }