List of usage examples for org.w3c.dom Node getLocalName
public String getLocalName();
From source file:nl.b3p.imagetool.CombineArcServerUrl.java
/** * Create a new CombineImageUrl with the given values * In this implementation the body is changed. * @param width width/* ww w. j a v a 2 s . c o m*/ * @param height height * @param bbox bbox * @return new clone of this CombineImageUrl but with changed values. * @see CombineImageUrl#calculateNewUrl(java.lang.Integer, java.lang.Integer, nl.b3p.viewer.image.Bbox) */ @Override public List<CombineImageUrl> calculateNewUrl(ImageBbox imbbox) { Integer width = imbbox.getWidth(); Integer height = imbbox.getHeight(); Bbox bbox = imbbox.getBbox(); CombineArcServerUrl ciu = new CombineArcServerUrl(this); try { Document doc = bodyAsDocument(); Node root = doc.getFirstChild(); //change the bbox Node extent = (Node) xPathExtent.evaluate(root, XPathConstants.NODE); NodeList nl = extent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if ("XMin".equals(child.getLocalName())) { child.setTextContent("" + bbox.getMinx()); } else if ("YMin".equals(child.getLocalName())) { child.setTextContent("" + bbox.getMiny()); } else if ("XMax".equals(child.getLocalName())) { child.setTextContent("" + bbox.getMaxx()); } else if ("YMax".equals(child.getLocalName())) { child.setTextContent("" + bbox.getMaxy()); } } //image size Node imageSize = (Node) xPathImageDisplay.evaluate(root, XPathConstants.NODE); nl = imageSize.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if ("ImageHeight".equals(child.getLocalName())) { child.setTextContent(height.toString()); } else if ("ImageWidth".equals(child.getLocalName())) { child.setTextContent(width.toString()); } } ciu.setBody(doc); } catch (Exception e) { log.warn("Error while changing body fragment", e); } List<CombineImageUrl> list = new ArrayList<CombineImageUrl>(); list.add(ciu); return list; }
From source file:nl.b3p.viewer.stripes.SldActionBean.java
private void addFilterToExistingSld() throws Exception { Filter f = CQL.toFilter(filter); f = (Filter) f.accept(new ChangeMatchCase(false), null); if (featureTypeName == null) { featureTypeName = layer;/*www .jav a2s .co m*/ } FeatureTypeConstraint ftc = sldFactory.createFeatureTypeConstraint(featureTypeName, f, new Extent[] {}); if (newSld == null) { SLDTransformer sldTransformer = new SLDTransformer(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); sldTransformer.transform(ftc, bos); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document sldXmlDoc = db.parse(new ByteArrayInputStream(sldXml)); Document ftcDoc = db.parse(new ByteArrayInputStream(bos.toByteArray())); String sldVersion = sldXmlDoc.getDocumentElement().getAttribute("version"); if ("1.1.0".equals(sldVersion)) { // replace sld:FeatureTypeName element generated by GeoTools // by se:FeatureTypeName NodeList sldFTNs = ftcDoc.getElementsByTagNameNS(NS_SLD, "FeatureTypeName"); if (sldFTNs.getLength() == 1) { Node sldFTN = sldFTNs.item(0); Node seFTN = ftcDoc.createElementNS(NS_SE, "FeatureTypeName"); seFTN.setTextContent(sldFTN.getTextContent()); sldFTN.getParentNode().replaceChild(seFTN, sldFTN); } } // Ignore namespaces to tackle both SLD 1.0.0 and SLD 1.1.0 // Add constraint to all NamedLayers, not only to the layer specified // in layers parameter NodeList namedLayers = sldXmlDoc.getElementsByTagNameNS(NS_SLD, "NamedLayer"); for (int i = 0; i < namedLayers.getLength(); i++) { Node namedLayer = namedLayers.item(i); // Search where to insert the FeatureTypeConstraint from our ftcDoc // Insert LayerFeatureConstraints after sld:Name, se:Name or se:Description // and before sld:NamedStyle or sld:UserStyle so search backwards. // If we find an existing LayerFeatureConstraints, use that NodeList childs = namedLayer.getChildNodes(); Node insertBefore = null; Node layerFeatureConstraints = null; int j = childs.getLength() - 1; do { Node child = childs.item(j); if ("LayerFeatureConstraints".equals(child.getLocalName())) { layerFeatureConstraints = child; break; } if ("Description".equals(child.getLocalName()) || "Name".equals(child.getLocalName())) { break; } insertBefore = child; j--; } while (j >= 0); Node featureTypeConstraint = sldXmlDoc.adoptNode(ftcDoc.getDocumentElement().cloneNode(true)); if (layerFeatureConstraints == null) { layerFeatureConstraints = sldXmlDoc.createElementNS(NS_SLD, "LayerFeatureConstraints"); layerFeatureConstraints.appendChild(featureTypeConstraint); namedLayer.insertBefore(layerFeatureConstraints, insertBefore); } else { layerFeatureConstraints.appendChild(featureTypeConstraint); } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(sldXmlDoc); bos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(bos); t.transform(source, result); sldXml = bos.toByteArray(); } }
From source file:nl.ellipsis.webdav.server.util.XMLHelper.java
public static Vector<String> getPropertiesFromXML(Node propNode) { Vector<String> properties; properties = new Vector<String>(); NodeList childList = propNode.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node currentNode = childList.item(i); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { String nodeName = currentNode.getLocalName(); String namespace = currentNode.getNamespaceURI(); String propertyName = null; if (nodeName.indexOf(':') != -1) { propertyName = nodeName.substring(nodeName.indexOf(':') + 1); } else { propertyName = nodeName; }/* w ww . j a v a 2 s . c o m*/ // href is a live property which is handled differently properties.addElement(propertyName); } } return properties; }
From source file:nz.govt.natlib.ndha.wctdpsdepositor.extractor.XPathWctMetsExtractor.java
private void populateSeedUrlsFrom(Document doc, XPath xpath) throws XPathExpressionException { NodeList seedNodes = (NodeList) xpath.evaluate(seedUrlsQuery, doc, XPathConstants.NODESET); //int l = seedNodes.getLength(); for (int i = 0; i < seedNodes.getLength(); i++) { Node node = seedNodes.item(i); if (node == null) continue; NodeList childNodes = node.getChildNodes(); if (childNodes.getLength() <= 0) continue; String url = null;//from www . j a v a 2 s .co m String type = null; for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); String nodeName = childNode.getLocalName(); if ("SeedURL".equals(nodeName)) url = childNode.getTextContent(); else if ("SeedType".equals(nodeName)) type = childNode.getTextContent(); } SeedUrl.Type typeAsEnum; if (url != null) { try { typeAsEnum = SeedUrl.Type.valueOf(type); } catch (RuntimeException e) { typeAsEnum = SeedUrl.Type.Primary; } SeedUrl seedUrl = new SeedUrl(url, typeAsEnum); seedUrls.add(seedUrl); } } }
From source file:org.adeptnet.auth.saml.SAMLClient.java
private Element[] selectNodes(final Node _sibling, final String uri, final String nodeName) { final List<Element> list = new ArrayList<>(); Node sibling = _sibling; while (sibling != null) { if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri) && sibling.getLocalName().equals(nodeName)) { list.add((Element) sibling); }/* ww w . j a va2s. co m*/ sibling = sibling.getNextSibling(); } return list.toArray(new Element[list.size()]); }
From source file:org.adeptnet.auth.saml.SAMLClient.java
private void initMap() { if (!map.isEmpty()) { return;/* w ww. j a v a 2s. c om*/ } final InputStream is = org.apache.xml.security.Init.class.getResourceAsStream("resource/config.xml"); if (is == null) { LOG.error("cannot read resource/config.xml"); return; } try { /* read library configuration file */ final DocumentBuilder db = createDocumentBuilder(false, true); final Document doc = db.parse(is); Node node = doc.getFirstChild(); for (; node != null; node = node.getNextSibling()) { if ("Configuration".equals(node.getLocalName())) { break; } } if (node == null) { LOG.error("Error in reading configuration file - Configuration element not found"); return; } for (Node el = node.getFirstChild(); el != null; el = el.getNextSibling()) { if (Node.ELEMENT_NODE != el.getNodeType()) { continue; } if (!"JCEAlgorithmMappings".equals(el.getLocalName())) { continue; } final Node algorithmsNode = ((Element) el).getElementsByTagName("Algorithms").item(0); if (algorithmsNode == null) { continue; } final Element[] algorithms = selectNodes(algorithmsNode.getFirstChild(), Init.CONF_NS, "Algorithm"); for (final Element element : algorithms) { final String algoClass = element.getAttributeNS(null, "AlgorithmClass"); if (!"Signature".equals(algoClass)) { continue; } final String uri = element.getAttributeNS(null, "URI"); final String name = element.getAttributeNS(null, "JCEName"); map.put(name, uri); if (LOG.isDebugEnabled()) { LOG.debug(String.format("Mapping %s - %s", name, uri)); } } } } catch (ParserConfigurationException | SAXException | IOException | DOMException ex) { LOG.error(ex.getMessage(), ex); } }
From source file:org.adl.parsers.dom.ADLDOMParser.java
/** * This method checks to see if the node that is being processed contains * an xsi:schemaLocation attribute. If it does, the method adds the name * value of the attribute to the list of schemaLocations. * //from www . jav a2 s. co m * @param iNode The node that is being processed * @param iXMLFileName The name of the XML test subject */ private void checkForSchemaLocations(Node iNode, String iXMLFileName) { log.debug("checkForSchemaLocations()"); log.debug("Processing Node: [" + iNode.getLocalName() + "]"); log.debug("Processing Nodes Namespace: [" + iNode.getNamespaceURI() + "]"); // Get the list of attributes of the element NamedNodeMap attrList = iNode.getAttributes(); // Loop over the attributes for this element, remove any attributes // that are extensions log.debug("Processing " + attrList.getLength() + " attributes"); for (int i = 0; i < attrList.getLength(); i++) { Attr currentAttribute = (Attr) attrList.item(i); String parentNamespace = iNode.getNamespaceURI(); String attributeNamespace = currentAttribute.getNamespaceURI(); log.debug("Processing attribute [" + i + "]: [" + currentAttribute.getLocalName() + "]"); log.debug("Attributes Namespace [" + i + "]: [" + attributeNamespace + "]"); if (!mDeclaredNamespaces.contains(attributeNamespace) && (attributeNamespace != null)) { mDeclaredNamespaces.add(attributeNamespace); } log.debug("Attributes Parent Node [" + i + "]: [" + iNode.getLocalName() + "]"); log.debug("Parent Nodes Namespace [" + i + "]: [" + parentNamespace + "]"); if (!mDeclaredNamespaces.contains(attributeNamespace) && (parentNamespace != null)) { mDeclaredNamespaces.add(parentNamespace); } // If one of the attributes is xsi:schemaLocation, then // save off the namespace and the schema location. These // will be used later during the validation process if (currentAttribute.getLocalName().equals("schemaLocation") && currentAttribute.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema-instance")) { // A schema location has been defined in the XML, don't // use the defaults if (mFirstTimeSchemaLocationFound) { // Don't use the default schema locations mSchemaLocation = null; mFirstTimeSchemaLocationFound = false; // flag that xsi:schemalocation attribute has been declared mSchemaLocExists = true; } addSchemaLocationToList(currentAttribute.getValue(), iXMLFileName); } } // end looping over attributes log.debug("checkForSchemaLocations()"); }
From source file:org.adl.parsers.dom.ADLDOMParser.java
/** * Traverses the DOM Tree and removes Ignorable Whitespace Text Nodes and * Comment Text. The function also removes extension elements and * attributes that are not defined by SCORM. If extensions are found * the function also sets a flag stating that extensions are present in the * input XML instance.// ww w .j av a2 s.com * * @param iNode The node to be pruned of whitespace and comments<br> * @param iXMLFileName The XML file to be pruned */ private void pruneTree(Node iNode, String iXMLFileName) { String value; // is there anything to do? if (iNode == null) return; switch (iNode.getNodeType()) { case Node.PROCESSING_INSTRUCTION_NODE: { break; } case Node.DOCUMENT_NODE: { pruneTree(((Document) iNode).getDocumentElement(), iXMLFileName); break; } case Node.ELEMENT_NODE: { log.debug("Processing Element Node: [" + iNode.getLocalName() + "]"); log.debug("************************************************"); log.debug("Processing Element Node: [" + iNode.getLocalName() + "]"); checkForSchemaLocations(iNode, iXMLFileName); // Get the list of attributes of the element NamedNodeMap attrList = iNode.getAttributes(); // Loop over the attributes for this element, remove any attributes // that are extensions log.debug("Processing " + attrList.getLength() + " attributes"); for (int i = 0; i < attrList.getLength(); i++) { Attr currentAttribute = (Attr) attrList.item(i); if (!(DOMTreeUtility.isSCORMAppProfileNode(currentAttribute, iNode))) { log.debug("Extension attribute, removing: [" + currentAttribute.getNamespaceURI() + "] " + currentAttribute.getLocalName() + " from the its parent node [" + iNode.getNamespaceURI() + "] " + iNode.getLocalName()); // Remove the Element Node from the DOM attrList.removeNamedItemNS(currentAttribute.getNamespaceURI(), currentAttribute.getLocalName()); i--; mExtensionsFound = true; } else { log.debug("Valid SCORM attribute, keeping attribute: [" + currentAttribute.getNamespaceURI() + "] " + currentAttribute.getLocalName()); } } log.debug("Done processing attributes for node: [" + iNode.getNamespaceURI() + "] " + iNode.getLocalName()); log.debug("************************************************"); // Done looping over the attributes for this element, now loop over // the set of children nodes. log.debug(""); log.debug("************************************************"); log.debug("Processing direct-descendances for node: [" + iNode.getNamespaceURI() + "] " + iNode.getLocalName()); NodeList children = iNode.getChildNodes(); if (children != null) { // Loop over set of children elements for this element, remove // any elements that are extensions log.debug("Processing " + children.getLength() + " elements"); for (int z = 0; z < children.getLength(); z++) { Node childNode = children.item(z); if (childNode.getNodeType() == Node.ELEMENT_NODE) { log.debug("Processing element: [" + childNode + "]"); log.debug("Elements Namespace: [" + childNode.getNamespaceURI() + "]"); log.debug("Elements Parent Node: [" + iNode.getLocalName() + "]"); log.debug("Parent Nodes Namespace: [" + iNode.getNamespaceURI() + "]"); if (!(DOMTreeUtility.isSCORMAppProfileNode(children.item(z), children.item(z).getParentNode()))) { // Before we remove the element see if the elemen // contains any xsi:schemaLocations. We need // to add them to the list of schema locations for // parsing checkForSchemaLocations(childNode, iXMLFileName); log.debug("Extension Element Found, removing from DOM Tree "); // Remove the Element Node from the DOM children.item(z).getParentNode().removeChild(children.item(z)); z--; mExtensionsFound = true; } else { log.debug("ADL SCORM Element Found, leaving " + "element in DOM Tree"); pruneTree(children.item(z), iXMLFileName); } } // end if NodeType == ELEMENT_NODE if (childNode instanceof TextImpl) { value = children.item(z).getNodeValue().trim(); if (((TextImpl) children.item(z)).isIgnorableWhitespace()) { iNode.removeChild(children.item(z)); z--; } else if (value.length() == 0) { iNode.removeChild(children.item(z)); z--; } } else if (children.item(z).getNodeType() == Node.COMMENT_NODE) { iNode.removeChild(children.item(z)); z--; } } // end looping over children nodes } // end if there are children log.debug("Done processing direct-descendants for node: [" + iNode.getNamespaceURI() + "] " + iNode.getLocalName()); log.debug("**************************************************"); break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { NodeList children = iNode.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { pruneTree(children.item(i), iXMLFileName); } } break; } // text case Node.COMMENT_NODE: { break; } case Node.CDATA_SECTION_NODE: { break; } case Node.TEXT_NODE: { break; } default: { break; } } }
From source file:org.adl.parsers.dom.DOMTreeUtility.java
/** * This method returns the attribute of the given node whose name matches * the named value (iAttributeName) and a particular namespace * (iNamespaceForAttr)./*from ww w . j av a2 s .com*/ * * @param iNode The element containing the attribute * @param iAttributeName The name of the attribute being retrieved * * @return Returns the attribute matching the name and namespace */ public static Attr getAttribute(Node iNode, String iAttributeName) { log.debug("DOMTreeUtility getAttribute()"); Attr result = null; // Determine if the node is null if (iNode != null) { log.debug("Parent Node: " + iNode.getLocalName()); log.debug("Node being searched for: " + iAttributeName); // If the node is not null, then get the list of attributes from // the node NamedNodeMap attrList = iNode.getAttributes(); int numAttr = attrList.getLength(); Attr currentAttrNode = null; String currentNodeName = null; // Loop through the attributes and get their values assuming // that the multiplicity of each attribute is 1 and only 1. for (int k = 0; k < numAttr; k++) { // Get the attribute currentAttrNode = (Attr) attrList.item(k); // Get the local name of the attribute currentNodeName = currentAttrNode.getLocalName(); // First check to see if the current node is the one with the // same Local Name as the value we are looking for (iAttributeName) if (currentNodeName.equalsIgnoreCase(iAttributeName)) { // We have found a node that shares the same name as the // node we are looking for (iAttributeName). // Matching attribute found result = currentAttrNode; break; } } // end for loop } return result; }
From source file:org.adl.parsers.dom.DOMTreeUtility.java
/** * This method returns the value of the attribute that matches the * attribute name (iAttributeName) and namepace (iNamespaceForAttr) in * the node. This is to cover cases where elements have multiple * attributes that have the same local name but come from different * namespaces.// ww w . j a v a 2s. c o m * * @param iNode The element containing the attribute * @param iAttributeName The name of the attribute being retrieved * * @return Returns the value the attribute<br> */ public static String getAttributeValue(Node iNode, String iAttributeName) { log.debug("DOMTreeUtility getAttributeValue()"); log.debug("Parent Node: " + iNode.getLocalName()); log.debug("Node being searched for: " + iAttributeName); String result = ""; // Get the attribute from the node matching the attribute name // and namespace Attr theAttribute = getAttribute(iNode, iAttributeName); // Make sure the attribute was present for the element if (theAttribute != null) { // If present, retrieve the value of the attribute result = theAttribute.getValue(); } // return the value return result; }