001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2008 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.build.ant.util;
025:
026: import java.io.File;
027: import java.util.List;
028: import java.util.Vector;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036:
037: public class JbiXmlFile {
038:
039: public static final String JBI_NS = "http://java.sun.com/xml/ns/jbi";
040: public static final String JBI = "jbi";
041: public static final String VERSION = "version";
042: public static final String COMPONENT = "component";
043: public static final String IDENTIFICATION = "identification";
044: public static final String NAME = "name";
045: public static final String DESCRIPTION = "description";
046: public static final String COMPONENT_CLASS_NAME = "component-class-name";
047: public static final String COMPONENT_CLASS_PATH = "component-class-path";
048: public static final String BOOTSTRAP_CLASS_NAME = "bootstrap-class-name";
049: public static final String BOOTSTRAP_CLASS_PATH = "bootstrap-class-path";
050: public static final String PATH_ELEMENT = "path-element";
051: public static final String SHARED_LIBRARY = "shared-library";
052: public static final String SHARED_LIBRARY_CLASS_PATH = "shared-library-class-path";
053:
054: private boolean isComponent = false;
055: private boolean isSharedLibrary = false;
056: private String name;
057: private String description;
058: private String componentClassName;
059: private Vector<String> componentClasspath;
060: private String bootstrapClassName;
061: private Vector<String> bootstrapClasspath;
062: private Vector<String> sharedLibraries;
063: private Vector<String> sharedLibraryClasspath;
064:
065: public JbiXmlFile() {
066: name = null;
067: description = null;
068: componentClassName = null;
069: componentClasspath = new Vector<String>();
070: bootstrapClassName = null;
071: bootstrapClasspath = new Vector<String>();
072: sharedLibraries = new Vector<String>();
073: sharedLibraryClasspath = new Vector<String>();
074: }
075:
076: public boolean isComponent() {
077: return isComponent;
078: }
079:
080: public boolean isSharedLibrary() {
081: return isSharedLibrary;
082: }
083:
084: public String getBootstrapClassName() {
085: return bootstrapClassName;
086: }
087:
088: public String getComponentClassName() {
089: return componentClassName;
090: }
091:
092: public String getDescription() {
093: return description;
094: }
095:
096: public String getName() {
097: return name;
098: }
099:
100: public List<String> getComponentClasspath() {
101: return componentClasspath;
102: }
103:
104: public List<String> getBootstrapClasspath() {
105: return bootstrapClasspath;
106: }
107:
108: public List<String> getSharedLibraries() {
109: return sharedLibraries;
110: }
111:
112: public List<String> getSharedLibraryClasspath() {
113: return sharedLibraryClasspath;
114: }
115:
116: public void load(File jbiFile) throws Exception {
117: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
118: .newInstance();
119: docBuilderFactory.setNamespaceAware(true);
120: DocumentBuilder docBuilder = docBuilderFactory
121: .newDocumentBuilder();
122: Document doc = docBuilder.parse(jbiFile);
123: Element root = doc.getDocumentElement();
124: if (JBI.equals(root.getLocalName())
125: && JBI_NS.equals(root.getNamespaceURI())) {
126: Node child = root.getFirstChild();
127: while (child != null) {
128: if (child.getNodeType() == Node.ELEMENT_NODE) {
129: if (child.getLocalName().equals(COMPONENT)) {
130: isComponent = true;
131: Node child2 = child.getFirstChild();
132: while (child2 != null) {
133: if (child2.getNodeType() == Node.ELEMENT_NODE) {
134: if (child2.getLocalName().equals(
135: IDENTIFICATION)) {
136: loadIdentification((Element) child2);
137: } else if (child2.getLocalName()
138: .equals(COMPONENT_CLASS_NAME)) {
139: loadComponentClassName((Element) child2);
140: } else if (child2.getLocalName()
141: .equals(COMPONENT_CLASS_PATH)) {
142: loadComponentClassPath((Element) child2);
143: } else if (child2.getLocalName()
144: .equals(BOOTSTRAP_CLASS_NAME)) {
145: loadBootstrapClassName((Element) child2);
146: } else if (child2.getLocalName()
147: .equals(BOOTSTRAP_CLASS_PATH)) {
148: loadBootstrapClassPath((Element) child2);
149: } else if (child2.getLocalName()
150: .equals(SHARED_LIBRARY)) {
151: loadSharedLibrary((Element) child2);
152: }
153: }
154: child2 = child2.getNextSibling();
155: }
156: } else if (child.getLocalName().equals(
157: SHARED_LIBRARY)) {
158: isSharedLibrary = true;
159: Node child2 = child.getFirstChild();
160: while (child2 != null) {
161: if (child2.getNodeType() == Node.ELEMENT_NODE) {
162: if (child2.getLocalName().equals(
163: IDENTIFICATION)) {
164: loadIdentification((Element) child2);
165: } else if (child2
166: .getLocalName()
167: .equals(
168: SHARED_LIBRARY_CLASS_PATH)) {
169: loadSharedLibraryClasspath((Element) child2);
170: }
171: }
172: child2 = child2.getNextSibling();
173: }
174:
175: }
176: }
177: child = child.getNextSibling();
178: }
179: } else {
180: throw new Exception("Root element of file is not '{"
181: + JBI_NS + "}" + JBI + "'");
182: }
183: }
184:
185: private void loadIdentification(Element identification) {
186: Node child = identification.getFirstChild();
187: while (child != null) {
188: if (child.getNodeType() == Node.ELEMENT_NODE) {
189: if (child.getLocalName().equals(NAME)) {
190: name = child.getTextContent();
191: } else if (child.getLocalName().equals(DESCRIPTION)) {
192: description = child.getTextContent();
193: }
194: }
195: child = child.getNextSibling();
196: }
197: }
198:
199: private void loadComponentClassName(Element componentClassName) {
200: this .componentClassName = componentClassName.getTextContent();
201: }
202:
203: private void loadComponentClassPath(Element componentClasspath) {
204: Node child = componentClasspath.getFirstChild();
205: while (child != null) {
206: if (child.getNodeType() == Node.ELEMENT_NODE
207: && child.getLocalName().equals(PATH_ELEMENT)) {
208: this .componentClasspath.add(child.getTextContent());
209: }
210: child = child.getNextSibling();
211: }
212: }
213:
214: private void loadBootstrapClassName(Element bootstrapClassName) {
215: this .bootstrapClassName = bootstrapClassName.getTextContent();
216: }
217:
218: private void loadBootstrapClassPath(Element bootstrapClassPath) {
219: Node child = bootstrapClassPath.getFirstChild();
220: while (child != null) {
221: if (child.getNodeType() == Node.ELEMENT_NODE
222: && child.getLocalName().equals(PATH_ELEMENT)) {
223: this .bootstrapClasspath.add(child.getTextContent());
224: }
225: child = child.getNextSibling();
226: }
227: }
228:
229: private void loadSharedLibrary(Element sharedLib) {
230: sharedLibraries.add(sharedLib.getTextContent());
231: }
232:
233: private void loadSharedLibraryClasspath(Element sharedLibClasspath) {
234: Node child = sharedLibClasspath.getFirstChild();
235: while (child != null) {
236: if (child.getNodeType() == Node.ELEMENT_NODE
237: && child.getLocalName().equals(PATH_ELEMENT)) {
238: this.sharedLibraryClasspath.add(child.getTextContent());
239: }
240: child = child.getNextSibling();
241: }
242:
243: }
244: }
|