View Javadoc

1   package org.opensync.engine.server.config;
2   
3   import java.io.IOException;
4   import java.io.StringReader;
5   
6   import javax.xml.parsers.ParserConfigurationException;
7   import javax.xml.parsers.SAXParserFactory;
8   
9   import org.opensync.engine.server.OpenSyncException;
10  import org.opensync.engine.server.Source;
11  import org.opensync.engine.server.Sources;
12  import org.opensync.engine.server.Synchronization;
13  import org.opensync.engine.server.Synchronizations;
14  import org.opensync.engine.server.View;
15  import org.xml.sax.Attributes;
16  import org.xml.sax.InputSource;
17  import org.xml.sax.SAXException;
18  import org.xml.sax.XMLReader;
19  import org.xml.sax.helpers.DefaultHandler;
20  
21  
22  /***
23    * This class represents the parser use to parse the synchronizations from the config file
24    * 
25    * @version	1.0
26    * @author	SOFTMED
27    */
28  
29   public class SynchronizationsSxPsr extends DefaultHandler {
30    private XMLReader parser;
31    private Synchronizations synchronizations = new Synchronizations();
32    private Synchronization synchronization;
33    private Sources sources;
34  
35    /***
36     * Construct the SynchronizationSxPsr
37     * 
38     * @param	sources
39     * @exception	ParserConfigurationException
40     */
41    public SynchronizationsSxPsr(Sources sources)throws ParserConfigurationException{
42      try {
43        this.sources = sources;
44        parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
45        parser.setContentHandler(this);
46      }
47      catch (SAXException ex) {
48        throw new RuntimeException(ex.getMessage());
49      }
50    }
51    /***
52     * @param	xml
53     * @exception	IOException
54     * @exception	SAXException
55     */
56    public Synchronizations parseSynchronizations(String xml)throws SAXException,IOException{
57      parser.parse(new InputSource(new StringReader(xml)));
58      return synchronizations;
59    }
60    /***
61     * @param	uri
62     * @param	localName ?qName?
63     * @param	qName
64     * @param	attributes
65     * @exception	SAXException
66     */
67    public void startElement(String uri, String localName, String qName, Attributes attributes)
68    throws SAXException{
69      if(qName.equals("synchronization")){
70        synchronization = new Synchronization();
71        synchronization.setName(attributes.getValue("nameId"));
72        synchronization.setUrlMapping(attributes.getValue("urlMapping"));
73        synchronizations.add(synchronization);
74      }
75      else if(qName.equals("from")){
76        String sourceRef = attributes.getValue("sourceRef");
77        Source source = sources.get(sourceRef);
78        if(source == null){
79          throw new SAXException(new OpenSyncException(
80              "error.config.source.not-found",new Object[]{sourceRef}
81            )
82          );
83        }
84        String viewRef = attributes.getValue("viewRef");
85        View view = source.getView(viewRef);
86        if(view == null){
87          throw new SAXException(new OpenSyncException(
88              "error.config.view.not-found",new Object[]{viewRef,sourceRef}
89            )
90          );
91        }
92        synchronization.setFromSource(source);
93        synchronization.setFromView(view);
94      }
95      else if(qName.equals("to")){
96        String sourceRef = attributes.getValue("sourceRef");
97        Source source = sources.get(sourceRef);
98        if(source == null){
99          throw new SAXException(new OpenSyncException(
100             "error.config.source.not-found",new Object[]{sourceRef}
101           )
102         );
103       }
104       String viewRef = attributes.getValue("viewRef");
105       View view = source.getView(viewRef);
106       if(view == null){
107         throw new SAXException(new OpenSyncException(
108             "error.config.view.not-found",new Object[]{viewRef,sourceRef}
109           )
110         );
111       }
112       synchronization.setToSource(source);
113       synchronization.setToView(view);
114     }
115   }
116 }