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.io.FileReader;
030import java.io.IOException;
031import java.util.Iterator;
032import java.util.LinkedList;
033import java.util.List;
034
035import javax.xml.stream.XMLInputFactory;
036import javax.xml.stream.XMLStreamReader;
037import javax.xml.transform.Source;
038import javax.xml.transform.TransformerFactory;
039import javax.xml.transform.stream.StreamSource;
040
041
042import org.vishia.mainCmd.MainCmdLogging_ifc;
043import org.vishia.mainCmd.Report;
044import org.vishia.xmlSimple.XmlException;
045
046//import net.sf.saxon.Configuration;
047//import net.sf.saxon.TransformerFactoryImpl;
048//import net.sf.saxon.jdom.DocumentWrapper;
049
050
051/**This class helps to read XML documents. 
052 * The separability to other readers is, more as one XML input file may be read,
053 * all input files are joined together in one Document, with an extra root element,
054 * named 'root'. Therefore the letter 'M' (multiple) is included in the name of the class. 
055 * <br>
056 * Inside, partly JDOM is used {@linkplain http:\\www.jdom.org}, but most of the
057 * inner working should be based on SAXON {@linkplain http:www.saxonica.com}.
058 */
059public abstract class XmlMReader
060{
061
062  public static final int mReplaceWhiteSpaceWith1Space = 0x0001;
063  public static final int mExpandWikiFormat            = 0x0002;
064  protected MainCmdLogging_ifc console;
065  
066  XmlMReader(Report console)
067  { this.console = console;
068  }
069  
070  
071  public XmlMReader()
072  { this.console = null;
073  }
074  
075  public void setReport(MainCmdLogging_ifc console)
076  { this.console = console;
077  }
078  
079  
080  protected static class FileTypeIn
081  { protected final String sName;
082    /** Mode of preprocessing inputfile, see m... */
083    private final int mode; 
084
085    private final File fileIn;;
086    
087
088    public FileTypeIn(String sFileIn, int mode)
089    { sName = sFileIn;
090      fileIn = new File(sName);
091      this.mode = mode;
092    }
093    
094    public File getFile(){ return fileIn; }  
095    
096    public int getMode(){ return mode; }  
097  }
098
099  /**Cmdline-argument, set on -i option. Inputfile to to something. :TODO: its a example.*/
100  final List<FileTypeIn> listFileIn = new LinkedList<FileTypeIn>();
101
102  void addInputFile(String sFileName)
103  { addInputFile(sFileName, 0);
104  }
105  
106  /**Adds the information about a input file. This methods adds a File instance to an internal list.
107   * it doesn't read the content yet.
108   * @param sFileName
109   * @param mode Ones of mReplaceWhiteSpaceWith1Space or mExpandWikiFormat
110   */
111  public void addInputFile(String sFileName, int mode)
112  { FileTypeIn fileIn = new FileTypeIn(sFileName, mode);
113    listFileIn.add(fileIn);
114  }
115  
116
117
118  
119  public abstract Source readInputs(TransformerFactory tfactory);
120
121  
122  public Source readInputsHowDoesitWorks(TransformerFactory tfactory)
123  {
124        String sFirstInputFile = listFileIn.get(0).getFile().getAbsolutePath();
125        Source xmlSource;
126        try{ 
127        xmlSource = new StreamSource(new FileReader(sFirstInputFile));
128        } catch(Exception exc){
129      xmlSource = null;                 
130        }
131        return xmlSource;
132        
133        /*
134        XMLInputFactory inputFactory = XMLInputFactory.newFactory();
135    XMLStreamReader xmlStreamReader = null;
136    try{ 
137        xmlStreamReader = inputFactory.createXMLStreamReader(new FileReader(sFirstInputFile));
138        
139        } catch(Exception exc){
140                
141        }
142        xmlStreamReader.
143        */
144  }
145  
146  /*  
147  private void readInput() throws TransformerConfigurationException, MalformedURLException, IOException, SAXException
148  {
149    TransformerFactory tfactory = TransformerFactory.newInstance();
150
151    if (tfactory.getFeature(SAXSource.FEATURE)) {
152        XMLReader reader =
153            ((SAXTransformerFactory) tfactory)
154                .newXMLFilter(new StreamSource(new File("xx")));
155
156        reader.setContentHandler(new ExampleContentHandler());
157        reader.parse(new InputSource(new File("xx").toURL().toString()));
158    } else {
159        System.out.println("tfactory does not support SAX features!");
160    }
161    
162  }
163  */
164  
165}