View Javadoc

1   package org.opensync.importexport;
2   
3   import java.io.CharArrayWriter;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   import java.io.StringReader;
8   import java.util.Hashtable;
9   import java.util.Vector;
10  
11  import javax.xml.parsers.ParserConfigurationException;
12  import javax.xml.parsers.SAXParser;
13  import javax.xml.parsers.SAXParserFactory;
14  
15  import org.opensync.tools.Constants;
16  import org.opensync.tools.DbFile;
17  import org.opensync.tools.Element;
18  import org.opensync.tools.Theme;
19  import org.opensync.tools.ThemesReference;
20  import org.opensync.tools.Utils;
21  import org.xml.sax.Attributes;
22  import org.xml.sax.InputSource;
23  import org.xml.sax.SAXException;
24  import org.xml.sax.SAXParseException;
25  import org.xml.sax.XMLReader;
26  import org.xml.sax.helpers.DefaultHandler;
27  
28  public final class ViewXml extends DefaultHandler {
29  
30  
31    private DbFile dbfile;
32    private Hashtable currentThemes,columnsIndex;
33    private ThemesReference themesref;
34    private Theme currentRefTheme;
35    private View currentTheme;
36    private Column currentColumn;
37    private Vector currentColumns;
38    private String keyWord;
39  
40  //  private Vector Columns;
41    private CharArrayWriter contents;
42    private String invalidColumn,ch2,ch3;
43  
44    /***
45     * Constructor
46     */
47    public ViewXml (String xmlFileName, DbFile dbf,ThemesReference themes)
48    throws IOException,SAXException,ParserConfigurationException{
49  
50      themesref = themes;
51      dbfile = dbf;
52      currentColumns = new Vector();
53      currentThemes = new Hashtable();
54      columnsIndex = new Hashtable();
55      this.processThemeXml(xmlFileName);
56  
57    }
58  
59    // Methode get
60  
61    public View getView(String s) {
62      return (View)currentThemes.get(s);
63    }
64    public String getKeyWordOfView() {
65      return keyWord;
66    }
67  
68    private void processThemeXml(String input)
69    throws SAXException,FileNotFoundException,ParserConfigurationException,IOException{
70      contents = new CharArrayWriter();
71  
72  //    boolean isFile = new File(input).exists();
73  
74      /* JAXP */
75      // If a validating parsing is needed, then Piccolo will use the following property to create
76      // a validating parser: here, we specify Xerces.
77      //System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
78      SAXParserFactory factory = SAXParserFactory.newInstance();
79  
80      if (new File(input).exists()) {
81        factory.setNamespaceAware(false);
82        factory.setValidating(true);
83        SAXParser parser = factory.newSAXParser();
84        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
85        xmlReader.setContentHandler(this);
86        org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
87        entityResolver.setURI(new File(input).getParentFile().toURL().toExternalForm());
88        xmlReader.setEntityResolver(entityResolver);
89        // installing error handler
90        xmlReader.setErrorHandler(this);
91        if (Utils.debug) System.out.println("ViewXml::processThemeXml - starting parser");
92        xmlReader.parse( new InputSource(new File(input).toURL().toExternalForm()));
93      } else {
94        try{
95          new java.net.URL(input);
96          factory.setNamespaceAware(false);
97          factory.setValidating(true);
98          SAXParser parser = factory.newSAXParser();
99          XMLReader xmlReader = factory.newSAXParser().getXMLReader();
100         xmlReader.setContentHandler(this);
101         org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
102         entityResolver.setURI(input.substring(0,input.lastIndexOf("/")+1));
103         xmlReader.setEntityResolver(entityResolver);
104         // installing error handler
105         xmlReader.setErrorHandler(this);
106         if (Utils.debug) System.out.println("ViewXml::processThemeXml - starting parser");
107         xmlReader.parse( new InputSource(input));
108 
109       }catch(java.net.MalformedURLException e){
110         factory.setValidating(false);
111         SAXParser parser = factory.newSAXParser();
112         if (Utils.debug) System.out.println("ViewXml::processThemeXml - starting parser");
113         parser.parse( new InputSource(new StringReader(input)), this);
114       }
115     }
116   }
117 
118   public void error(SAXParseException exception) throws SAXException {
119     String sText = "\r\nError at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage();
120     if (Utils.debug) System.out.println("ViewXml::error - "+sText);
121     throw new SAXException(sText);
122   }
123 
124   public void fatalError(SAXParseException exception) throws SAXException {
125     String sText = "\r\nFatal error at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage();
126     if (Utils.debug) System.out.println("ViewXml::fatalError - "+sText);
127     throw new SAXException(sText);
128   }
129 
130   public void warning(SAXParseException exception) throws SAXException {
131     String sText = "\r\nWarning at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage();
132     if (Utils.debug) System.out.println("ViewXml::warning - "+sText);
133     throw new SAXException(sText);
134   }
135 
136   public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
137     if (Utils.debug) System.out.println("ViewXml::startElement - namespaceURI="+namespaceURI+" localName="+localName+" qName="+qName);
138     contents.reset();
139     Element currentRefColumn;
140 
141     if (qName.equalsIgnoreCase("query")) {
142           keyWord = attr.getValue("KeyWord");
143           currentRefTheme = themesref.getTheme(attr.getValue("KeyWord"));
144        if (currentRefTheme==null) throw new SAXException("The THEME ["+keyWord+"] was not found, please update THEMES.XML");
145           currentTheme = new View(currentRefTheme,dbfile);
146           currentTheme.setInMainMenu("FALSE");
147           currentTheme.setMode(attr.getValue("Mode"));
148           //System.out.println("Start View ..... :"+attr.getValue("KeyWord")+" currentRefTheme : "+currentRefTheme);
149       }
150 
151       //COLUMN
152       if (qName.equals("COLUMN")) {
153          invalidColumn = "";
154          ch2 = attr.getValue("KeyWord");
155          if (currentRefTheme == null){
156            String sText = "ViewXml::startElement - currentRefTheme is null";
157            if (Utils.debug) System.out.println("ViewXml::error - "+sText);
158            throw new SAXException(sText);
159          }
160          currentRefColumn = currentRefTheme.getThemeElementByKey(ch2);
161          //System.out.println("Start COLUMN in THEME.XML..... :"+ch2);
162          if (currentRefColumn==null) {
163             invalidColumn = ch2;
164             throw new SAXException("Class ViewXml: Start COLUMN NOT FOUND IN THEME.XML :"+attr.getValue("KeyWord"));
165             }
166          else {
167             currentColumn = new Column(currentRefColumn);
168             currentColumn.setKeyWord(ch2);
169             currentColumn.setVisibleList("TRUE");
170             currentColumn.setVisibleSearch("FALSE");
171             //currentColumn.setWidthField(new String(new Integer(widthField).toString()));
172             //System.out.println("Start COLUMN FOUND IN Query.XML :"+ch2);
173             }
174       }
175       // Where clause
176       if (qName.equals("SearchedColumn")) {
177          invalidColumn = "";
178          ch2 = attr.getValue("KeyWord");
179          currentRefColumn = currentRefTheme.getThemeElementByKey(ch2);
180          //System.out.println("Start COLUMN in THEME.XML..... :"+ch2);
181          if (currentRefColumn==null) {
182             invalidColumn = ch2;
183             throw new SAXException("Start COLUMN NOT FOUND IN THEME.XML :"+attr.getValue("KeyWord"));
184          }else{
185             if (columnsIndex!=null && !columnsIndex.containsKey(ch2)){
186               currentColumn = new Column(currentRefColumn);
187             }else{
188               currentColumn = (Column)columnsIndex.get(ch2);
189             }
190             currentColumn.setKeyWord(ch2);
191             currentColumn.setVisibleSearch("TRUE");
192             // Allow for the same keyword in the where clause more than once.
193             String sSearchValue = attr.getValue("Value");
194             if (sSearchValue == null
195             ||  sSearchValue.trim().equals("")){
196               sSearchValue = Constants.EMPTY_ELEMENT;
197             }
198             if (currentColumn.getOperateur() != null
199             && !currentColumn.getOperateur().equals("")){
200               currentColumn.setOperateur(currentColumn.getOperateur()+"`"+attr.getValue("Operator"));
201               currentColumn.setSearchValue(currentColumn.getSearchValue()+"`"+sSearchValue);
202             }else{
203               currentColumn.setOperateur(attr.getValue("Operator"));
204               currentColumn.setSearchValue(sSearchValue);
205             }
206             currentColumn.setPrefix(attr.getValue("Prefix"));
207             currentColumn.setSuffix(attr.getValue("Suffix"));
208          }
209       }
210       // Order By
211       if (qName.equals("OrderByColumn")) {
212          if (Utils.debug) System.out.println("ViewXml::startElement - found OrderByColumn");
213          invalidColumn = "";
214          ch2 = attr.getValue("KeyWord");
215          currentRefColumn = currentRefTheme.getThemeElementByKey(ch2);
216          //System.out.println("Start COLUMN in THEME.XML..... :"+ch2);
217          if (currentRefColumn==null) {
218             invalidColumn = ch2;
219             throw new SAXException("Start COLUMN NOT FOUND IN THEME.XML :"+attr.getValue("KeyWord"));
220             }
221          else {
222             if (columnsIndex!=null && !columnsIndex.containsKey(ch2)) {
223               invalidColumn = ch2;
224               if (Utils.debug) System.out.println("ViewXml::startElement - COLUMN NOT FOUND IN XML :"+invalidColumn);
225               //System.out.println("COLUMN NOT FOUND IN XML :"+invalidColumn);
226             }
227             else currentColumn = (Column)columnsIndex.get(ch2);
228             currentColumn.setOrderBy(attr.getValue("Order"));
229             if (Utils.debug) System.out.println("ViewXml::startElement - set order by to "+attr.getValue("Order"));
230             }
231       }
232     }
233 
234    public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
235      if (Utils.debug) System.out.println("ViewXml::endElement - namespaceURI="+namespaceURI+" localName"+localName+" qName="+qName);
236             // Add into Columns(vector v) the attributs of ColumnSophieWeb
237             if (qName.equals("COLUMN")) {
238               currentColumns.addElement(currentColumn);
239               columnsIndex.put(ch2,currentColumn);
240               }
241             if (qName.equals("SearchedColumn")){
242               if (columnsIndex!=null && !columnsIndex.containsKey(ch2)){
243                   currentColumns.addElement(currentColumn);
244                   columnsIndex.put(ch2,currentColumn);
245               }
246             }
247 
248             if (qName.equalsIgnoreCase("query")) {
249               currentTheme.setElements(currentColumns);
250               //System.out.println("currentTheme.getKeyWord() and currentTheme :"+currentTheme.getKeyWord()+" "+currentTheme);
251               currentThemes.put(currentTheme.getKeyWord(),currentTheme);
252               //System.out.println("End View....parsing :");
253               }
254    }
255 
256    public void characters( char[] ch, int start, int length ) throws SAXException {
257     contents.write( ch, start, length );
258    }
259 
260 }