001/**************************************************************************** 002 * Copyright/Copyleft: 003 * 004 * For this source the LGPL Lesser General Public License, 005 * published by the Free Software Foundation is valid. 006 * It means: 007 * 1) You can use this source without any restriction for any desired purpose. 008 * 2) You can redistribute copies of this source to everybody. 009 * 3) Every user of this source, also the user of redistribute copies 010 * with or without payment, must accept this license for further using. 011 * 4) But the LPGL ist not appropriate for a whole software product, 012 * if this source is only a part of them. It means, the user 013 * must publish this part of source, 014 * but don't need to publish the whole source of the own product. 015 * 5) You can study and modify (improve) this source 016 * for own using or for redistribution, but you have to license the 017 * modified sources likewise under this LGPL Lesser General Public License. 018 * You mustn't delete this Copyright/Copyleft inscription in this source file. 019 * 020 * @author www.vishia.de/Java 021 * @version 2006-06-15 (year-month-day) 022 * list of changes: 023 * 2006-05-00 JcHartmut: creation 024 * 025 ****************************************************************************/ 026package org.vishia.xml; 027 028import java.io.File; 029import java.io.FileNotFoundException; 030import java.io.FileOutputStream; 031 032//import javax.xml.transform.Result; 033import javax.xml.transform.Source; 034import javax.xml.transform.Transformer; 035import javax.xml.transform.TransformerException; 036//import javax.xml.transform.TransformerFactory; 037import javax.xml.transform.stream.StreamResult; 038 039//import net.sf.saxon.Configuration; 040//import net.sf.saxon.TransformerFactoryImpl; 041//import net.sf.saxon.jdom.DocumentWrapper; 042//import net.sf.saxon.om.NodeInfo; 043//import net.sf.saxon.tinytree.TinyBuilder; 044//import net.sf.saxon.tinytree.TinyTree; 045//import net.sf.saxon.tree.TreeBuilder; 046 047 048 049/**This class helps to transform XML documents. Internally it uses the saxon transformer. 050 * The separability to saxon is, the output will not write only to a file, 051 * it may be postprocessed, therefor an alternativ output of the xml tree is used. 052 * <br> 053 * TODO: here are some tryings to output to TinyTree, Jdom and others, 054 * but there are no prosperity now. It is indefinite here now, how to evaluate 055 * and change a tree with the saxon object model. 056 * Therefore only the simple output method is useable, see comments. 057 */ 058 059public class XmlMTransformer 060{ 061 062 /**The transformer containing the XSL stylesheet. It should be an instance 063 * of SAXON. But the type is defined in javax.xml.transform. 064 */ 065 final Transformer transformer; 066 067 /**The Source for transformation.*/ 068 final Source xmlSource; 069 070 /**Creates the instance with given Transformer and given Source. 071 * The instance type of Source should be matches to the Transformer. 072 * @param source 073 * @param transformer 074 */ 075 XmlMTransformer(Source source, Transformer transformer) 076 { this.transformer = transformer; 077 this.xmlSource = source; 078 } 079 080 /**Transforms and writes the result XML directly to the given file. 081 * 082 * @param fileOut 083 * @throws FileNotFoundException 084 * @throws TransformerException 085 */ 086 void transformToFile(File fileOut) 087 throws FileNotFoundException, TransformerException 088 { 089 transformer.transform(xmlSource, new StreamResult(new FileOutputStream(fileOut))); 090 } 091 092 093 /**Transforms and writes the result in a SAXON TinyTree. 094 * This is tested, it works good. 095 * @return 096 * @throws TransformerException 097 */ 098/* 099 NodeInfo transformToTinyTree() 100 throws TransformerException 101 { 102 TinyBuilder builder = new TinyBuilder(); 103 transformer.transform(xmlSource, builder); 104 TinyTree treeOut = builder.getTree(); 105 NodeInfo xmlOut = treeOut.getNode(0); //it should be the root node (?) 106 return xmlOut; 107 } 108*/ 109 110 111 /**Transforms and writes the result in a SAXON Tree. 112 * This is not tested, how to get the root node? 113 * @return 114 * @throws TransformerException 115 */ 116/* 117 NodeInfo transformToSaxonTree() 118 throws TransformerException 119 { 120 TreeBuilder builder = new TreeBuilder(); 121 transformer.transform(xmlSource, builder); 122 //how get the output? TinyTree xmlOut = builder.getTree(); 123 return null; 124 } 125*/ 126 127 128 /**Transforms and writes the result in a SAXON Tree. 129 * This is tested, but doesn't work in this form. 130 * How to transform or convert to a jdom tree? 131 * @return 132 * @throws TransformerException 133 */ 134/* 135 org.jdom.Element transformToJdom(TransformerFactory tfactory) 136 throws TransformerException 137 { //org.jdom.transform.JDOMResult xmlResult = new org.jdom.transform.JDOMResult(); 138 Configuration config = ((TransformerFactoryImpl)tfactory).getConfiguration(); 139 140 org.jdom.Document outJdom = new org.jdom.Document(); 141 net.sf.saxon.jdom.DocumentWrapper docWrapper 142 = new DocumentWrapper(outJdom, "xx", config); 143 transformer.transform(xmlSource, (Result)docWrapper); 144 if(!outJdom.hasRootElement()) 145 { throw new TransformerException("xslTransformationXml: no root element produced"); 146 } 147 //if success than return the detached root element from conversion document. 148 //The document is further unnecessary and will be deleted by the garbage collector. 149 org.jdom.Element xmlOut = outJdom.getRootElement(); 150 xmlOut.detach(); 151 return xmlOut; 152 } 153*/ 154 155 156/* 157 void output(Document document, File fOut) 158 throws IOException, XMLStreamException 159 { XMLOutputFactory factory = new XMLOutputFactoryImpl(); 160 Writer stream = new FileWriter(fOut); 161 XMLStreamWriter writer = factory.createXMLStreamWriter(stream); 162 writer.writeStartDocument(); 163 Node node = document.getFirstChild(); 164 writer.writeEmptyElement(node.getLocalName()); 165 writer.writeEndDocument(); 166 writer.close(); 167 } 168 169 170 String outputSimple(Document document, File fOut) 171 { String sError = null; 172 try{ output(document, fOut); } 173 catch(IOException exception) 174 { sError = "IOException: " + exception.getMessage(); 175 } 176 catch(XMLStreamException exception) 177 { sError = "XMLStreamException: " + exception.getMessage(); 178 } 179 return sError; 180 } 181 182 183*/ 184}