List of usage examples for org.w3c.dom Element hasChildNodes
public boolean hasChildNodes();
From source file:org.apache.hadoop.mapred.QueueConfigurationParser.java
/** * @param parent Name of the parent queue * @param queueNode/*from w ww .ja va 2 s . c om*/ * @return */ private Queue createHierarchy(String parent, Element queueNode) { if (queueNode == null) { return null; } //Name of the current queue. //Complete qualified queue name. String name = ""; Queue newQueue = new Queue(); Map<String, AccessControlList> acls = new HashMap<String, AccessControlList>(); NodeList fields = queueNode.getChildNodes(); validate(queueNode); List<Element> subQueues = new ArrayList<Element>(); String submitKey = ""; String adminKey = ""; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) { continue; } Element field = (Element) fieldNode; if (QUEUE_NAME_TAG.equals(field.getTagName())) { String nameValue = field.getTextContent(); if (field.getTextContent() == null || field.getTextContent().trim().equals("") || field.getTextContent().contains(NAME_SEPARATOR)) { throw new RuntimeException("Improper queue name : " + nameValue); } if (!parent.equals("")) { name += parent + NAME_SEPARATOR; } //generate the complete qualified name //parent.child name += nameValue; newQueue.setName(name); submitKey = toFullPropertyName(name, QueueACL.SUBMIT_JOB.getAclName()); adminKey = toFullPropertyName(name, QueueACL.ADMINISTER_JOBS.getAclName()); } if (QUEUE_TAG.equals(field.getTagName()) && field.hasChildNodes()) { subQueues.add(field); } if (isAclsEnabled()) { if (ACL_SUBMIT_JOB_TAG.equals(field.getTagName())) { acls.put(submitKey, new AccessControlList(field.getTextContent())); } if (ACL_ADMINISTER_JOB_TAG.equals(field.getTagName())) { acls.put(adminKey, new AccessControlList(field.getTextContent())); } } if (PROPERTIES_TAG.equals(field.getTagName())) { Properties properties = populateProperties(field); newQueue.setProperties(properties); } if (STATE_TAG.equals(field.getTagName())) { String state = field.getTextContent(); newQueue.setState(QueueState.getState(state)); } } if (!acls.containsKey(submitKey)) { acls.put(submitKey, new AccessControlList(" ")); } if (!acls.containsKey(adminKey)) { acls.put(adminKey, new AccessControlList(" ")); } //Set acls newQueue.setAcls(acls); //At this point we have the queue ready at current height level. //so we have parent name available. for (Element field : subQueues) { newQueue.addChild(createHierarchy(newQueue.getName(), field)); } return newQueue; }
From source file:org.apache.jxtadoop.conf.Configuration.java
private void loadResource(Properties properties, Object name, boolean quiet) { try {/*from w w w . ja va 2 s. 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 && value != null) { if (!finalParameters.contains(attr)) { properties.setProperty(attr, value); if (finalParameter) finalParameters.add(attr); } else { LOG.warn(name + ":a attempt to override final parameter: " + attr + "; Ignoring."); } } } } 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.nutch.net.urlnormalizer.regex.RegexURLNormalizer.java
private List readConfiguration(InputStream is) { Perl5Compiler compiler = new Perl5Compiler(); List rules = new ArrayList(); try {//from w w w.ja v a2s. c om // borrowed heavily from code in Configuration.java Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); Element root = doc.getDocumentElement(); if ((!"regex-normalize".equals(root.getTagName())) && (LOG.isFatalEnabled())) { LOG.fatal("bad conf file: top-level element not <regex-normalize>"); } NodeList regexes = root.getChildNodes(); for (int i = 0; i < regexes.getLength(); i++) { Node regexNode = regexes.item(i); if (!(regexNode instanceof Element)) continue; Element regex = (Element) regexNode; if ((!"regex".equals(regex.getTagName())) && (LOG.isWarnEnabled())) { LOG.warn("bad conf file: element not <regex>"); } NodeList fields = regex.getChildNodes(); String patternValue = null; String subValue = null; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) continue; Element field = (Element) fieldNode; if ("pattern".equals(field.getTagName()) && field.hasChildNodes()) patternValue = ((Text) field.getFirstChild()).getData(); if ("substitution".equals(field.getTagName()) && field.hasChildNodes()) subValue = ((Text) field.getFirstChild()).getData(); if (!field.hasChildNodes()) subValue = ""; } if (patternValue != null && subValue != null) { Rule rule = new Rule(); rule.pattern = (Perl5Pattern) compiler.compile(patternValue); rule.substitution = subValue; rules.add(rule); } } } catch (Exception e) { if (LOG.isFatalEnabled()) { LOG.fatal("error parsing conf file: " + e); } return EMPTY_RULES; } if (rules.size() == 0) return EMPTY_RULES; return rules; }
From source file:org.apache.oozie.cli.OozieCLI.java
private Properties parseDocument(Document doc, Properties conf) throws IOException { try {// w w w.j a v a 2 s .c om Element root = doc.getDocumentElement(); if (!"configuration".equals(root.getLocalName())) { throw new RuntimeException("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.getLocalName())) { throw new RuntimeException("bad conf file: element not <property>"); } NodeList fields = prop.getChildNodes(); String attr = null; 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.getLocalName()) && field.hasChildNodes()) { attr = ((Text) field.getFirstChild()).getData(); } if ("value".equals(field.getLocalName()) && field.hasChildNodes()) { value = ((Text) field.getFirstChild()).getData(); } } if (attr != null && value != null) { conf.setProperty(attr, value); } } return conf; } catch (DOMException e) { throw new IOException(e); } }
From source file:org.apache.openaz.xacml.pdp.policy.dom.DOMAttributeAssignmentExpression.java
public static boolean repair(Node nodeAttributeAssignmentExpression) throws DOMStructureException { Element elementAttributeAssignmentExpression = DOMUtil.getElement(nodeAttributeAssignmentExpression); boolean result = false; if (DOMUtil.getFirstChildElement(elementAttributeAssignmentExpression) == null) { /*/*from ww w .j av a 2 s. c o m*/ * See if we can repair the <AttributeAssignmentExpression * DataType="">string</AttributeAssignmentExpression> pattern */ Identifier identifier = DOMUtil.getIdentifierAttribute(elementAttributeAssignmentExpression, XACML3.ATTRIBUTE_DATATYPE); String textContent = elementAttributeAssignmentExpression.getTextContent(); if (textContent != null) { textContent = textContent.trim(); } if (textContent != null && textContent.length() > 0 && identifier != null) { Element attributeValue = elementAttributeAssignmentExpression.getOwnerDocument() .createElementNS(XACML3.XMLNS, XACML3.ELEMENT_ATTRIBUTEVALUE); attributeValue.setAttribute(XACML3.ATTRIBUTE_DATATYPE, identifier.stringValue()); attributeValue.setTextContent(textContent); logger.warn("Adding a new AttributeValue using the DataType from the AttributeAssignment"); elementAttributeAssignmentExpression.removeAttribute(XACML3.ATTRIBUTE_DATATYPE); while (elementAttributeAssignmentExpression.hasChildNodes()) { elementAttributeAssignmentExpression .removeChild(elementAttributeAssignmentExpression.getFirstChild()); } elementAttributeAssignmentExpression.appendChild(attributeValue); result = true; } else { throw DOMUtil.newMissingElementException(elementAttributeAssignmentExpression, XACML3.XMLNS, XACML3.ELEMENT_EXPRESSION); } } result = DOMUtil.repairIdentifierAttribute(elementAttributeAssignmentExpression, XACML3.ATTRIBUTE_ATTRIBUTEID, logger) || result; return result; }
From source file:org.apache.safe.service.Configuration.java
private void loadResource(Properties properties, Object name) { try {/* w w w.j a va 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) { doc = builder.parse(url.toString()); } } else if (name instanceof String) { // a CLASSPATH resource URL url = getResource((String) name); if (url != null) { doc = builder.parse(url.toString()); } } 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) { 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); 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) { properties.setProperty(attr, value); } } } } 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.safe.service.FileBasedAuthorizationService.java
void loadResource(InputStream input) { try {/*from w ww. java 2s. c om*/ 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; try { doc = builder.parse((InputStream) input); } finally { ((InputStream) input).close(); } if (doc == null && root == null) { throw new RuntimeException(input + " not found"); } if (root == null) { root = doc.getDocumentElement(); } if (!"acls".equals(root.getTagName())) LOG.fatal("bad conf file: top-level element not <acls>"); 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 (!"acl".equals(prop.getTagName())) LOG.warn("bad conf file: element not <acl>"); NodeList fields = prop.getChildNodes(); String key = null; String users = null; String groups = null; for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) continue; Element field = (Element) fieldNode; if ("key".equals(field.getTagName()) && field.hasChildNodes()) key = ((Text) field.getFirstChild()).getData().trim(); if ("users".equals(field.getTagName()) && field.hasChildNodes()) users = ((Text) field.getFirstChild()).getData().trim(); if ("groups".equals(field.getTagName()) && field.hasChildNodes()) groups = ((Text) field.getFirstChild()).getData(); if (key != null) { Set<String> userSet = null; Set<String> groupSet = null; if (users != null && !users.isEmpty()) { userSet = new HashSet<String>(Arrays.asList(users.split(","))); } if (groups != null && !groups.isEmpty()) { groupSet = new HashSet<String>(Arrays.asList(groups.split(","))); } ACL acl = new ACL(userSet, groupSet); acls.put(key, acl); } } } } 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.sling.its.servlets.ItsImportServlet.java
/** * If element has child elements, don't process them and skip those nodes. * * @param element// w w w . j a va 2 s .c om * current element * @param itsEng * the ITSEngine */ private void skipChildren(final Element element, final ITraversal itsEng) { if (element.hasChildNodes()) { Node node; while ((node = itsEng.nextNode()) != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (itsEng.backTracking() && StringUtils.equals(node.getLocalName(), element.getLocalName())) { break; } } } } }
From source file:org.apache.sling.its.servlets.ItsImportServlet.java
/** * Store the global rule.//www . j a va2s. c o m * * @param element * an Element from the Document object. * @param resourceType * resource type * @param itsEng * the ITSEngine */ private void storeGlobalRule(final Element element, final String resourceType, final ITraversal itsEng) { if (StringUtils.isNotBlank(resourceType)) { String globalPath = SlingItsConstants.getGlobalRules().get(element.getLocalName()) + resourceType; if (element.getPrefix() != null) { globalPath += String.format("/%s(%d)", element.getLocalName(), getCounter(globalPath + "/" + element.getLocalName())); element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix()); } else { globalPath += String.format("/%s(%d)", element.getNodeName(), getCounter(globalPath + "/" + element.getNodeName())); } output(globalPath, null, null); if (element.getLocalName().equals("param")) { output(globalPath, null, element.getTextContent()); } else if (element.getLocalName().equals(SlingItsConstants.ITS_LOCNOTE_RULE) && element.hasChildNodes()) { final Element locNoteElement = (Element) XmlNodeUtils.getChildNodeByLocalName(element, SlingItsConstants.ITS_LOCNOTE); if (locNoteElement != null) { element.setAttribute(SlingItsConstants.ITS_NOTE, locNoteElement.getTextContent()); } } setAttributes(element, globalPath); } skipChildren(element, itsEng); }
From source file:org.apache.sling.its.servlets.ItsImportServlet.java
/** * Store the element and its attribute. The child node of global rules are * specially handled so they will not be traversed. * * @param path//from w w w . java 2s .c o m * the target path * @param resourceType * the resourceType * @param doc * the document * @param file * the file. * @param isExternalDoc * true if this is for storing global rules for external documents */ private void store(String path, final String resourceType, final Document doc, final File file, final boolean isExternalDoc) { final ITraversal itsEng = applyITSRules(doc, file, null, false); itsEng.startTraversal(); Node node; while ((node = itsEng.nextNode()) != null) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; // Use !backTracking() to get to the elements only once // and to include the empty elements (for attributes). if (itsEng.backTracking()) { if (!SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) { path = backTrack(path); } } else { if (element.isSameNode(doc.getDocumentElement()) && !isExternalDoc) { path += "/" + element.getNodeName(); output(path, null, null); setAttributes(element, path); } else if (SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) { storeGlobalRule(element, resourceType, itsEng); } else if (!isExternalDoc && !SlingItsConstants.getGlobalRules().containsKey(element.getLocalName()) && !(element.getParentNode().getLocalName().equals(SlingItsConstants.ITS_RULES) && element.getParentNode().getPrefix() != null)) { if (element.getLocalName().equals(SlingItsConstants.ITS_RULES) && element.getPrefix() != null) { this.hasGlobalRules = true; } if (element.getPrefix() != null) { path += String.format("/%s(%d)", element.getLocalName(), getCounter(path + "/" + element.getLocalName())); element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix()); } else if (element.getNodeName().equals("link") && StringUtils.endsWith(element.getAttribute("rel"), "-rules")) { path += String.format("/%s(%d)", SlingItsConstants.ITS_RULES, getCounter(path + "/" + SlingItsConstants.ITS_RULES)); final String prefix = StringUtils.substringBefore(element.getAttribute("rel"), "-rules"); element.setAttribute(SlingItsConstants.NODE_PREFIX, prefix); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + prefix, Namespaces.ITS_NS_URI); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:h", Namespaces.HTML_NS_URI); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:jcr", NamespaceRegistry.NAMESPACE_JCR); this.hasGlobalRules = true; } else { path += String.format("/%s(%d)", element.getNodeName(), getCounter(path + "/" + element.getNodeName())); } output(path, null, null); setAttributes(element, path); if (!element.hasChildNodes()) // Empty elements: { path = backTrack(path); } } } break; case Node.TEXT_NODE: if (StringUtils.isNotBlank(node.getNodeValue()) && !isExternalDoc) { path += String.format("/%s(%d)", SlingItsConstants.TEXT_CONTENT_NODE, getCounter(path + "/" + SlingItsConstants.TEXT_CONTENT_NODE)); output(path, null, node.getNodeValue()); path = backTrack(path); } break; default: break; } } }