public static void putAt(Element self, String property, Object value) { if (property.startsWith("@")) { String attributeName = property.substring(1); Document doc = self.getOwnerDocument(); Attr newAttr = doc.createAttribute(attributeName); newAttr.setValue(value.toString()); self.setAttributeNode(newAttr); return; } InvokerHelper.setProperty(self, property, value); }
public Node getAppletAppNode() { Node node = doc.getDocumentElement(); if (node == null) { Element root = doc.createElement(XmlTagNames.XML_APPLET_APP_TAG); Attr attr = doc.createAttribute(XmlTagNames.XML_VERSION_ATTR); attr.setNodeValue(XmlTagNames.XML_APPLET_APP_VERSION); doc.appendChild(root); return root; } else if (node.getNodeName().equals(XmlTagNames.XML_APPLET_APP_TAG)) { return node; } return null; }
/** * Method responsible for creating Document type response * * @param responseObject * @return Document or Null if exception occurs */ protected Document createResponseObject(Map<String, String> responseObject) throws ResponseException { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("snmp-response-message"); doc.appendChild(rootElement); Element status = doc.createElement("status"); rootElement.appendChild(status); Element result = doc.createElement("result"); result.appendChild(doc.createTextNode(String.valueOf(resultCode))); status.appendChild(result); Element description = doc.createElement("description"); description.appendChild(doc.createTextNode(descriptionText)); status.appendChild(description); Element operation = doc.createElement("weatherProbe"); rootElement.appendChild(operation); Attr attr = doc.createAttribute("ip"); attr.setValue(att.get("IP-ADDRESS")); operation.setAttributeNode(attr); if (resultCode != 0) { return doc; } for (Map.Entry<String, String> entry : responseObject.entrySet()) { Element nextElement = doc.createElement(entry.getKey()); nextElement.appendChild(doc.createTextNode(entry.getValue())); operation.appendChild(nextElement); } return doc; } catch (ParserConfigurationException e) { LOG.error(e.toString()); throw new ResponseException( "<snmp-response-message><status><result>2</result><description>" + e.toString() + "</description></status><weatherProbe ip=\"" + att.get("IP-ADDRESS") + "\"/></snmp-response-message>"); } }
/** * Sets an attribute of a node. * * @param doc The document the node comes from. * @param node The node where to set the attribute. * @param attribName The name of the attribute to set. * @param attribValue The value of the attribute to set. */ public static void setAttribute(Document doc, Node node, String attribName, String attribValue) { Attr attr = doc.createAttribute(attribName); attr.setNodeValue(attribValue); node.getAttributes().setNamedItem(attr); }
/** * @param list * @param document * @param parent */ void addAttributesFromListToNode(AttributeList list, Document document, Node parent) { DicomDictionary dictionary = list.getDictionary(); Iterator i = list.values().iterator(); while (i.hasNext()) { Attribute attribute = (Attribute) i.next(); AttributeTag tag = attribute.getTag(); String elementName = dictionary.getNameFromTag(tag); if (elementName == null) { elementName = makeElementNameFromHexadecimalGroupElementValues(tag); } Node node = document.createElement(elementName); parent.appendChild(node); { Attr attr = document.createAttribute("group"); attr.setValue(HexDump.shortToPaddedHexString(tag.getGroup())); node.getAttributes().setNamedItem(attr); } { Attr attr = document.createAttribute("element"); attr.setValue(HexDump.shortToPaddedHexString(tag.getElement())); node.getAttributes().setNamedItem(attr); } { Attr attr = document.createAttribute("vr"); attr.setValue(ValueRepresentation.getAsString(attribute.getVR())); node.getAttributes().setNamedItem(attr); } if (attribute instanceof SequenceAttribute) { int count = 0; Iterator si = ((SequenceAttribute) attribute).iterator(); while (si.hasNext()) { SequenceItem item = (SequenceItem) si.next(); Node itemNode = document.createElement("Item"); Attr numberAttr = document.createAttribute("number"); numberAttr.setValue(Integer.toString(++count)); itemNode.getAttributes().setNamedItem(numberAttr); node.appendChild(itemNode); addAttributesFromListToNode(item.getAttributeList(), document, itemNode); } } else { // Attr attr = document.createAttribute("value"); // attr.setValue(attribute.getDelimitedStringValuesOrEmptyString()); // node.getAttributes().setNamedItem(attr); // node.appendChild(document.createTextNode(attribute.getDelimitedStringValuesOrEmptyString())); String values[] = null; try { values = attribute.getStringValues(); } catch (DicomException e) { // e.printStackTrace(System.err); } if (values != null) { for (int j = 0; j < values.length; ++j) { Node valueNode = document.createElement("value"); Attr numberAttr = document.createAttribute("number"); numberAttr.setValue(Integer.toString(j + 1)); valueNode.getAttributes().setNamedItem(numberAttr); valueNode.appendChild(document.createTextNode(values[j])); node.appendChild(valueNode); } } } } }
/** * @return <code>Document</code> based on <code>documentURI</code>. * @throws Exception if an error occurs during the process of building a <code>Document</code> */ private Document getDocument() throws Exception { Document returnDoc; DocumentBuilder db = getNonValidatingBuilder(); URL documentURL = documentURI.toURL(); InputSource is = new InputSource(getInputStream(documentURL)); is.setSystemId(documentURI.toURL().toExternalForm()); Document doc = null; try { doc = db.parse(is); } catch (SAXParseException spe) { // [mojarra-1693] // Test if this is a zero length or whitespace only faces-config.xml file. // If so, just make an empty Document InputStream stream = is.getByteStream(); stream.close(); is = new InputSource(getInputStream(documentURL)); stream = is.getByteStream(); if (streamIsZeroLengthOrEmpty(stream) && documentURL.toExternalForm().endsWith("faces-config.xml")) { ClassLoader loader = this.getClass().getClassLoader(); is = new InputSource(getInputStream(loader.getResource(EMPTY_FACES_CONFIG))); doc = db.parse(is); } } String documentNS = doc.getDocumentElement().getNamespaceURI(); if (validating && documentNS != null) { DOMSource domSource = new DOMSource(doc, documentURL.toExternalForm()); /* * If the Document in question is 1.2 (i.e. it has a namespace matching * JAVAEE_SCHEMA_DEFAULT_NS, then perform validation using the cached schema * and return. Otherwise we assume a 1.0 or 1.1 faces-config in which case * we need to transform it to reference a special 1.1 schema before validating. */ Node documentElement = ((Document) domSource.getNode()).getDocumentElement(); if (JAVAEE_SCHEMA_DEFAULT_NS.equals(documentNS)) { Attr version = (Attr) documentElement.getAttributes().getNamedItem("version"); DbfFactory.FacesSchema schema; if (version != null) { String versionStr = version.getValue(); if ("2.0".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { schema = DbfFactory.FacesSchema.FACES_20; } } else if ("2.1".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { schema = DbfFactory.FacesSchema.FACES_21; } } else if ("2.2".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_22; } else { schema = DbfFactory.FacesSchema.FACES_21; } } else if ("1.2".equals(versionStr)) { schema = DbfFactory.FacesSchema.FACES_12; } else { throw new ConfigurationException("Unknown Schema version: " + versionStr); } DocumentBuilder builder = getBuilderForSchema(schema); if (builder.isValidating()) { builder.getSchema().newValidator().validate(domSource); returnDoc = ((Document) domSource.getNode()); } else { returnDoc = ((Document) domSource.getNode()); } } else { // this shouldn't happen, but... throw new ConfigurationException("No document version available."); } } else { DOMResult domResult = new DOMResult(); Transformer transformer = getTransformer(documentNS); transformer.transform(domSource, domResult); // copy the source document URI to the transformed result // so that processes that need to build URLs relative to the // document will work as expected. ((Document) domResult.getNode()) .setDocumentURI(((Document) domSource.getNode()).getDocumentURI()); DbfFactory.FacesSchema schemaToApply; if (FACES_CONFIG_1_X_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACES_11; } else if (FACELETS_1_0_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { throw new IllegalStateException(); } DocumentBuilder builder = getBuilderForSchema(schemaToApply); if (builder.isValidating()) { builder.getSchema().newValidator().validate(new DOMSource(domResult.getNode())); returnDoc = (Document) domResult.getNode(); } else { returnDoc = (Document) domResult.getNode(); } } } else { returnDoc = doc; } // mark this document as the parsed representation of the // WEB-INF/faces-config.xml. This is used later in the configuration // processing. if (documentURL.toExternalForm().contains("/WEB-INF/faces-config.xml")) { Attr webInf = returnDoc.createAttribute(WEB_INF_MARKER); webInf.setValue("true"); returnDoc.getDocumentElement().getAttributes().setNamedItem(webInf); } return returnDoc; }