List of usage examples for javax.xml.transform.dom DOMResult DOMResult
public DOMResult()
From source file:org.infoscoop.dao.model.TabLayout.java
private Collection getPanelXmlWidgets(String uid, String tabId, String panelXml, boolean isStatic) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer;/*from w w w. j a v a2 s . com*/ Collection widgetList = new ArrayList(); if (log.isDebugEnabled()) log.debug("--" + Thread.currentThread().getContextClassLoader()); InputStream xsl = Thread.currentThread().getContextClassLoader() .getResourceAsStream("widget_xml2object.xsl"); transformer = factory.newTransformer(new StreamSource(xsl)); DOMResult result = new DOMResult(); transformer.transform(new StreamSource(new StringReader("<widgets>" + panelXml + "</widgets>")), result); //Store widgets at the end of each line Map siblingMap = new HashMap(); Document widgets = (Document) result.getNode(); NodeList widgetNodeList = widgets.getElementsByTagName("widget"); for (int i = 0; i < widgetNodeList.getLength(); i++) { Element widgetEl = (Element) widgetNodeList.item(i); Widget widget = new Widget(); widget.setTabid(tabId); widget.setDeletedate(new Long(0)); widget.setWidgetid(widgetEl.getAttribute("widgetId")); widget.setUid(uid); //widget.setWidgetId(widgetEl.getAttribute("widgetId")); widget.setType(widgetEl.getAttribute("type")); String column = widgetEl.getAttribute("colnum"); if (column != null || !"".equals(column)) { try { widget.setColumn(Integer.valueOf(widgetEl.getAttribute("colnum"))); } catch (NumberFormatException e) { widget.setColumn(new Integer(0)); } } if (isStatic) { widget.setSiblingid(widgetEl.getAttribute("siblingId")); } else { String siblingId = (String) siblingMap.get(widget.getColumn()); if (siblingId != null) { widget.setSiblingid(siblingId); } siblingMap.put(widget.getColumn(), widget.getWidgetid()); } widget.setMenuid(isStatic ? "" : widget.getWidgetid().substring(2)); widget.setParentid(widgetEl.getAttribute("parentId")); widget.setTitle(widgetEl.getAttribute("title")); widget.setHref(widgetEl.getAttribute("href")); widget.setIgnoreHeader(new Boolean(widgetEl.getAttribute("ignoreHeader")).booleanValue()); widget.setNoBorder(new Boolean(widgetEl.getAttribute("noBorder")).booleanValue()); Element data = (Element) widgetEl.getElementsByTagName("data").item(0); NodeList propertyNodes = data.getElementsByTagName("property"); for (int k = 0; k < propertyNodes.getLength(); k++) { Element propEl = (Element) propertyNodes.item(k); widget.setUserPref(propEl.getAttribute("name"), getText(propEl)); } if (isStatic) { widget.setIsstatic(new Integer(1)); } else { widget.setIsstatic(new Integer(0)); } widgetList.add(widget); } return widgetList; }
From source file:org.lilyproject.runtime.tools.plugin.genclassloader.ClassloaderMojo.java
private void parseClassloaderTemplate(File file) { DOMResult domResult = null;/*from w ww. j ava 2 s .co m*/ Transformer transformer = null; try { Source xmlSource = new StreamSource(file); TransformerFactory tfFactory = TransformerFactory.newInstance(); if (tfFactory.getFeature(DOMResult.FEATURE)) { transformer = tfFactory.newTransformer(); domResult = new DOMResult(); transformer.transform(xmlSource, domResult); } } catch (TransformerException ex) { throw new RuntimeException("Error parsing file '" + file + "'."); } if (domResult != null && transformer != null) { Document docu = (Document) domResult.getNode(); Element classpath; NodeList list; String shareSelf = docu.getDocumentElement().getAttribute("share-self").trim(); if (shareSelf.length() > 0) { this.shareSelf = shareSelf; } try { classpath = (Element) docu.getElementsByTagName("classpath").item(0); list = classpath.getElementsByTagName("artifact"); } catch (NullPointerException npex) { throw new RuntimeException("Classloader template is invalid."); } NamedNodeMap map; Entry entry; String groupId; String classifier; String artifactId; Element domArtifact; for (int i = 0; i < list.getLength(); i++) { domArtifact = (Element) list.item(i); map = domArtifact.getAttributes(); groupId = extractAttVal(map, "groupId"); artifactId = extractAttVal(map, "artifactId"); classifier = extractAttVal(map, "classifier"); entry = new Entry(groupId, artifactId, classifier); entryMap.put(entry, domArtifact); } } }
From source file:org.mule.module.xml.filters.AbstractJaxpFilter.java
public Node toDOMNode(Object src) throws Exception { if (src instanceof Node) { return (Document) src; } else if (src instanceof org.dom4j.Document) { org.dom4j.Document dom4j = (org.dom4j.Document) src; DOMDocument dom = new DOMDocument(); dom.setDocument(dom4j);//from w ww . ja va 2 s .c o m return dom; } else if (src instanceof OutputHandler) { OutputHandler handler = ((OutputHandler) src); ByteArrayOutputStream output = new ByteArrayOutputStream(); handler.write(RequestContext.getEvent(), output); InputStream stream = new ByteArrayInputStream(output.toByteArray()); return getDocumentBuilderFactory().newDocumentBuilder().parse(stream); } else if (src instanceof byte[]) { ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src); return getDocumentBuilderFactory().newDocumentBuilder().parse(stream); } else if (src instanceof InputStream) { return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src); } else if (src instanceof String) { return getDocumentBuilderFactory().newDocumentBuilder() .parse(new InputSource(new StringReader((String) src))); } else if (src instanceof XMLStreamReader) { XMLStreamReader xsr = (XMLStreamReader) src; // StaxSource requires that we advance to a start element/document event if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT) { xsr.nextTag(); } return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource()); } else if (src instanceof DelayedResult) { DelayedResult result = ((DelayedResult) src); DOMResult domResult = new DOMResult(); result.write(domResult); return domResult.getNode(); } else { return (Node) xmlToDom.transform(src); } }
From source file:org.mule.module.xml.filters.SchemaValidationFilter.java
/** * Accepts the message if schema validation passes. * // w w w .j a va 2 s . co m * @param message The message. * @return Whether the message passes schema validation. */ public boolean accept(MuleMessage message) { Source source; try { source = loadSource(message); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } if (logger.isInfoEnabled()) { logger.info( "SchemaValidationFilter rejected a message because there was a problem interpreting the payload as XML.", e); } return false; } if (source == null) { if (logger.isInfoEnabled()) { logger.info("SchemaValidationFilter rejected a message because the XML source was null."); } return false; } DOMResult result = null; try { if (returnResult) { result = new DOMResult(); createValidator().validate(source, result); } else { createValidator().validate(source); } } catch (SAXException e) { if (logger.isDebugEnabled()) { logger.debug( "SchemaValidationFilter rejected a message because it apparently failed to validate against the schema.", e); } return false; } catch (IOException e) { if (logger.isInfoEnabled()) { logger.info( "SchemaValidationFilter rejected a message because there was a problem reading the XML.", e); } return false; } finally { if (result != null && result.getNode() != null) { message.setPayload(result.getNode()); } } if (logger.isDebugEnabled()) { logger.debug("SchemaValidationFilter accepted the message."); } return true; }
From source file:org.mule.module.xml.transformer.AbstractXmlTransformer.java
/** * @param desiredClass Java class representing the desired format * @return Callback interface representing the desiredClass - or null if the * return class isn't supported (or is null). *//*from w w w.j a v a 2s. com*/ protected static ResultHolder getResultHolder(Class<?> desiredClass) { if (desiredClass == null) { return null; } if (byte[].class.equals(desiredClass) || InputStream.class.isAssignableFrom(desiredClass)) { return new ResultHolder() { ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); StreamResult result = new StreamResult(resultStream); public Result getResult() { return result; } public Object getResultObject() { return resultStream.toByteArray(); } }; } else if (String.class.equals(desiredClass)) { return new ResultHolder() { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); public Result getResult() { return result; } public Object getResultObject() { return writer.getBuffer().toString(); } }; } else if (org.w3c.dom.Document.class.isAssignableFrom(desiredClass)) { return new ResultHolder() { DOMResult result = new DOMResult(); public Result getResult() { return result; } public Object getResultObject() { return result.getNode(); } }; } else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass)) { return new ResultHolder() { DocumentResult result = new DocumentResult(); public Result getResult() { return result; } public Object getResultObject() { return result; } }; } else if (org.dom4j.Document.class.isAssignableFrom(desiredClass)) { return new ResultHolder() { DocumentResult result = new DocumentResult(); public Result getResult() { return result; } public Object getResultObject() { return result.getDocument(); } }; } return null; }
From source file:org.mule.module.xml.util.XMLUtils.java
/** * Converts a payload to a {@link org.w3c.dom.Document} representation. * <p> Reproduces the behavior from {@link org.mule.module.xml.util.XMLUtils#toDocument(Object, MuleContext)} * which works converting to {@link org.dom4j.Document}. * * @param payload the payload to convert. * @return a document from the payload or null if the payload is not a valid XML document. *///w ww. ja v a 2s .com public static org.w3c.dom.Document toW3cDocument(Object payload) throws Exception { if (payload instanceof org.dom4j.Document) { DOMWriter writer = new DOMWriter(); org.w3c.dom.Document w3cDocument = writer.write((org.dom4j.Document) payload); return w3cDocument; } else if (payload instanceof org.w3c.dom.Document) { return (org.w3c.dom.Document) payload; } else if (payload instanceof org.xml.sax.InputSource) { return parseXML((InputSource) payload); } else if (payload instanceof javax.xml.transform.Source || payload instanceof javax.xml.stream.XMLStreamReader) { DOMResult result = new DOMResult(); Transformer idTransformer = getTransformer(); Source source = (payload instanceof Source) ? (Source) payload : toXmlSource(null, true, payload); idTransformer.transform(source, result); return (Document) result.getNode(); } else if (payload instanceof java.io.InputStream) { InputStreamReader input = new InputStreamReader((InputStream) payload); return parseXML(input); } else if (payload instanceof String) { Reader input = new StringReader((String) payload); return parseXML(input); } else if (payload instanceof byte[]) { // TODO Handle encoding/charset somehow Reader input = new StringReader(new String((byte[]) payload)); return parseXML(input); } else if (payload instanceof File) { Reader input = new FileReader((File) payload); return parseXML(input); } else { return null; } }
From source file:org.mule.module.xml.util.XMLUtils.java
/** * Convert our object to a Source type efficiently. *///w ww . j a va2 s . c o m public static javax.xml.transform.Source toXmlSource(javax.xml.stream.XMLInputFactory xmlInputFactory, boolean useStaxSource, Object src) throws Exception { if (src instanceof javax.xml.transform.Source) { return (Source) src; } else if (src instanceof byte[]) { ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src); return toStreamSource(xmlInputFactory, useStaxSource, stream); } else if (src instanceof InputStream) { return toStreamSource(xmlInputFactory, useStaxSource, (InputStream) src); } else if (src instanceof String) { if (useStaxSource) { return new StaxSource(xmlInputFactory.createXMLStreamReader(new StringReader((String) src))); } else { return new StreamSource(new StringReader((String) src)); } } else if (src instanceof org.dom4j.Document) { return new DocumentSource((org.dom4j.Document) src); } else if (src instanceof org.xml.sax.InputSource) { return new SAXSource((InputSource) src); } // TODO MULE-3555 else if (src instanceof XMLStreamReader) { XMLStreamReader xsr = (XMLStreamReader) src; // StaxSource requires that we advance to a start element/document event if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT) { xsr.nextTag(); } return new StaxSource((XMLStreamReader) src); } else if (src instanceof org.w3c.dom.Document || src instanceof org.w3c.dom.Element) { return new DOMSource((org.w3c.dom.Node) src); } else if (src instanceof DelayedResult) { DelayedResult result = ((DelayedResult) src); DOMResult domResult = new DOMResult(); result.write(domResult); return new DOMSource(domResult.getNode()); } else if (src instanceof OutputHandler) { OutputHandler handler = ((OutputHandler) src); ByteArrayOutputStream output = new ByteArrayOutputStream(); handler.write(RequestContext.getEvent(), output); return toStreamSource(xmlInputFactory, useStaxSource, new ByteArrayInputStream(output.toByteArray())); } else { return null; } }
From source file:org.nuxeo.ecm.core.io.impl.TestTypedExportedDocument.java
/** * Transforms a dom4j document to a w3c Document. * * @param dom4jdoc the org.dom4j.Document document * @return the org.w3c.dom.Document document * @throws TransformerException the transformer exception *///from www . jav a 2 s.c o m protected final Document dom4jToW3c(org.dom4j.Document dom4jdoc) throws TransformerException { SAXSource source = new DocumentSource(dom4jdoc); DOMResult result = new DOMResult(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(source, result); return (Document) result.getNode(); }
From source file:org.ojbc.xslt.EntityResolutionTransformerServiceTest.java
private Document transform(Document inputDocument, Document xsltDocument) throws TransformerFactoryConfigurationError, Exception { Transformer t = TransformerFactory.newInstance().newTransformer(new DOMSource(xsltDocument)); t.setParameter("erAttributeParametersFilePath", "src/main/resources/xslt/erConfig/example/VehicleSearchAttributeParameters.xml"); t.setParameter("entityResolutionRecordThreshold", "150"); DOMResult result = new DOMResult(); t.transform(new DOMSource(inputDocument), result); Document resultDocument = (Document) result.getNode(); return resultDocument; }
From source file:org.ojbc.xslt.IncidentReportingTransformerServiceTest.java
private Document transformIncidentReportToNotifyDoc(Document incidentReportDocument) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(xsltSource); DOMResult domResult = new DOMResult(); DOMSource inDocDomSource = new DOMSource(incidentReportDocument); transformer.transform(inDocDomSource, domResult); Document resultDoc = (Document) domResult.getNode(); return resultDoc; }