001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 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: */
025: package com.bostechcorp.cbesb.common.util.generators;
026:
027: import java.io.File;
028: import java.io.FileNotFoundException;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.util.List;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.jdom.DefaultJDOMFactory;
036: import org.jdom.Document;
037: import org.jdom.Element;
038: import org.jdom.JDOMFactory;
039: import org.jdom.Namespace;
040: import org.jdom.output.Format;
041: import org.jdom.output.XMLOutputter;
042:
043: import com.bostechcorp.cbesb.common.util.TemplateProcessorIO;
044:
045: /**
046: * @author LPS
047: *
048: */
049: public abstract class AbstractDOMGenerator {
050: protected final transient Log logger = LogFactory
051: .getLog(getClass());
052: protected String filePath = "";
053:
054: protected Element rootElement;
055:
056: protected abstract String getFileOut();
057:
058: protected abstract void buildDocumentStructure();
059:
060: protected JDOMFactory jdomFactory = new DefaultJDOMFactory();
061:
062: public AbstractDOMGenerator() {
063: this .filePath = "";
064: buildDocumentStructure();
065: }
066:
067: protected AbstractDOMGenerator(String path) {
068: this .filePath = path;
069: buildDocumentStructure();
070: }
071:
072: private void resetUniqueRootNamespace(Element root,
073: Element namespaceHolder) {
074: List childList = root.getChildren();
075:
076: if (root.getNamespace() != Namespace.NO_NAMESPACE) {
077: Namespace n = namespaceHolder.getNamespace(root
078: .getNamespace().getPrefix());
079: if (n != null) {
080: root.setNamespace(n);
081: } else {
082: root.setNamespace(namespaceHolder.getNamespace());
083: }
084: } else
085: root.setNamespace(namespaceHolder.getNamespace());
086: /*if (childList==null)
087: {
088: //root.setNamespace(uns);
089: return;
090: }else
091: if (childList.size()==0)
092: {
093: //root.setNamespace(uns);
094: return;
095: }
096: else*/
097: {
098: for (Object object : childList) {
099: if (object instanceof Element) {
100: Element childTag = (Element) object;
101: // childTag.setNamespace(uns);
102: resetUniqueRootNamespace(childTag, namespaceHolder);
103: }
104: }
105: }
106:
107: /*Iterator descendants= root.getDescendants();
108: while (descendants.hasNext())
109: {
110: Object element = descendants.next();
111: if (element instanceof Element)
112: {
113: Element childTag = (Element) element;
114: childTag.setNamespace(uns);
115: }
116: else continue;
117: }*/
118:
119: }
120:
121: public void write() {
122: //
123: //Namespace uns=rootElement.getNamespace();
124: //rootElement.getNamespace( );
125: resetUniqueRootNamespace(rootElement, rootElement);
126: //
127: Document document = rootElement.getDocument();
128: if (rootElement.getDocument() == null) {
129: document = new Document(rootElement);
130: }
131: XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
132: FileOutputStream fostream = null;
133:
134: TemplateProcessorIO io = new TemplateProcessorIO("", "", null); // operational tool
135: // Checking if the paths exist,and creating the patch if necessary
136: String onlyPath = getFileOut();
137: if (getFileOut().endsWith(File.separator)) {
138: onlyPath = onlyPath.substring(0, onlyPath.length() - 1);
139: }
140: onlyPath = onlyPath.substring(0, onlyPath.length()
141: - io.getFileName(onlyPath).length());
142: if (!io.exist(onlyPath))
143: io.createPathToFile(getFileOut());
144:
145: try { //oppening the file
146: fostream = new FileOutputStream(getFileOut());
147: } catch (FileNotFoundException e) {
148: logger
149: .error("Exception in opening file: "
150: + e.getMessage());
151: if (logger.isDebugEnabled()) {
152: logger.debug("Exception in opening file:", e);
153: }
154: }
155:
156: //writting the XML Document into file
157: try {
158: xmlOut.output(document, fostream);
159: } catch (IOException e) {
160: logger
161: .error("Exception in writting XML document into file: "
162: + e.getMessage());
163: if (logger.isDebugEnabled()) {
164: logger
165: .debug(
166: "Exception in writting XML document into file:",
167: e);
168: }
169: }
170: // closing the stream
171: try {
172: fostream.close();
173: } catch (Exception e) {
174: logger.error("Exception in closing stream: "
175: + e.getMessage());
176: if (logger.isDebugEnabled()) {
177: logger.debug("Exception in closing stream:", e);
178: }
179: }
180: }
181:
182: }
|