001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.masterindex.plugin.datamodel;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.util.List;
046: import java.util.ArrayList;
047: import javax.xml.parsers.DocumentBuilder;
048: import javax.xml.parsers.DocumentBuilderFactory;
049: import javax.xml.parsers.ParserConfigurationException;
050:
051: import org.w3c.dom.Document;
052: import org.w3c.dom.Element;
053: import org.w3c.dom.NodeList;
054: import org.xml.sax.SAXException;
055:
056: /**
057: *
058: * ObjectDefinitionBuilder : this class is mainly used to build an
059: * ObjectDefinition from xml input stream and from ePaths.
060: *
061: *
062: * @author Sujit Biswas
063: *
064: */
065: public class ObjectDefinitionBuilder {
066:
067: private static final String TAG = "tag";
068: private static final String NODES = "nodes";
069: private static final String KEY_TYPE = "key-type";
070: private static final String CODE_MODULE = "code-module";
071: private static final String REQUIRED = "required";
072: private static final String UPDATEABLE = "updateable";
073: private static final String SIZE = "size";
074: private static final String FIELD_TYPE = "field-type";
075: private static final String FIELD_NAME = "field-name";
076: private static final String FIELDS = "fields";
077: private static final String CHILDREN = "children";
078: private static final String NAME = "name";
079: private static final String RELATIONSHIPS = "relationships";
080:
081: /**
082: * parse the object-definition.xml inputStream to build the ObjectDefinition
083: * object
084: *
085: * @param objectDefinition
086: * inputStream
087: * @return ObjectDefinition
088: */
089: public ObjectDefinition parse(InputStream objectDefinition) {
090:
091: ObjectDefinition parent = null;
092: try {
093: Element root = getRootElement(objectDefinition);
094:
095: Element relation = (Element) root.getElementsByTagName(
096: RELATIONSHIPS).item(0);
097:
098: Element name = (Element) relation
099: .getElementsByTagName(NAME).item(0);
100:
101: parent = new ObjectDefinition(name.getTextContent().trim());
102:
103: updateFields(parent, root);
104:
105: NodeList nl = relation.getElementsByTagName(CHILDREN);
106:
107: for (int i = 0; i < nl.getLength(); i++) {
108:
109: Element e = (Element) nl.item(i);
110: ObjectDefinition child = new ObjectDefinition(e
111: .getTextContent().trim());
112: parent.addchild(child);
113: updateFields(child, root);
114: }
115:
116: } catch (Exception e) {
117: e.printStackTrace();
118: }
119:
120: return parent;
121:
122: }
123:
124: /**
125: * Bulds Object Defintion from given set of epaths.
126: * @param epaths
127: * @return
128: * @throws EPathException
129: */
130: /*
131: public static ObjectDefinition buildObjectDefinition(String[] epaths) throws EPathException {
132: ObjectDefinition root = null;
133: ObjectDefinition cur = root;
134: for (int i = 0; i < epaths.length; i++) {
135: EPath e = EPathParser.parse(epaths[i]);
136:
137:
138: String[] names = e.getTokenQueue();
139:
140:
141: for (int j = 0; j < names.length-1; j++) {
142: String name = names[j];
143:
144: if (j == 0) {
145: if (root == null) {
146: root = new ObjectDefinition(name);
147: }
148: cur = root;
149: } else if (j != 0) {
150: ObjectDefinition child = findChild(cur, name);
151: if (child == null) {
152: child = new ObjectDefinition(name);
153: cur.addchild(child);
154: }
155: cur = child;
156: }
157:
158: }
159: String field = names[names.length-1]; // last name is field name
160: if (!findField(cur, field)) {
161: addField(cur, field);
162: }
163: }
164:
165: return root;
166: }
167: */
168: /*
169: *
170: */
171: private static ObjectDefinition findChild(ObjectDefinition od,
172: String name) {
173: List<ObjectDefinition> children = od.getChildren();
174: for (int k = 0; k < children.size(); k++) {
175: ObjectDefinition cur = children.get(k);
176: if (cur.getName().equals(name)) {
177: return cur;
178: }
179: }
180: return null;
181: }
182:
183: private static boolean findField(ObjectDefinition node, String field) {
184: List<Field> fields = node.getFields();
185: for (int i = 0; i < fields.size(); i++) {
186: Field f = fields.get(i);
187: if (field.equals(f.getName())) {
188: return true;
189: }
190: }
191: return false;
192:
193: }
194:
195: private static void addField(ObjectDefinition node, String field) {
196: Field f = new Field();
197: f.setName(field);
198: node.addField(f);
199: }
200:
201: /**
202: * @param objectDefinition
203: * inputStream
204: * @return the root element for a document object
205: * @throws ParserConfigurationException
206: * @throws SAXException
207: * @throws IOException
208: */
209: private Element getRootElement(InputStream objectDefinition)
210: throws ParserConfigurationException, SAXException,
211: IOException {
212: Document doc = getDocument(objectDefinition);
213: Element root = doc.getDocumentElement();
214: return root;
215: }
216:
217: /**
218: * update the object definition fields
219: *
220: * @param child
221: * @param root
222: */
223: private void updateFields(ObjectDefinition child, Element root) {
224: Element e = getNode(child.getName(), root);
225:
226: ArrayList<Field> fields = getFields(e);
227:
228: child.setFields(fields);
229:
230: }
231:
232: /**
233: *
234: * @param e
235: * @return the list if fields for a given object node
236: */
237: private ArrayList<Field> getFields(Element e) {
238:
239: ArrayList<Field> fields = new ArrayList<Field>();
240: NodeList nl = e.getElementsByTagName(FIELDS);
241:
242: for (int i = 0; i < nl.getLength(); i++) {
243: Element fieldNode = (Element) nl.item(i);
244:
245: Field f = createField(fieldNode);
246:
247: fields.add(f);
248: }
249:
250: return fields;
251: }
252:
253: /**
254: * creates a field object from the xml
255: *
256: * @param fieldNode
257: * @return a ObjectDefnition field
258: */
259: private Field createField(Element fieldNode) {
260:
261: Field f = new Field();
262:
263: Element field_name = (Element) fieldNode.getElementsByTagName(
264: FIELD_NAME).item(0);
265:
266: if (field_name != null)
267: f.setName(field_name.getTextContent().trim());
268:
269: Element field_type = (Element) fieldNode.getElementsByTagName(
270: FIELD_TYPE).item(0);
271:
272: if (field_type != null)
273: f.setType(field_type.getTextContent().trim());
274:
275: Element size = (Element) fieldNode.getElementsByTagName(SIZE)
276: .item(0);
277:
278: if (size != null)
279: f.setSize(Integer.parseInt(size.getTextContent().trim()));
280:
281: Element updateable = (Element) fieldNode.getElementsByTagName(
282: UPDATEABLE).item(0);
283:
284: if (updateable != null)
285: f.setUpdateable(Boolean.parseBoolean(updateable
286: .getTextContent().trim()));
287:
288: Element required = (Element) fieldNode.getElementsByTagName(
289: REQUIRED).item(0);
290:
291: if (required != null)
292: f.setRequired(Boolean.parseBoolean(required
293: .getTextContent().trim()));
294:
295: Element code_module = (Element) fieldNode.getElementsByTagName(
296: CODE_MODULE).item(0);
297:
298: if (code_module != null)
299: f.setCodeModule(code_module.getTextContent().trim());
300:
301: Element key_type = (Element) fieldNode.getElementsByTagName(
302: KEY_TYPE).item(0);
303:
304: if (key_type != null)
305: f.setKeyType(Boolean.parseBoolean(key_type.getTextContent()
306: .trim()));
307:
308: return f;
309: }
310:
311: /**
312: *
313: * @param name
314: * Object definition name
315: * @param root
316: * @return the corresponding object definition node
317: */
318:
319: private Element getNode(String name, Element root) {
320: NodeList nl = root.getElementsByTagName(NODES);
321:
322: for (int i = 0; i < nl.getLength(); i++) {
323:
324: Element e = (Element) nl.item(i);
325:
326: if (matches(name, e)) {
327: return e;
328: }
329:
330: }
331:
332: return null;
333: }
334:
335: private boolean matches(String name, Element e) {
336: Element tag = (Element) e.getElementsByTagName(TAG).item(0);
337:
338: return tag.getTextContent().trim().equals(name);
339:
340: }
341:
342: /**
343: *
344: * @param objectDef.xml
345: * @return Document object
346: * @throws ParserConfigurationException
347: * @throws SAXException
348: * @throws IOException
349: */
350: private Document getDocument(InputStream objectDef)
351: throws ParserConfigurationException, SAXException,
352: IOException {
353: DocumentBuilderFactory docFactory = DocumentBuilderFactory
354: .newInstance();
355: // docFactory.setValidating(true);
356:
357: DocumentBuilder builder = docFactory.newDocumentBuilder();
358:
359: return builder.parse(objectDef);
360: }
361: }
|