List of usage examples for org.w3c.dom Document setXmlVersion
public void setXmlVersion(String xmlVersion) throws DOMException;
From source file:Main.java
License:asdf
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString))); xmlDoc.setXmlVersion("2.0"); }
From source file:Main.java
/** * @param .Should be followed by createRoot() * @return Creates a new Document that contains XML * @throws ParserConfigurationException//w w w. jav a 2 s . co m * @throws SAXException * @throws IOException */ public static Document createDocument(String version, String encoding) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document parse = builder.newDocument(); parse.setXmlVersion(version); return parse; }
From source file:Main.java
public static final Document createDocument(boolean standalone, String xmlVersion) { Document result = null; try {//from w w w. j a v a2 s . c o m result = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); result.setXmlStandalone(standalone); result.setXmlVersion(xmlVersion); } catch (ParserConfigurationException e) { e.printStackTrace(); } return result; }
From source file:hu.bme.mit.sette.common.descriptors.eclipse.EclipseProjectDescriptor.java
/** * Creates the XML document for the .project file. * * @return The XML document./*from ww w .j a v a 2 s . c o m*/ * @throws ParserConfigurationException * If a DocumentBuilder cannot be created which satisfies the * configuration requested. */ public Document createXmlDocument() throws ParserConfigurationException { // create document object Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); document.setXmlVersion("1.0"); document.setXmlStandalone(true); // add root tag Element rootTag = document.createElement("projectDescription"); document.appendChild(rootTag); // add name and comment rootTag.appendChild(document.createElement("name")).setTextContent(name); rootTag.appendChild(document.createElement("comment")).setTextContent(comment); // add the projects tag rootTag.appendChild(document.createElement("projects")); // add the Java builder Element buildSpecTag = document.createElement("buildSpec"); rootTag.appendChild(buildSpecTag); Element buildCommandTag = document.createElement("buildCommand"); buildSpecTag.appendChild(buildCommandTag); buildCommandTag.appendChild(document.createElement("name")) .setTextContent(EclipseProjectDescriptor.JAVA_BUILDER); buildCommandTag.appendChild(document.createElement("arguments")); // add the Java nature Element naturesTag = document.createElement("natures"); rootTag.appendChild(naturesTag); naturesTag.appendChild(document.createElement("nature")) .setTextContent(EclipseProjectDescriptor.JAVA_NATURE); return document; }
From source file:fi.csc.kapaVirtaAS.WSDLManipulator.java
public void generateVirtaKapaWSDL() throws Exception { // Fetch current WSDL-file File inputFile = new File("opiskelijatiedot.wsdl"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.setXmlVersion("1.0"); doc.getDocumentElement().normalize(); // Manipulate WSDL to meet the requirements of xroad // Root element <wsdl:definitions> attribute manipulations Element root = doc.getDocumentElement(); root.setAttribute("xmlns:" + conf.getXroadSchemaPrefixForWSDL(), conf.getXroadSchema()); root.setAttribute("xmlns:" + conf.getXroadIdSchemaPrefixForWSDL(), conf.getXroadIdSchema()); root = replaceAttribute(root, "xmlns:tns", conf.getAdapterServiceSchema()); root = replaceAttribute(root, "targetNamespace", conf.getAdapterServiceSchema()); // Schema elements <xs:schema> attribute manipulations NodeList schemas = root.getElementsByTagName("xs:schema"); for (int i = 0; i < schemas.getLength(); ++i) { Node schema = schemas.item(i); if (schema != null) { NamedNodeMap schemaAttributes = schema.getAttributes(); if (schemaAttributes != null && schemaAttributes.getNamedItem("xmlns:virtaluku") != null) { schemaAttributes.getNamedItem("xmlns:virtaluku").setTextContent(conf.getAdapterServiceSchema()); if (schemaAttributes != null && schemaAttributes.getNamedItem("targetNamespace") != null) { schemaAttributes.getNamedItem("targetNamespace") .setTextContent(conf.getAdapterServiceSchema()); }//from ww w .ja v a 2 s . com Element el = (Element) schema.appendChild(doc.createElement("xs:import")); el.setAttribute("id", conf.getXroadSchemaPrefixForWSDL()); el.setAttribute("namespace", conf.getXroadSchema()); el.setAttribute("schemaLocation", conf.getXroadSchema()); // Remove Request part from xs:element -elements NodeList elementsInSchema = schema.getChildNodes(); for (int j = 0; j < elementsInSchema.getLength(); ++j) { Element el1 = (Element) elementsInSchema.item(j); if (el1.getNodeName() == "xs:element") { replaceAttribute(el1, "name", StringUtils.substringBefore(el1.getAttribute("name"), "Request")); } } } } } // Append xroad request headers Element xroadReqHeadersElement = doc.createElement("wsdl:message"); xroadReqHeadersElement.setAttribute("name", "requestheader"); for (String xroadHeader : conf.getXroadHeaders()) { Element reqHeader = doc.createElement("wsdl:part"); reqHeader.setAttribute("name", xroadHeader); reqHeader.setAttribute("element", conf.getXroadSchemaPrefixForWSDL() + ":" + xroadHeader); xroadReqHeadersElement.appendChild(reqHeader); } root.appendChild(xroadReqHeadersElement); NodeList childrenList = root.getChildNodes(); for (int i = 0; i < childrenList.getLength(); ++i) { if (childrenList.item(i).getNodeName().contains(":binding")) { NodeList binding = childrenList.item(i).getChildNodes(); for (int j = 0; j < binding.getLength(); ++j) { if (binding.item(j).getNodeName().contains(":operation")) { Element el1 = (Element) binding.item(j) .appendChild(doc.createElement(conf.getXroadIdSchemaPrefixForWSDL() + ":version")); el1.setTextContent(conf.getVirtaVersionForXRoad()); for (Node child = binding.item(j).getFirstChild(); child != null; child = child .getNextSibling()) { // Append xroad wsdl:binding operation headers if (child.getNodeName().contains(":input") || child.getNodeName().contains(":output")) { Element el = (Element) child; for (String xroadHeader : conf.getXroadHeaders()) { el.appendChild(soapHeader(doc.createElement("soap:header"), "tns:requestheader", xroadHeader, "literal")); } } if (child.getNodeName().contains(":input")) { Element el = (Element) child; replaceAttribute(el, "name", StringUtils.substringBefore(el.getAttribute("name"), "Request")); } } } } // Remove Request from wsdl:message > wsdl:part element so that can see element } else if (childrenList.item(i).getNodeName().contains(":message") && childrenList.item(i) .getAttributes().getNamedItem("name").getNodeValue().contains("Request")) { Element part = (Element) childrenList.item(i).getFirstChild().getNextSibling(); replaceAttribute(part, "element", StringUtils.substringBefore(part.getAttribute("element"), "Request")); } // Change wsdl input names to meet XRoad standard if (childrenList.item(i).getNodeName().contains(":portType")) { NodeList binding = childrenList.item(i).getChildNodes(); for (int j = 0; j < binding.getLength(); ++j) { if (binding.item(j).getNodeName().contains(":operation")) { for (Node child = binding.item(j).getFirstChild(); child != null; child = child .getNextSibling()) { if (child.getNodeName().contains(":input")) { Element el = (Element) child; replaceAttribute(el, "name", StringUtils.substringBefore(el.getAttribute("name"), "Request")); } } } } } // Append kapaVirtaAS service address if (childrenList.item(i).getNodeName().contains(":service")) { NodeList service = childrenList.item(i).getChildNodes(); for (int j = 0; j < service.getLength(); ++j) { if (service.item(j).getNodeName().contains(":port")) { for (Node child = service.item(j).getFirstChild(); child != null; child = child .getNextSibling()) { if (child.getNodeName().contains(":address")) { Element el = (Element) child; replaceAttribute(el, "location", conf.getAdapterServiceSOAPURL()); } } } } } } // Write manipulated WSDL to file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(conf.getAdapterServiceWSDLPath() + "/kapavirta_as.wsdl")); transformer.transform(source, result); }
From source file:hd3gtv.as5kpc.protocol.ProtocolHandler.java
private Document createDocument() { try {/*from www.j av a2s . c o m*/ DocumentBuilderFactory fq = DocumentBuilderFactory.newInstance(); DocumentBuilder constructeur = fq.newDocumentBuilder(); Document document = constructeur.newDocument(); document.setXmlVersion("1.0"); document.setXmlStandalone(true); return document; } catch (Exception e) { log.error("Can't make XML document", e); } return null; }
From source file:fi.csc.kapaVirtaAS.MessageTransformer.java
public String createAuthenticationString(String message) { try {// w ww . jav a 2s .co m DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder .parse(new InputSource(new ByteArrayInputStream(stripXmlDefinition(message).getBytes()))); doc.setXmlVersion("1.0"); doc.normalizeDocument(); Optional<NodeList> headerNodes = getChildByKeyword( nodeListToStream(doc.getDocumentElement().getChildNodes()), "header"); Optional<NodeList> clientHeaders = headerNodes .map(nodeList -> getChildByKeyword(nodeListToStream(nodeList), "client")) .orElse(Optional.empty()); Number l = clientHeaders.get().getLength(); l.toString(); return clientHeaders .map(nodeList -> getNodeByKeyword(nodeListToStream(nodeList), "xRoadInstance") .map(node -> node.getTextContent())) .orElse(Optional.empty()).orElse("") + ":" + clientHeaders.map(nodeList -> getNodeByKeyword(nodeListToStream(nodeList), "memberClass") .map(node -> node.getTextContent())).orElse(Optional.empty()).orElse("") + ":" + clientHeaders.map(nodeList -> getNodeByKeyword(nodeListToStream(nodeList), "memberCode") .map(node -> node.getTextContent())).orElse(Optional.empty()).orElse("") + ":" + clientHeaders.map(nodeList -> getNodeByKeyword(nodeListToStream(nodeList), "subsystemCode") .map(node -> node.getTextContent())).orElse(Optional.empty()).orElse(""); } catch (Exception e) { log.error("Error in parsing authenticationstring"); log.error(e.toString()); return ""; } }
From source file:fi.csc.kapaVirtaAS.MessageTransformer.java
public String transform(String message, MessageDirection direction) throws Exception { try {/*from ww w . j a va2 s. co m*/ DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder .parse(new InputSource(new ByteArrayInputStream(stripXmlDefinition(message).getBytes()))); doc.setXmlVersion("1.0"); doc.normalizeDocument(); Element root = doc.getDocumentElement(); if (direction == MessageDirection.XRoadToVirta) { // Save XRoad schema prefix for response message xroadSchemaPrefix = namedNodeMapToStream(root.getAttributes()) .filter(node -> node .getNodeValue().toLowerCase().contains(conf.getXroadSchema().toLowerCase())) .findFirst() .orElseThrow( () -> new DOMException(DOMException.NOT_FOUND_ERR, "Xroad schema prefix not found")) .getNodeName(); xroadIdSchemaPrefix = namedNodeMapToStream(root.getAttributes()) .filter(node -> node.getNodeValue().toLowerCase() .contains(conf.getXroadIdSchema().toLowerCase())) .findFirst().orElseThrow(() -> new DOMException(DOMException.NOT_FOUND_ERR, "XroadId schema prefix not found")) .getNodeName(); // Change tns schema getNodeByKeyword(namedNodeMapToStream(root.getAttributes()), conf.getAdapterServiceSchema()) .map(attribute -> setNodeValueToNode(attribute, conf.getVirtaServiceSchema())); //There should be two children under the root node: header and body for (int i = 0; i < root.getChildNodes().getLength(); ++i) { Node child = root.getChildNodes().item(i); // Save soap-headers for reply message and remove child elements under soap-headers if (child.getNodeName().toLowerCase().contains("header")) { this.xroadHeaderElement = child.cloneNode(true); root.replaceChild(child.cloneNode(false), child); } // Change SOAP-body else if (child.getNodeName().toLowerCase().contains("body")) { for (int j = 0; j < child.getChildNodes().getLength(); ++j) { if (child.getChildNodes().item(j).getNodeType() == Node.ELEMENT_NODE) { doc.renameNode(child.getChildNodes().item(j), conf.getVirtaServiceSchema(), child.getChildNodes().item(j).getNodeName() + "Request"); break; } } } } } if (direction == MessageDirection.VirtaToXRoad) { // Add XRoad schemas with saved prefix to response message root.setAttribute(xroadSchemaPrefix, conf.getXroadSchema()); root.setAttribute(xroadIdSchemaPrefix, conf.getXroadIdSchema()); // Change tns schema getNodeByKeyword(namedNodeMapToStream(root.getAttributes()), conf.getVirtaServiceSchema()) .map(attribute -> setNodeValueToNode(attribute, conf.getAdapterServiceSchema())); // Change SOAP-headers Node headerNode = getNodeByKeyword(nodeListToStream(root.getChildNodes()), "header").get(); for (int i = 0; i < this.xroadHeaderElement.getChildNodes().getLength(); ++i) { headerNode.appendChild(doc.importNode(this.xroadHeaderElement.getChildNodes().item(i), true)); } // Change SOAP-body getNodeByKeyword(nodeListToStream(root.getChildNodes()), "body") .map(bodyNode -> removeAttribureFromElement(nodeToElement(bodyNode), virtaServicePrefix)) .map(bodyNode -> setAttributeToElement(nodeToElement(bodyNode), virtaServicePrefix, conf.getAdapterServiceSchema())); //Virta gives malformed soap fault message. Need to parse it correct. getNodeByKeyword(nodeListToStream(root.getChildNodes()), "body") .map(bodyNode -> nodeListToStream(bodyNode.getChildNodes())).map( nodesInBodyStream -> getNodeByKeyword(nodesInBodyStream, "fault") .map(faultNode -> removeAttribureFromElement( nodeToElement(nodeToElement(faultNode) .getElementsByTagName("faultstring").item(0)), "xml:lang"))); } doc.normalizeDocument(); DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(domSource, result); message = writer.toString(); return stripXmlDefinition(message); } catch (Exception e) { if (direction == MessageDirection.XRoadToVirta) { log.error("Error in parsing request message."); throw e; } else { log.error("Error in parsing response message"); log.error(e.toString()); return stripXmlDefinition(faultMessageService.generateSOAPFault(message, faultMessageService.getResValidFail(), this.xroadHeaderElement)); } } }
From source file:com.connectsdk.service.DLNAService.java
protected String getMessageXml(String serviceURN, String method, String instanceId, Map<String, String> params) { try {// ww w . j av a2s. c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); doc.setXmlStandalone(true); doc.setXmlVersion("1.0"); Element root = doc.createElement("s:Envelope"); Element bodyElement = doc.createElement("s:Body"); Element methodElement = doc.createElementNS(serviceURN, "u:" + method); Element instanceElement = doc.createElement("InstanceID"); root.setAttribute("s:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/"); root.setAttribute("xmlns:s", "http://schemas.xmlsoap.org/soap/envelope/"); doc.appendChild(root); root.appendChild(bodyElement); bodyElement.appendChild(methodElement); if (instanceId != null) { instanceElement.setTextContent(instanceId); methodElement.appendChild(instanceElement); } if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); Element element = doc.createElement(key); element.setTextContent(value); methodElement.appendChild(element); } } return xmlToString(doc, true); } catch (Exception e) { return null; } }
From source file:org.apache.airavata.gfac.hadoop.handler.HadoopDeploymentHandler.java
private void clusterPropertiesToHadoopSiteXml(Properties props, File hadoopSiteXml) throws ParserConfigurationException, TransformerException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = domFactory.newDocumentBuilder(); Document hadoopSiteXmlDoc = documentBuilder.newDocument(); hadoopSiteXmlDoc.setXmlVersion("1.0"); hadoopSiteXmlDoc.setXmlStandalone(true); hadoopSiteXmlDoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\""); Element configEle = hadoopSiteXmlDoc.createElement("configuration"); hadoopSiteXmlDoc.appendChild(configEle); for (Map.Entry<Object, Object> entry : props.entrySet()) { addPropertyToConfiguration(entry, configEle, hadoopSiteXmlDoc); }// w w w .j av a 2s .c o m saveDomToFile(hadoopSiteXmlDoc, hadoopSiteXml); }