View Javadoc

1   package pt.digitalis.maven.ormgenerator;
2   
3   import org.xml.sax.Attributes;
4   import org.xml.sax.SAXException;
5   import org.xml.sax.helpers.DefaultHandler;
6   
7   import pt.digitalis.maven.ormgenerator.utils.DAOFinder;
8   
9   /**
10   * This class should handler the data in the configuration xml and put it
11   *
12   * @author António Silva <a href="mailto:asilva@digitalis.pt">asilva@digitalis.pt</a>
13   * @created Nov 6, 2007
14   */
15  public class ServiceHandler extends DefaultHandler {
16  
17      /** The DAO interface classes base package directory */
18      private String basePackageDir;
19  
20      /**
21       * This flag indicates if we are inside a service xml tag
22       */
23      private boolean inService = false;
24  
25      /**
26       * This is a temporary variable that holds the service that is being build at the moment
27       */
28      private Services service;
29  
30      /**
31       * Holds all the structure of the xml built
32       */
33      private ServiceCfg serviceCfg = new ServiceCfg();
34  
35      /**
36       * Default constructor
37       *
38       * @param basePackage
39       */
40      public ServiceHandler(String basePackage) {
41          this.basePackageDir = basePackage;
42      }
43  
44      /**
45       * Receive notification of the end of an element and should save the information that is recorded in the data object
46       * to the main object.
47       *
48       * @param uri
49       *            The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing
50       *            is not being performed.
51       * @param localName
52       *            The local name (without prefix), or the empty string if Namespace processing is not being performed.
53       * @param qName
54       *            The qualified name (with prefix), or the empty string if qualified names are not available.
55       * @exception org.xml.sax.SAXException
56       *                Any SAX exception, possibly wrapping another exception.
57       * @see org.xml.sax.ContentHandler#endElement
58       */
59      @Override
60      public void endElement(String uri, String localName, String qName) throws SAXException {
61          if (qName.equals("service")) {
62              this.setInService(false);
63              this.getServiceCfg().getServices().add(service);
64          }
65      }
66  
67      /**
68       * @return the service
69       */
70      private Services getService() {
71          return service;
72      }
73  
74      /**
75       * The Service Configuration built by this class based on the xml this should only be obtained after running parser
76       *
77       * @return the serviceCfg
78       */
79      public ServiceCfg getServiceCfg() {
80          return serviceCfg;
81      }
82  
83      /**
84       * @return the inService
85       */
86      private boolean isInService() {
87          return inService;
88      }
89  
90      /**
91       * @param inService
92       *            the inService to set
93       */
94      private void setInService(boolean inService) {
95          this.inService = inService;
96      }
97  
98      /**
99       * @param service
100      *            the service to set
101      */
102     private void setService(Services service) {
103         this.service = service;
104     }
105 
106     /**
107      * Receive notification of the start of an element and starts the transformation of xml tags to the data objects.
108      *
109      * @param uri
110      *            The NameSpace URI, or the empty string if the element has no NameSpace URI or if NameSpace processing
111      *            is not being performed.
112      * @param localName
113      *            The local name (without prefix), or the empty string if NameSpace processing is not being performed.
114      * @param qName
115      *            The qualified name (with prefix), or the empty string if qualified names are not available.
116      * @param attributes
117      *            The attributes attached to the element. If there are no attributes, it shall be an empty Attributes
118      *            object.
119      * @exception org.xml.sax.SAXException
120      *                Any SAX exception, possibly wrapping another exception.
121      * @see org.xml.sax.ContentHandler#startElement
122      */
123     @Override
124     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
125         if (qName.equals("service")) {
126             this.setInService(true);
127             Services localServicePkgs = new Services();
128             localServicePkgs.setName(attributes.getValue("name"));
129             this.setService(localServicePkgs);
130         }
131         if (this.isInService()) {
132             if (qName.equals("uses")) {
133                 if (attributes.getIndex("dao") != -1) {
134                     String daoEntityName = attributes.getValue("dao");
135                     DAO dao = DAOFinder.findDAO(basePackageDir, daoEntityName);
136 
137                     if (dao != null)
138                         this.getService().getLinkedDAOs().add(dao);
139                 }
140                 if (attributes.getIndex("service") != -1) {
141                     this.getService().getLinkedServices().add(attributes.getValue("service"));
142                 }
143             }
144         }
145     }
146 }