001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 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: ConfigFileSupport.java 8115 2007-06-29 05:43:49Z zjin $
023: */
024: package com.bostechcorp.cbesb.runtime.scheduler;
025:
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.TransformerFactory;
038:
039: import org.jdom.Attribute;
040: import org.jdom.DefaultJDOMFactory;
041: import org.jdom.Document;
042: import org.jdom.Element;
043: import org.jdom.JDOMException;
044: import org.jdom.JDOMFactory;
045: import org.jdom.Namespace;
046: import org.jdom.input.SAXBuilder;
047: import org.jdom.output.XMLOutputter;
048: import org.jdom.output.Format;
049: import org.xml.sax.InputSource;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: public abstract class ConfigFileSupport {
055:
056: private Log log;
057:
058: private JDOMFactory jDomFactory;
059: private Document doc;
060: private HashMap<String, Namespace> namespaceUriMap;
061: private HashMap<String, Namespace> namespacePrefixMap;
062: private int namespaceCounter;
063: protected String namespaceURI;
064: protected String rootName;
065:
066: public ConfigFileSupport() {
067: log = LogFactory.getLog(ConfigFileSupport.class);
068: jDomFactory = new DefaultJDOMFactory();
069: namespaceUriMap = new HashMap<String, Namespace>();
070: namespacePrefixMap = new HashMap<String, Namespace>();
071: }
072:
073: /**
074: * @return the namespaceURI
075: */
076: protected String getNamespaceURI() {
077: return namespaceURI;
078: }
079:
080: /**
081: * @param namespaceURI the namespaceURI to set
082: */
083: protected void setNamespaceURI(String namespaceURI) {
084: this .namespaceURI = namespaceURI;
085: }
086:
087: /**
088: * @return the rootName
089: */
090: protected String getRootName() {
091: return rootName;
092: }
093:
094: /**
095: * @param rootName the rootName to set
096: */
097: protected void setRootName(String rootName) {
098: this .rootName = rootName;
099: }
100:
101: protected void addNamespace(String NamespaceURI) {
102: if (!namespaceUriMap.containsKey(NamespaceURI)) {
103: String prefix = "q" + namespaceCounter;
104: namespaceCounter++;
105: Namespace ns = Namespace.getNamespace(prefix, NamespaceURI);
106: addNamespaceToMaps(ns);
107: }
108: }
109:
110: protected void AddNamespaceDeclarationsToRoot(Element root) {
111: for (Iterator iter = namespaceUriMap.values().iterator(); iter
112: .hasNext();) {
113: root.addNamespaceDeclaration((Namespace) iter.next());
114: }
115: }
116:
117: private void addNamespaceToMaps(Namespace ns) {
118: namespaceUriMap.put(ns.getURI(), ns);
119: namespacePrefixMap.put(ns.getPrefix(), ns);
120: }
121:
122: public boolean load(Object configFile) throws JDOMException,
123: IOException {
124:
125: SAXBuilder saxBuilder = new SAXBuilder();
126: // try {
127: if (configFile instanceof File) {
128: doc = saxBuilder.build((File) configFile);
129: } else {
130: doc = saxBuilder.build((BufferedReader) configFile);
131: }
132:
133: // }
134: // catch (JDOMException e)
135: // {
136: // log.error("Exception caught parsing file: " + configFile.toString(), e);
137: // doc = null;
138: // return false;
139: // }
140: // catch (IOException e)
141: // {
142: // log.error("Exception caught parsing file: " + configFile.toString(), e);
143: // doc = null;
144: // return false;
145: // }
146:
147: Element rootElem = doc.getRootElement();
148: if (!(rootElem.getNamespaceURI().equals(namespaceURI) && rootElem
149: .getName().equals(rootName))) {
150: log.error("Unexpected XML document. Expected {"
151: + namespaceURI + "}" + rootName + " received {"
152: + rootElem.getNamespaceURI() + "}"
153: + rootElem.getName());
154: return false;
155: }
156: List nsList = rootElem.getAdditionalNamespaces();
157: for (Iterator iter = nsList.iterator(); iter.hasNext();) {
158: Namespace ns = (Namespace) iter.next();
159: addNamespaceToMaps(ns);
160: }
161: return processParsedDoc();
162: }
163:
164: public boolean save(File configFile) {
165: if (buildDoc()) {
166: AddNamespaceDeclarationsToRoot(getRootElement());
167: try {
168: FileOutputStream fos = new FileOutputStream(configFile);
169: XMLOutputter xmlOutputter = new XMLOutputter(Format
170: .getPrettyFormat());
171: xmlOutputter.output(doc, fos);
172: fos.close();
173: return true;
174: } catch (IOException e) {
175: log.error("Exception caught while saving config file: "
176: + configFile.getAbsolutePath(), e);
177: return false;
178: }
179: } else {
180: log.error("Unable to build XML document for config file: "
181: + configFile.getAbsolutePath());
182: return false;
183: }
184: }
185:
186: protected void createNewDoc() {
187: Element rootElem = createElement(rootName);
188: doc = jDomFactory.document(rootElem);
189: }
190:
191: protected Element createElement(String name) {
192: return jDomFactory.element(name, namespaceURI);
193: }
194:
195: protected Attribute createAttribute(String name, String value) {
196: return jDomFactory.attribute(name, value);
197: }
198:
199: protected Element getRootElement() {
200: return doc.getRootElement();
201: }
202:
203: protected Element getChild(Element parent, String name) {
204: return parent.getChild(name, Namespace
205: .getNamespace(namespaceURI));
206: }
207:
208: protected String getChildTextValue(Element parent, String name) {
209: Element child = parent.getChild(name, Namespace
210: .getNamespace(namespaceURI));
211: if (child != null) {
212: return child.getText();
213: } else {
214: return null;
215: }
216: }
217:
218: protected QName prefixedNameToQName(String prefixedName) {
219: String prefix;
220: String localName;
221: QName qname;
222: int index = prefixedName.indexOf(':');
223: if (index > 0) {
224: prefix = prefixedName.substring(0, index);
225: localName = prefixedName.substring(index + 1);
226: } else {
227: prefix = null;
228: localName = prefixedName;
229: }
230: Namespace ns = namespacePrefixMap.get(prefix);
231: if (ns != null) {
232: qname = new QName(ns.getURI(), localName);
233: } else {
234: log.error("Unable to resolve namespace prefix " + prefix);
235: qname = null;
236: }
237: return qname;
238: }
239:
240: protected String QNameToPrefixedName(QName qname) {
241: addNamespace(qname.getNamespaceURI());
242: Namespace ns = namespaceUriMap.get(qname.getNamespaceURI());
243: String prefix = ns.getPrefix();
244: if (prefix == null || prefix.equals("")) {
245: return qname.getLocalPart();
246: } else {
247: return prefix + ":" + qname.getLocalPart();
248: }
249:
250: }
251:
252: protected abstract boolean processParsedDoc();
253:
254: protected abstract boolean buildDoc();
255:
256: }
|