View Javadoc

1   package org.opensync.tools;
2   
3   import java.io.CharArrayWriter;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   import java.net.URL;
8   import java.util.Enumeration;
9   import java.util.Hashtable;
10  import java.util.Vector;
11  
12  import javax.xml.parsers.ParserConfigurationException;
13  import javax.xml.parsers.SAXParserFactory;
14  
15  import org.xml.sax.Attributes;
16  import org.xml.sax.InputSource;
17  import org.xml.sax.SAXException;
18  import org.xml.sax.SAXParseException;
19  import org.xml.sax.XMLReader;
20  import org.xml.sax.helpers.DefaultHandler;
21  
22  public final class ThemesReference extends DefaultHandler {
23    //private Hashtable dicoLabels;
24    private static Hashtable themes;
25    private static Hashtable themesByIdFile;
26    private CharArrayWriter contents;
27    private Element currentElement;
28    private Theme currentTheme;
29    private Vector currentElements, currentLinks, currentGroups, currentInputs, currentPrimaryKeys, currentPrimaryKeyElements ;
30    private ElementAssociation currentAssociation;
31    private GroupFixedCriteria currentGroup;
32    private InputFixedCriteria currentInput;
33    private ElementLink currentLink;
34    private DbFile table;
35    private boolean hasPlanning;
36  
37    /***
38     * Constructor
39     */
40    public ThemesReference(/*DicoReader dico,*/DbFile t) {
41      //read TABLES.xml
42      table = t;
43      themes = new Hashtable();
44      themesByIdFile = new Hashtable();
45      //dicoLabels = dico.getDico();
46  
47      currentElements = new Vector();
48      currentGroups = new Vector();
49      currentInputs = new Vector();
50      currentLinks = new Vector();
51      currentAssociation = null;
52  
53      currentPrimaryKeys = new Vector(10);
54      currentPrimaryKeyElements = new Vector(10);
55    }
56  
57    // Methode get
58    public static Theme getTheme(String t) {
59      return ((Theme)themes.get(t));
60    }
61    public static Theme getThemeByIdFile(String idFile) {
62      return ((Theme)themesByIdFile.get(idFile));
63    }
64  
65    public static Theme getFirstTheme() {
66    Enumeration themesEnum = themes.elements();
67  
68      return (Theme)themesEnum.nextElement();
69    }
70  
71    public static Enumeration getKeys() {
72      return themes.keys();
73    }
74  
75    public void destroy() {
76      currentGroups.clear();
77      currentInputs.clear();
78      currentPrimaryKeys.clear();
79      currentPrimaryKeyElements.clear();
80      currentElements.clear();
81      currentLinks.clear();
82      currentAssociation = null;
83    }
84  
85  /*
86    public void processThemeXml(String filename) {
87  
88    contents = new CharArrayWriter();
89  
90      try {
91          XMLReader xr = XMLReaderFactory.createXMLReader();
92          xr.setContentHandler( this );
93          xr.parse( new InputSource(filename));
94        }
95          catch ( Exception e ) {
96          e.printStackTrace();
97          }
98  */
99    public void processThemeXml(String filename)
100   throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
101     contents = new CharArrayWriter();
102 
103     /*
104     XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
105     xr.setContentHandler( this );
106 
107 
108     xr.setFeature("http://xml.org/sax/features/validation", true);
109     // installing error handler
110     org.xml.sax.ErrorHandler errorHandler = this;
111     xr.setErrorHandler(errorHandler);
112     */
113 
114     /* JAXP */
115     // If a validating parsing is needed, then Piccolo will use the following property to create
116     // a validating parser: here, we specify Xerces.
117     System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
118     SAXParserFactory factory = SAXParserFactory.newInstance();
119     factory.setNamespaceAware(false);
120     factory.setValidating(true);
121     XMLReader xmlReader = factory.newSAXParser().getXMLReader();
122     xmlReader.setContentHandler(this);
123     org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
124     xmlReader.setErrorHandler(this);
125     if (new File(filename).exists()){
126       entityResolver.setURI(new File(filename).getParentFile().toURL().toExternalForm());
127       xmlReader.setEntityResolver(entityResolver);
128       xmlReader.parse(new InputSource(new File(filename).toURL().toExternalForm()));
129     }else{
130       entityResolver.setURI(filename.substring(0,filename.lastIndexOf("/")+1));
131       xmlReader.setEntityResolver(entityResolver);
132       xmlReader.parse(new InputSource(new URL(filename).toExternalForm()));
133     }
134     //xr.parse( new InputSource(new FileInputStream(filename)));
135     //PV 05-03-2002 because of weblogic 6.1 war deployement...
136     //xr.parse( new InputSource(filename));
137     //xr.parse( new InputSource(new File(filename).toURL().toExternalForm()));
138     /*
139     if (new File(filename).exists())
140       xr.parse(new InputSource(new File(filename).toURL().toExternalForm()));
141     else
142       xr.parse(new InputSource(new URL(filename).toExternalForm()));
143     */
144 
145 /*
146     SAXParserFactory factory = SAXParserFactory.newInstance();
147     factory.setValidating(false);
148     SAXParser parser = factory.newSAXParser();
149     parser.parse( new InputSource(new File(filename).toURL().toExternalForm()), this);
150 */
151 
152   }
153 
154     public void error(SAXParseException exception) throws SAXException {
155       throw new SAXException("\r\nError at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
156     }
157 
158     public void fatalError(SAXParseException exception) throws SAXException {
159           throw new SAXException("\r\nFatal error at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
160     }
161 
162     public void warning(SAXParseException exception) throws SAXException {
163           throw new SAXException("\r\nWarning at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
164     }
165 
166     public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
167     String ch1,ch2,ch3,ch4,idPar="",idChild="";
168     contents.reset();
169 
170       if (qName.equals("VIEW")) {
171           idPar = attr.getValue("IdParentFile");
172           currentTheme = new Theme(attr.getValue("KeyWord"),idPar,attr.getValue("ClassName"),attr.getValue("IdRightField"),table.getParent(idPar),attr.getValue("MenuActionView"));
173           if (Utils.debug) System.out.println("Start THEME ...."+attr.getValue("KeyWord"));
174           if (Utils.debug) System.out.println("**************************************************************************************************");
175          }
176 
177       if (qName.equals("COLUMN")) {
178           hasPlanning = false;
179           currentElement = new Element();
180           ch2 = attr.getValue("KeyWord");
181           currentElement.setKeyWord(ch2);
182           //currentElement.setLabel((String)dicoLabels.get(ch2));
183           currentElement.setLabel("");
184           currentElement.setFile(attr.getValue("TableName"));
185 
186           currentElement.setParentFile(attr.getValue("ParentFile"));
187           currentElement.setKeyParent(attr.getValue("KeyParent"));
188           ch3 = attr.getValue("IsPrimaryKey");
189           currentElement.setPrimaryKey(ch3);
190           currentElement.setType(attr.getValue("TypeField"));
191           currentElement.setLength(attr.getValue("LengthField"));
192           idChild = attr.getValue("RefThemeNumber");
193           currentElement.setRefThemeNumber(idChild);
194           currentElement.setSequenceNumber(attr.getValue("SequenceNumber"));
195           if (ch3.equals("TRUE")) {
196               currentPrimaryKeys.addElement(ch2);
197               currentPrimaryKeyElements.addElement(currentElement);
198             }
199           if (attr.getValue("TableName").equals("")) {
200               // field = field
201               currentElement.setField(attr.getValue("ColumnName"));
202 
203               if (attr.getValue("TypeField").equals("CT"))
204               {
205                 currentElement.setIsFixedCriteria(false);
206                 if(attr.getValue("KeyWordDateDeb") != null && attr.getValue("KeyWordDateFin") != null)
207                 {
208                     currentElement.setDateDebPlanning(attr.getValue("KeyWordDateDeb"));
209                     currentElement.setDateFinPlanning(attr.getValue("KeyWordDateFin"));
210                     hasPlanning = true;
211                 }
212               }
213               else currentElement.setIsFixedCriteria(true);
214 
215 
216               if (attr.getValue("TypeField").equals("AT"))
217                 currentElement.setIsAssociatedTheme(true);
218               else
219                 currentElement.setIsAssociatedTheme(false);
220               if (attr.getValue("TypeField").equals("APT"))
221                 currentElement.setIsAssociatedParentTheme(true);
222               else
223                 currentElement.setIsAssociatedParentTheme(false);
224               }
225           else {
226               // field = file+"."+field
227               currentElement.setField(attr.getValue("TableName")+"."+attr.getValue("ColumnName"));
228               currentElement.setIsFixedCriteria(false);
229               }
230           if (Utils.debug) System.out.println("Start ELEMENT ...."+attr.getValue("KeyWord"));
231         }
232 
233         if (qName.equals("DATE")) {
234           currentElement.setFormatDateSql(attr.getValue("Sql"));
235           currentElement.setSeparatorDateSql(attr.getValue("Separator"));
236            if (Utils.debug) System.out.println("Start DATE ...."+attr.getValue("KeyWord"));
237           }
238         if (qName.equals("TIME")) {
239           currentElement.setFormatTimeSql(attr.getValue("Sql"));
240           currentElement.setSeparatorTimeSql(attr.getValue("Separator"));
241           if (Utils.debug) System.out.println("Start TIME ...."+attr.getValue("KeyWord"));
242           }
243 
244         if (qName.equals("GROUP")) {
245           ch2 = attr.getValue("KeyWord");
246           currentGroup = new GroupFixedCriteria(ch2,""/*(String)dicoLabels.get(ch2)*/,attr.getValue("Type"));
247           if (Utils.debug) System.out.println("Start GROUP ...."+attr.getValue("KeyWord"));
248           }
249 
250       if (qName.equals("INPUT") ) {
251           ch2 = attr.getValue("KeyWord");
252           currentInput = new InputFixedCriteria(ch2,""/*(String)dicoLabels.get(ch2)*/,attr.getValue("Value"));
253           if (Utils.debug) System.out.println("Start INPUT ...."+attr.getValue("KeyWord"));
254           }
255       if (qName.equals("LINK") ) {
256           currentLink = new ElementLink(attr.getValue("KeyWordParent"),attr.getValue("KeyWordChild"),attr.getValue("Constant"),idPar,idChild);
257           if (Utils.debug) System.out.println("Start LINK ...."+attr.getValue("KeyWordParent"));
258           }
259       if (qName.equals("ASSOCIATION") ) {
260           // currentTheme.getIdParentFile() : membre de l'association affiché comme un formulaire
261           // attr.getValue("KeyWordAssociated") : membre de l'association affiché comme une liste d'éléments à sélectionner.
262           // currentElement.getRefThemeNumber(): membre de l'association utilisé pour la mise à jour de la base (table des liens entre les 2 membres
263           // précédents de l'association dans la base de données).
264           currentAssociation = new ElementAssociation(currentTheme.getKeyWord(), attr.getValue("KeyWordAssociated"),currentElement.getRefThemeNumber(), attr.getValue("MasterTheme"));
265           if (Utils.debug) System.out.println("Start Association ...."+currentTheme.getKeyWord());
266           }
267     }
268 
269    public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
270           if (qName.equals("COLUMN")) {
271             if (currentGroups.size() > 0) currentElement.setGroups(currentGroups);
272             if (currentLinks.size() > 0) {
273                 currentElement.setElementsLink(currentLinks);
274                 currentTheme.setChildsTheme(currentElement.getRefThemeNumber(),currentElement.getElementsLink());
275                 currentTheme.setHasPlanning(hasPlanning);
276                 }
277             if (currentAssociation != null) {
278                 currentElement.setElementAssociation(currentAssociation);
279                 //currentTheme.setAssociations(currentElement.getKeyParent(),currentElement.getElementsAssociation());
280                 currentTheme.setAssociations( currentElement.getKeyWord(),
281                                               currentElement.getElementAssociation()
282                                             );
283                 currentAssociation = null;
284                 }
285             currentElements.addElement(currentElement);
286             if (Utils.debug) System.out.println("End COLUMN ....");
287             }
288           if (qName.equals("VIEW")) {
289             currentTheme.setThemeElements(currentElements);
290             currentTheme.setPrimaryKeys(currentPrimaryKeys);
291             currentTheme.setPrimaryKeyElements(currentPrimaryKeyElements);
292             themes.put(currentTheme.getKeyWord(),currentTheme);
293             themesByIdFile.put(currentTheme.getIdParentFile(),currentTheme);
294             if (Utils.debug) System.out.println("End VIEW ....");
295            }
296           if (qName.equals("INPUT")) {
297             currentInputs.addElement(currentInput);
298             if (Utils.debug) System.out.println("End INPUT ....");
299             }
300           if (qName.equals("GROUP")) {
301               if (currentInputs.size() > 0) {
302                   currentGroup.setInputs(currentInputs);
303                   currentGroups.addElement(currentGroup);
304                   }
305               if (Utils.debug) System.out.println("End GROUP ....");
306             }
307           if (qName.equals("LINK")) {
308             currentLinks.addElement(currentLink);
309             if (Utils.debug) System.out.println("End LINK ....");
310             }
311           if (qName.equals("ASSOCIATION")) {}
312    }
313 
314    public void characters( char[] ch, int start, int length ) throws SAXException {
315       contents.write( ch, start, length );
316       if (Utils.debug) System.out.print(ch);
317    }
318 
319 }
320