List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:org.apache.ode.bpel.runtime.AssignHelper.java
private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; }/* www . j av a 2 s. c o m*/ Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.pdfbox.pdmodel.fdf.FDFDictionary.java
/** * This will create an FDF dictionary from an XFDF XML document. * * @param fdfXML The XML document that contains the XFDF data. * @throws IOException If there is an error reading from the dom. *///from w w w . j a va2 s .c o m public FDFDictionary(Element fdfXML) { this(); NodeList nodeList = fdfXML.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { Element child = (Element) node; if (child.getTagName().equals("f")) { PDSimpleFileSpecification fs = new PDSimpleFileSpecification(); fs.setFile(child.getAttribute("href")); setFile(fs); } else if (child.getTagName().equals("ids")) { COSArray ids = new COSArray(); String original = child.getAttribute("original"); String modified = child.getAttribute("modified"); try { ids.add(COSString.parseHex(original)); } catch (IOException e) { LOG.warn("Error parsing ID entry for attribute 'original' [" + original + "]. ID entry ignored.", e); } try { ids.add(COSString.parseHex(modified)); } catch (IOException e) { LOG.warn("Error parsing ID entry for attribute 'modified' [" + modified + "]. ID entry ignored.", e); } setID(ids); } else if (child.getTagName().equals("fields")) { NodeList fields = child.getChildNodes(); List<FDFField> fieldList = new ArrayList<FDFField>(); for (int f = 0; f < fields.getLength(); f++) { Node currentNode = fields.item(f); if (currentNode instanceof Element && ((Element) currentNode).getTagName().equals("field")) { try { fieldList.add(new FDFField((Element) fields.item(f))); } catch (IOException e) { LOG.warn("Error parsing field entry [" + currentNode.getNodeValue() + "]. Field ignored.", e); } } } setFields(fieldList); } else if (child.getTagName().equals("annots")) { NodeList annots = child.getChildNodes(); List<FDFAnnotation> annotList = new ArrayList<FDFAnnotation>(); for (int j = 0; j < annots.getLength(); j++) { Node annotNode = annots.item(j); if (annotNode instanceof Element) { // the node name defines the annotation type Element annot = (Element) annotNode; String annotationName = annot.getNodeName(); try { if (annotationName.equals("text")) { annotList.add(new FDFAnnotationText(annot)); } else if (annotationName.equals("caret")) { annotList.add(new FDFAnnotationCaret(annot)); } else if (annotationName.equals("freetext")) { annotList.add(new FDFAnnotationFreeText(annot)); } else if (annotationName.equals("fileattachment")) { annotList.add(new FDFAnnotationFileAttachment(annot)); } else if (annotationName.equals("highlight")) { annotList.add(new FDFAnnotationHighlight(annot)); } else if (annotationName.equals("ink")) { annotList.add(new FDFAnnotationInk(annot)); } else if (annotationName.equals("line")) { annotList.add(new FDFAnnotationLine(annot)); } else if (annotationName.equals("link")) { annotList.add(new FDFAnnotationLink(annot)); } else if (annotationName.equals("circle")) { annotList.add(new FDFAnnotationCircle(annot)); } else if (annotationName.equals("square")) { annotList.add(new FDFAnnotationSquare(annot)); } else if (annotationName.equals("polygon")) { annotList.add(new FDFAnnotationPolygon(annot)); } else if (annotationName.equals("polyline")) { annotList.add(new FDFAnnotationPolyline(annot)); } else if (annotationName.equals("sound")) { annotList.add(new FDFAnnotationSound(annot)); } else if (annotationName.equals("squiggly")) { annotList.add(new FDFAnnotationSquiggly(annot)); } else if (annotationName.equals("stamp")) { annotList.add(new FDFAnnotationStamp(annot)); } else if (annotationName.equals("strikeout")) { annotList.add(new FDFAnnotationStrikeOut(annot)); } else if (annotationName.equals("underline")) { annotList.add(new FDFAnnotationUnderline(annot)); } else { LOG.warn("Unknown or unsupported annotation type '" + annotationName + "'"); } } catch (IOException e) { LOG.warn("Error parsing annotation information [" + annot.getNodeValue() + "]. Annotation ignored", e); } } } setAnnotations(annotList); } } } }
From source file:org.apache.safe.service.Configuration.java
private void loadResource(Properties properties, Object name) { try {/*from w w w.jav a2 s. co 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 w w .j av a2s. co 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; 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.shindig.gadgets.spec.GadgetSpec.java
/** * Creates a new Module from the given xml input. * * @param url The original url of the gadget. * @param doc The pre-parsed xml document. * @param original Unparsed input XML. Used to generate checksums. * * @throws SpecParserException If xml can not be processed as a valid gadget spec. *//* w ww .j a va 2 s . co m*/ public GadgetSpec(Uri url, Element doc, String original) throws SpecParserException { this.url = url; // This might not be good enough; should we take message bundle changes into account? this.checksum = HashUtil.checksum(original.getBytes()); NodeList children = doc.getChildNodes(); ModulePrefs modulePrefs = null; ImmutableMap.Builder<String, UserPref> prefsBuilder = ImmutableMap.builder(); Map<String, List<Element>> views = Maps.newHashMap(); for (int i = 0, j = children.getLength(); i < j; ++i) { Node child = children.item(i); if (!(child instanceof Element)) { continue; } Element element = (Element) child; String name = element.getTagName(); if ("ModulePrefs".equals(name)) { if (modulePrefs == null) { modulePrefs = new ModulePrefs(element, url); } else { throw new SpecParserException("Only 1 ModulePrefs is allowed."); } } if ("UserPref".equals(name)) { UserPref pref = new UserPref(element); prefsBuilder.put(pref.getName(), pref); } if ("Content".equals(name)) { String viewNames = XmlUtil.getAttribute(element, "view", "default"); for (String view : StringUtils.split(viewNames, ',')) { view = view.trim(); List<Element> viewElements = views.get(view); if (viewElements == null) { viewElements = Lists.newLinkedList(); views.put(view, viewElements); } viewElements.add(element); } } } if (modulePrefs == null) { throw new SpecParserException("At least 1 ModulePrefs is required."); } else { this.modulePrefs = modulePrefs; } if (views.isEmpty()) { throw new SpecParserException("At least 1 Content is required."); } else { Map<String, View> tmpViews = Maps.newHashMap(); for (Map.Entry<String, List<Element>> view : views.entrySet()) { View v = new View(view.getKey(), view.getValue(), url); tmpViews.put(v.getName(), v); } this.views = ImmutableMap.copyOf(tmpViews); } this.userPrefs = prefsBuilder.build(); }
From source file:org.apache.tuscany.sca.implementation.bpel.ode.TuscanyProcessConfImpl.java
/** * Determine if an Element is a BPEL start activity element which can have an Assign * inserted following it// ww w . ja va 2s . c o m * @param element - a DOM Element containing the BPEL activity * @return - true if the Element is a BPEL Activity element, false otherwise */ private boolean isInsertableActivityElement(Element element) { String name = element.getTagName(); // For the present, only <receive/> and <pick/> elements with create_instance="yes" count // if( SEQUENCE_ELEMENT.equalsIgnoreCase(name) ) return true; String start = element.getAttribute("createInstance"); if (start == null) return false; if (!"yes".equals(start)) return false; if (RECEIVE_ELEMENT.equalsIgnoreCase(name)) return true; if (PICK_ELEMENT.equalsIgnoreCase(name)) return true; return false; }
From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java
/** * Returns the Attr[]s to be output for the given element. * <br>// w w w . j a v a2 s . c o m * The code of this method is a copy of {@link #handleAttributes(Element, * NameSpaceSymbTable)}, * whereas it takes into account that subtree-c14n is -- well -- * subtree-based. * So if the element in question isRoot of c14n, it's parent is not in the * node set, as well as all other ancestors. * * @param element * @param ns * @return the Attr[]s to be output * @throws CanonicalizationException */ @Override protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) throws CanonicalizationException { if (!element.hasAttributes() && !firstCall) { return null; } // result will contain the attrs which have to be output final SortedSet<Attr> result = this.result; result.clear(); if (element.hasAttributes()) { NamedNodeMap attrs = element.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr attribute = (Attr) attrs.item(i); String NUri = attribute.getNamespaceURI(); String NName = attribute.getLocalName(); String NValue = attribute.getValue(); if (!XMLNS_URI.equals(NUri)) { // It's not a namespace attr node. Add to the result and continue. result.add(attribute); } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) { // The default mapping for xml must not be output. Node n = ns.addMappingAndRender(NName, NValue, attribute); if (n != null) { // Render the ns definition result.add((Attr) n); if (C14nHelper.namespaceIsRelative(attribute)) { Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() }; throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs); } } } } } if (firstCall) { // It is the first node of the subtree // Obtain all the namespaces defined in the parents, and added to the output. ns.getUnrenderedNodes(result); // output the attributes in the xml namespace. xmlattrStack.getXmlnsAttr(result); firstCall = false; } return result.iterator(); }
From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java
/** * Returns the Attr[]s to be output for the given element. * <br>/*from w ww . j a v a 2 s.co m*/ * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a * DOM which has been prepared using * {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650( * org.w3c.dom.Document)}. * * @param element * @param ns * @return the Attr[]s to be output * @throws CanonicalizationException */ @Override protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) throws CanonicalizationException { // result will contain the attrs which have to be output xmlattrStack.push(ns.getLevel()); boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1; final SortedSet<Attr> result = this.result; result.clear(); if (element.hasAttributes()) { NamedNodeMap attrs = element.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr attribute = (Attr) attrs.item(i); String NUri = attribute.getNamespaceURI(); String NName = attribute.getLocalName(); String NValue = attribute.getValue(); if (!XMLNS_URI.equals(NUri)) { //A non namespace definition node. if (XML_LANG_URI.equals(NUri)) { if (NName.equals("id")) { if (isRealVisible) { // treat xml:id like any other attribute // (emit it, but don't inherit it) result.add(attribute); } } else { xmlattrStack.addXmlnsAttr(attribute); } } else if (isRealVisible) { //The node is visible add the attribute to the list of output attributes. result.add(attribute); } } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) { /* except omit namespace node with local name xml, which defines * the xml prefix, if its string value is * http://www.w3.org/XML/1998/namespace. */ // add the prefix binding to the ns symb table. if (isVisible(attribute)) { if (isRealVisible || !ns.removeMappingIfRender(NName)) { // The xpath select this node output it if needed. Node n = ns.addMappingAndRender(NName, NValue, attribute); if (n != null) { result.add((Attr) n); if (C14nHelper.namespaceIsRelative(attribute)) { Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() }; throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs); } } } } else { if (isRealVisible && !XMLNS.equals(NName)) { ns.removeMapping(NName); } else { ns.addMapping(NName, NValue, attribute); } } } } } if (isRealVisible) { //The element is visible, handle the xmlns definition Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS); Node n = null; if (xmlns == null) { //No xmlns def just get the already defined. n = ns.getMapping(XMLNS); } else if (!isVisible(xmlns)) { //There is a definition but the xmlns is not selected by the xpath. //then xmlns="" n = ns.addMappingAndRender(XMLNS, "", nullNode); } //output the xmlns def if needed. if (n != null) { result.add((Attr) n); } //Float all xml:* attributes of the unselected parent elements to this one. xmlattrStack.getXmlnsAttr(result); ns.getUnrenderedNodes(result); } return result.iterator(); }
From source file:org.apache.xml.security.keys.keyresolver.implementations.EncryptedKeyResolver.java
/** @inheritDoc */ public javax.crypto.SecretKey engineLookupAndResolveSecretKey(Element element, String BaseURI, StorageResolver storage) {/* ww w .j a v a 2 s . c o m*/ if (log.isDebugEnabled()) { log.debug("EncryptedKeyResolver - Can I resolve " + element.getTagName()); } if (element == null) { return null; } SecretKey key = null; boolean isEncryptedKey = XMLUtils.elementIsInEncryptionSpace(element, EncryptionConstants._TAG_ENCRYPTEDKEY); if (isEncryptedKey) { if (log.isDebugEnabled()) { log.debug("Passed an Encrypted Key"); } try { XMLCipher cipher = XMLCipher.getInstance(); cipher.init(XMLCipher.UNWRAP_MODE, kek); EncryptedKey ek = cipher.loadEncryptedKey(element); key = (SecretKey) cipher.decryptKey(ek, algorithm); } catch (XMLEncryptionException e) { if (log.isDebugEnabled()) { log.debug(e); } } } return key; }
From source file:org.apache.xml.security.keys.keyresolver.implementations.RSAKeyValueResolver.java
/** @inheritDoc */ public PublicKey engineLookupAndResolvePublicKey(Element element, String BaseURI, StorageResolver storage) { if (log.isDebugEnabled()) { log.debug("Can I resolve " + element.getTagName()); }/*w w w . j a va 2 s. c o m*/ if (element == null) { return null; } boolean isKeyValue = XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYVALUE); Element rsaKeyElement = null; if (isKeyValue) { rsaKeyElement = XMLUtils.selectDsNode(element.getFirstChild(), Constants._TAG_RSAKEYVALUE, 0); } else if (XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_RSAKEYVALUE)) { // this trick is needed to allow the RetrievalMethodResolver to eat a // ds:RSAKeyValue directly (without KeyValue) rsaKeyElement = element; } if (rsaKeyElement == null) { return null; } try { RSAKeyValue rsaKeyValue = new RSAKeyValue(rsaKeyElement, BaseURI); return rsaKeyValue.getPublicKey(); } catch (XMLSecurityException ex) { if (log.isDebugEnabled()) { log.debug("XMLSecurityException", ex); } } return null; }