View Javadoc
1   // $Id: AnnotationContentHandler.java 165 2009-05-28 21:46:38Z agony $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14   */
15  /**
16   * 
17   */
18  package de.mindcrimeilab.xsanalyzer.xml;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.xml.sax.Attributes;
30  import org.xml.sax.ContentHandler;
31  import org.xml.sax.Locator;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * SAX content handler extracting {@code xs:appinfo} and {@code xs:documentation} entries contents of a schema
36   * annotation into a {@code XmlSchemaAnnotation} structure.
37   * 
38   * @author Michael Engelhardt<me@mindcrime-ilab.de>
39   * @author $Author: agony $
40   * @version $Revision: 165 $
41   * @see XmlSchemaAnnotation
42   */
43  public class AnnotationContentHandler implements ContentHandler {
44  
45      /** logger */
46      private static final Log logger = LogFactory.getLog(AnnotationContentHandler.class);
47  
48      /** annotation tag */
49      private static final QName XSD_ANNOTATION_TAG = new QName("http://www.w3.org/2001/XMLSchema", "annotation");
50  
51      /** documentation tag */
52      private static final QName XSD_DOCUMENTATION_TAG = new QName("http://www.w3.org/2001/XMLSchema", "documentation");
53  
54      /** appinfo tag */
55      private static final QName XSD_APPINFO_TAG = new QName("http://www.w3.org/2001/XMLSchema", "appinfo");
56  
57      private final StringBuilder annotationContent;
58  
59      /** temporal holder for the tag contents */
60      private StringBuilder anContent;
61  
62      /**
63       * list of parsed elements of either {@code XmlSchemaAnnotation.Type.APPINFO} or {@code
64       * XmlSchemaAnnotation.Type.DOCUMENTATION}
65       */
66      private final List<XmlSchemaAnnotation> annotations;
67  
68      /**
69       * ctor()
70       */
71      public AnnotationContentHandler() {
72          annotationContent = new StringBuilder();
73          annotations = new ArrayList<XmlSchemaAnnotation>();
74      }
75      
76      
77  
78      /**
79       * Return extracted annotations.
80       * @return
81       */
82      public List<XmlSchemaAnnotation> getAnnotations() {
83          return Collections.unmodifiableList(annotations);
84      }
85  
86  
87  
88      @Override
89      public void characters(char[] ch, int start, int length) throws SAXException {
90          annotationContent.append(ch, start, length);
91          if (null != anContent) {
92              anContent.append(ch, start, length);
93          }
94      }
95  
96      @Override
97      public void endDocument() throws SAXException {
98          logger.debug("Annotation content [" + annotationContent.toString() + "]");
99      }
100 
101     @Override
102     public void endElement(String uri, String localName, String qName) throws SAXException {
103         annotationContent.append("</").append(qName).append(">");
104         if (StringUtils.equals(XSD_APPINFO_TAG.getNamespaceURI(), uri) && StringUtils.equals(XSD_APPINFO_TAG.getLocalPart(), localName)) {
105             logger.debug("found appinfo end tag ");
106             annotations.add(new XmlSchemaAnnotation(XmlSchemaAnnotation.Type.APPINFO, anContent.toString()));
107             anContent = null;
108         }
109         else if (StringUtils.equals(XSD_DOCUMENTATION_TAG.getNamespaceURI(), uri) && StringUtils.equals(XSD_DOCUMENTATION_TAG.getLocalPart(), localName)) {
110             logger.debug("found documentation end tag");
111             annotations.add(new XmlSchemaAnnotation(XmlSchemaAnnotation.Type.DOCUMENTATION, anContent.toString()));
112             anContent = null;
113         }
114         else if (null != anContent) {
115             anContent.append("</").append(qName).append(">");
116         }
117 
118     }
119 
120     @Override
121     public void endPrefixMapping(String arg0) throws SAXException {
122         // TODO Auto-generated method stub
123 
124     }
125 
126     @Override
127     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
128         // TODO Auto-generated method stub
129 
130     }
131 
132     @Override
133     public void processingInstruction(String target, String data) throws SAXException {
134         // TODO Auto-generated method stub
135 
136     }
137 
138     @Override
139     public void setDocumentLocator(Locator locator) {
140         // TODO Auto-generated method stub
141 
142     }
143 
144     @Override
145     public void skippedEntity(String name) throws SAXException {
146         // TODO Auto-generated method stub
147 
148     }
149 
150     @Override
151     public void startDocument() throws SAXException {
152         // TODO Auto-generated method stub
153 
154     }
155 
156     @Override
157     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
158         if (StringUtils.equals(XSD_ANNOTATION_TAG.getNamespaceURI(), uri) && StringUtils.equals(XSD_ANNOTATION_TAG.getLocalPart(), localName)) {
159             logger.debug("found annotation tag");
160         }
161         else if (StringUtils.equals(XSD_APPINFO_TAG.getNamespaceURI(), uri) && StringUtils.equals(XSD_APPINFO_TAG.getLocalPart(), localName)) {
162             logger.debug("found appinfo tag");
163             anContent = new StringBuilder();
164         }
165         else if (StringUtils.equals(XSD_DOCUMENTATION_TAG.getNamespaceURI(), uri) && StringUtils.equals(XSD_DOCUMENTATION_TAG.getLocalPart(), localName)) {
166             logger.debug("found documentation tag");
167             anContent = new StringBuilder();
168         }
169         else if (null != anContent) {
170             anContent.append("<").append(qName);
171             for (int i = 0; i < atts.getLength(); ++i) {
172                 annotationContent.append(" ").append(atts.getQName(i)).append("=\"").append(atts.getValue(i)).append("\"");
173                 anContent.append(" ").append(atts.getQName(i)).append("=\"").append(atts.getValue(i)).append("\"");
174             }
175             anContent.append(">");
176         }
177 
178         annotationContent.append("<").append(qName);
179         for (int i = 0; i < atts.getLength(); ++i) {
180             annotationContent.append(" ").append(atts.getQName(i)).append("=\"").append(atts.getValue(i)).append("\"");
181         }
182         annotationContent.append(">");
183 
184     }
185 
186     @Override
187     public void startPrefixMapping(String prefix, String uri) throws SAXException {
188         // TODO Auto-generated method stub
189 
190     }
191 
192     public String getAnnotationContent() {
193         return annotationContent.toString();
194     }
195 }