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;
027import java.io.File;
028import java.io.FileNotFoundException;
029import java.util.Iterator;
030import java.util.LinkedList;
031import java.util.List;
032
033import javax.xml.transform.Source;
034import javax.xml.transform.TransformerFactory;
035
036
037import org.vishia.mainCmd.MainCmd;
038import org.vishia.mainCmd.MainCmdLoggingStream;
039import org.vishia.mainCmd.Report;
040import org.vishia.xmlSimple.XmlException;
041
042import net.sf.saxon.Configuration;
043import net.sf.saxon.TransformerFactoryImpl;
044import net.sf.saxon.jdom.DocumentWrapper;
045
046
047/**This class helps to read XML documents. 
048 * The separability to other readers is, more as one XML input file may be read,
049 * all input files are joined together in one Document, with an extra root element,
050 * named 'root'. Therefore the letter 'M' (multiple) is included in the name of the class. 
051 * <br>
052 * Inside, partly JDOM is used {@linkplain http:\\www.jdom.org}, but most of the
053 * inner working should be based on SAXON {@linkplain http:www.saxonica.com}.
054 */
055public class XmlMReaderJdomSaxon extends XmlMReader
056{
057
058  XmlMReaderJdomSaxon(Report console)
059  { super(console);
060  }
061  
062  
063  public XmlMReaderJdomSaxon()
064  {
065    console = MainCmd.getLogging_ifc();
066    if(console == null){
067      console = new MainCmdLoggingStream(System.out, 3);
068    }
069  }
070  
071  
072
073  
074  private org.jdom.Element readXmlFileJdom(File fileIn, int mode)
075  throws FileNotFoundException, XmlException
076  {
077    org.jdom.Element xmlInput = null;
078    if( (mode & mReplaceWhiteSpaceWith1Space ) != 0)
079    { xmlInput = XmlExtensions.readXmlFileTrimWhiteSpace(fileIn);
080    }
081    else
082    { xmlInput = XmlExtensions.readXmlFile(fileIn);
083    }
084    return xmlInput;
085  }
086  
087  
088  
089  
090  /**Reads the input of all files into a Jdom Element named "root".
091   * 
092   * @param xmlRoot The element &lt;root> with content of all input files.
093   * @return the nr of readed files if all files are readable, or the negativ nr of readed files, if at least one input failes.
094   */
095  public int readInputsToJdomElement(org.jdom.Element xmlRoot)
096  { boolean bOk = true;
097    int nReadedFiles = 0;
098    
099    Iterator<FileTypeIn> iterFileIn = listFileIn.iterator();
100    while(iterFileIn.hasNext())
101    { //add one xml-tree from the given input file to /root
102      FileTypeIn input = iterFileIn.next();
103      String sFileIn = input.sName;
104      console.writeInfoln("reading " + sFileIn);
105      try
106      { org.jdom.Element xmlInput = readXmlFileJdom(input.getFile(), input.getMode());
107        xmlInput.detach();
108        xmlRoot.addContent(xmlInput);
109        nReadedFiles += 1;
110        console.writeInfo(" ...done.");
111      }
112      catch(Exception exception)
113      { console.writeError("Problem reading " + input.getFile().getAbsolutePath());
114        bOk = false;
115      }
116    }
117    return bOk ? nReadedFiles : -nReadedFiles; 
118  }
119  
120
121  //org.w3c.dom.
122  //public net.sf.saxon.om.DocumentInfo readInputsViaJdom(TransformerFactory tfactory)
123  @Override public Source readInputs(TransformerFactory tfactory)
124  { 
125    org.jdom.Document docJdom = new org.jdom.Document();
126    org.jdom.Element xmlRoot = new org.jdom.Element("root");
127    readInputsToJdomElement(xmlRoot);
128    docJdom.setRootElement(xmlRoot);
129
130    String sFirstInputFile = listFileIn.get(0).getFile().getAbsolutePath();
131    
132    Configuration config = ((TransformerFactoryImpl)tfactory).getConfiguration();
133    
134    net.sf.saxon.jdom.DocumentWrapper docWrapper 
135    = new DocumentWrapper(docJdom, sFirstInputFile, config);
136    return docWrapper;
137  }
138  /*
139  net.sf.saxon.om.DocumentInfo readInputsViaAelfred()
140  {
141    DocumentBuilderFactory dfactory =
142        DocumentBuilderFactory.newInstance();
143    DocumentBuilder docBuilder = null;
144    try
145    {
146      docBuilder = dfactory.newDocumentBuilder();
147    }
148    catch (ParserConfigurationException e)
149    {
150      // TODO Auto-generated catch block
151      e.printStackTrace();
152      return null;
153    }
154    Document docRoot = docBuilder.newDocument();
155    
156    SAXParserFactory parserFactory = net.sf.saxon.aelfred.SAXParserFactoryImpl.newInstance();
157    SAXParser parser = null;
158    try
159    {
160      parser = parserFactory.newSAXParser();
161    }
162    catch (ParserConfigurationException e)
163    {
164      // TODO Auto-generated catch block
165      e.printStackTrace(); return null;
166    }
167    catch (SAXException e)
168    {
169      // TODO Auto-generated catch block
170      e.printStackTrace();
171    }
172    org.xml.sax.helpers.DefaultHandler parserHandler = new net.sf.saxon.aelfred.DefaultHandler();
173    
174    TinyBuilder tree = new TinyBuilder();
175    
176    
177    Iterator iterIn = null; //sFileIn.iterator();
178    while(iterIn.hasNext())
179    { //FileIn fileIn = (FileIn) iterIn.next();
180      try
181      {
182        parser.parse(fileIn.getFile(), parserHandler); //tree); //parserHandler);
183      }
184      catch (SAXException e)
185      {
186        // TODO Auto-generated catch block
187        e.printStackTrace();
188        return null;
189      }
190      catch (IOException e)
191      {
192        // TODO Auto-generated catch block
193        e.printStackTrace(); return null;
194      }
195      Document inputDocument;
196      try
197      {
198        inputDocument = docBuilder.parse(new InputSource(new FileInputStream(fileIn.getFile())));
199      }
200      catch (FileNotFoundException e)
201      {
202        // TODO Auto-generated catch block
203        e.printStackTrace(); return null;
204      }
205      catch (SAXException e)
206      {
207        // TODO Auto-generated catch block
208        e.printStackTrace(); return null;
209      }
210      catch (IOException e)
211      {
212        // TODO Auto-generated catch block
213        e.printStackTrace(); return null;
214      }
215      outputSimple(inputDocument, new File("testi1.out"));
216      Node inputNode = inputDocument.getDocumentElement();
217      docRoot.adoptNode(inputNode);
218    }
219    
220    return null;    
221  }
222  */
223  
224}