001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal.annotationreader;
020:
021: import org.apache.beehive.netui.util.xml.DomUtils;
022: import org.apache.beehive.netui.util.logging.Logger;
023:
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.ParserConfigurationException;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.xml.sax.SAXException;
035:
036: public final class ProcessedAnnotationParser {
037: private static final Logger _log = Logger
038: .getInstance(ProcessedAnnotationParser.class);
039:
040: private static final String ANNOTATED_ELEMENT = "annotated-element";
041: private static final String ANNOTATION = "annotation";
042: private static final String ANNOTATION_ATTRIBUTE = "annotation-attribute";
043: private static final String ANNOTATION_NAME = "annotation-name";
044: private static final String ATTRIBUTE_NAME = "attribute-name";
045: private static final String ATTRIBUTE_STRING_VALUE = "string-value";
046: private static final String ATTRIBUTE_VALUE = "annotation-value";
047: private static final String ELEMENT_NAME = "element-name";
048: private static final String TYPE_NAME = "type-name";
049:
050: /* do not construct */
051: private ProcessedAnnotationParser() {
052: }
053:
054: public static ProcessedAnnotations parse(
055: final String annotationsXml, final InputStream is) {
056: assert is != null;
057:
058: ProcessedAnnotations processedAnnotations = null;
059: try {
060: /* parse the config document */
061: DocumentBuilderFactory dbf = DocumentBuilderFactory
062: .newInstance();
063: DocumentBuilder db = dbf.newDocumentBuilder();
064: Document document = db.parse(is);
065: Element root = document.getDocumentElement();
066: String typeName = getElementText(root, TYPE_NAME);
067: assert typeName != null : "Missing the following element: "
068: + TYPE_NAME;
069: Map annotatedElements = parseAnnotatedElements(root);
070: processedAnnotations = new ProcessedAnnotations(typeName,
071: annotatedElements);
072: } catch (ParserConfigurationException pce) {
073: _log.error(
074: "Error occurred while parsing annotations XML file "
075: + annotationsXml, pce);
076: } catch (SAXException saxe) {
077: _log.error(
078: "Error occurred while parsing annotations XML file "
079: + annotationsXml, saxe);
080: } catch (IOException ioe) {
081: _log.error(
082: "Error occurred while parsing annotations XML file "
083: + annotationsXml, ioe);
084: }
085: return processedAnnotations;
086: }
087:
088: private static final Map parseAnnotatedElements(Element parent) {
089: if (parent == null) {
090: return null;
091: }
092:
093: List list = DomUtils.getChildElementsByName(parent,
094: ANNOTATED_ELEMENT);
095: if (list == null || list.size() == 0) {
096: return null;
097: }
098:
099: HashMap annotatedElements = new HashMap();
100: for (int i = 0; i < list.size(); i++) {
101: Element elem = (Element) list.get(i);
102: String name = getElementText(elem, ELEMENT_NAME);
103: assert name != null : "Missing the following element: "
104: + ELEMENT_NAME;
105:
106: ProcessedAnnotation[] annotations = parseProcessedAnnotations(
107: elem, ANNOTATION);
108: assert annotations != null : "Missing the following element: "
109: + ANNOTATION;
110:
111: annotatedElements.put(name, annotations);
112: }
113: return annotatedElements;
114: }
115:
116: private static final ProcessedAnnotation[] parseProcessedAnnotations(
117: Element parent, String nodeName) {
118: if (parent == null) {
119: return null;
120: }
121:
122: List list = DomUtils.getChildElementsByName(parent, nodeName);
123: if (list == null || list.size() == 0) {
124: return null;
125: }
126:
127: ProcessedAnnotation[] annotations = new ProcessedAnnotation[list
128: .size()];
129: for (int i = 0; i < list.size(); i++) {
130: Element elem = (Element) list.get(i);
131: String name = getElementText(elem, ANNOTATION_NAME);
132: assert name != null : "Missing the following element: "
133: + ANNOTATION_NAME;
134:
135: AnnotationAttribute[] attributes = parseAnnotationAttribute(elem);
136: annotations[i] = new ProcessedAnnotation(name, attributes);
137: }
138: return annotations;
139: }
140:
141: private static final AnnotationAttribute[] parseAnnotationAttribute(
142: Element parent) {
143: if (parent == null) {
144: return null;
145: }
146:
147: List list = DomUtils.getChildElementsByName(parent,
148: ANNOTATION_ATTRIBUTE);
149: if (list == null || list.size() == 0) {
150: return null;
151: }
152:
153: AnnotationAttribute[] attributes = new AnnotationAttribute[list
154: .size()];
155: for (int i = 0; i < list.size(); i++) {
156: Element elem = (Element) list.get(i);
157: String name = getElementText(elem, ATTRIBUTE_NAME);
158: assert name != null : "Missing the following element: "
159: + ATTRIBUTE_NAME;
160:
161: String value = getElementText(elem, ATTRIBUTE_STRING_VALUE);
162: if (value != null) {
163: attributes[i] = new AnnotationAttribute(name, value);
164: } else {
165: ProcessedAnnotation[] annotations = parseProcessedAnnotations(
166: elem, ATTRIBUTE_VALUE);
167: attributes[i] = new AnnotationAttribute(name,
168: annotations);
169: }
170: }
171: return attributes;
172: }
173:
174: private static String getElementText(Element parent,
175: String elementName) {
176: Element child = DomUtils.getChildElementByName(parent,
177: elementName);
178: if (child != null) {
179: String text = DomUtils.getElementText(child);
180: if (text != null) {
181: return text.length() == 0 ? null : text;
182: }
183: }
184:
185: return null;
186: }
187: }
|