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
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) 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
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: MDLDocumentFactory.java 6617 2007-04-11 01:04:44Z zjin $
023: *
024: */
025: package com.bostechcorp.cbesb.common.mdl;
026:
027: import java.io.File;
028: import java.util.HashMap;
029:
030: import javax.xml.parsers.SAXParserFactory;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.xml.sax.InputSource;
035:
036: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocumentImpl;
037:
038: /**
039: * Provides methods to retrieve instances of MDLDocument from a file or creating
040: * new empty MDLDocuments. This also allows for caching of loaded instances.
041: *
042: */
043: public class MDLDocumentFactory {
044: protected final transient Log logger = LogFactory
045: .getLog(getClass());
046:
047: private HashMap<String, IMDLDocument> cache;
048: private boolean cacheEnabled;
049:
050: /**
051: * To resolver the locations of referenced MDL files.
052: */
053: private IDocLocationResolver docLocationResolver;
054:
055: public MDLDocumentFactory() {
056: cacheEnabled = true;
057: cache = new HashMap<String, IMDLDocument>();
058: docLocationResolver = new DefaultDocLocationResolver();
059: }
060:
061: public MDLDocumentFactory(IDocLocationResolver docLocationResolver) {
062: cacheEnabled = true;
063: cache = new HashMap<String, IMDLDocument>();
064: this .docLocationResolver = docLocationResolver;
065: }
066:
067: /**
068: * Set the cache mode.
069: *
070: * @param enabled boolean
071: */
072: public void setCacheMode(boolean enabled) {
073: cacheEnabled = enabled;
074: }
075:
076: /**
077: * purge the cache.
078: */
079: public void purgeCache() {
080: cache.clear();
081: }
082:
083: /**
084: * @return the docLocationResolver
085: */
086: public IDocLocationResolver getDocLocationResolver() {
087: return docLocationResolver;
088: }
089:
090: /**
091: * @param docLocationResolver the docLocationResolver to set
092: */
093: public void setDocLocationResolver(
094: IDocLocationResolver docLocationResolver) {
095: this .docLocationResolver = docLocationResolver;
096: }
097:
098: public IMDLDocument getMDLDocumentFromFormatSpec(String formatSpec) {
099: try {
100: String cacheID = docLocationResolver
101: .getCacheIDFromFormatSpec(formatSpec);
102: if (cacheEnabled && cache.containsKey(cacheID)) {
103: //This MDL has already been loaded and cached
104: return cache.get(cacheID);
105: } else {
106: //Either the MDL has not been loaded, or cache mode is disabled
107: InputSource inputSrc = docLocationResolver
108: .resolveFormatSpec(formatSpec);
109: IMDLDocument mdlDoc = null;
110: SAXParserFactory factory = SAXParserFactory
111: .newInstance();
112: factory.setNamespaceAware(true);
113: MDLParser mdlParser = new MDLParser(this );
114: File mdlFile = new File(docLocationResolver
115: .getAbsolutePathFromFormatSpec(formatSpec));
116: mdlParser.setMdlFile(mdlFile);
117: mdlDoc = (MDLDocumentImpl) mdlParser.parse(inputSrc);
118: if (cacheEnabled) {
119: //Add it to the cache
120: cache.put(cacheID, mdlDoc);
121: }
122: return mdlDoc;
123: }
124: } catch (MDLException e) {
125: // e.printStackTrace();
126: logger
127: .error("Exception in getMDLDocumentFromFormatSpec(): "
128: + e.getMessage());
129: if (logger.isDebugEnabled()) {
130: logger.debug(
131: "Exception in getMDLDocumentFromFormatSpec():",
132: e);
133: }
134: return null;
135: }
136: }
137:
138: /**
139: * Get the MDL document.
140: *
141: * @param docLocation String
142: * @return a MDL document.
143: */
144: public IMDLDocument getMDLDocument(String docLocation,
145: String currentWorkingDirectory) {
146: try {
147: String cacheID = docLocationResolver
148: .getCacheIDFromDocLocation(docLocation,
149: currentWorkingDirectory);
150: if (cacheEnabled && cache.containsKey(cacheID)) {
151: //This MDL has already been loaded and cached
152: return cache.get(cacheID);
153: } else {
154: //Either the MDL has not been loaded, or cache mode is disabled
155: InputSource inputSrc = docLocationResolver
156: .resolveDocLocation(docLocation,
157: currentWorkingDirectory);
158: MDLDocumentImpl mdlDoc = null;
159: SAXParserFactory factory = SAXParserFactory
160: .newInstance();
161: factory.setNamespaceAware(true);
162: MDLParser mdlParser = new MDLParser(this );
163: File mdlFile = new File(docLocationResolver
164: .getAbsolutePathFromDocLocation(docLocation,
165: currentWorkingDirectory));
166: mdlParser.setMdlFile(mdlFile);
167: mdlDoc = (MDLDocumentImpl) mdlParser.parse(inputSrc);
168: if (cacheEnabled) {
169: //Add it to the cache
170: cache.put(cacheID, mdlDoc);
171: }
172: return mdlDoc;
173: }
174: } catch (MDLException e) {
175: logger.error("Exception in getMDLDocument(): "
176: + e.getMessage());
177: if (logger.isDebugEnabled()) {
178: logger.debug("Exception in getMDLDocument():", e);
179: }
180: return null;
181: }
182: }
183:
184: /**
185: * Get the MDL document For x12 .
186: *
187: * @param docLocation String
188: * @return a MDL document.
189: */
190: // public IMDLDocument getMDLDocument(String docLocation,String zipLocation) {
191: // File mdlFile = new File(docLocation);
192: // mdlDoc = new MDLDocumentImpl();
193: // try {
194: // SAXParserFactory factory = SAXParserFactory.newInstance();
195: // factory.setNamespaceAware(true);
196: //
197: // if (mdlFile == null) {
198: // throw new IllegalArgumentException(new Message(Messages.FILE_IS_NULL, docLocation).getMessage());
199: // }
200: // if(zipLocation==null || zipLocation.equals("")) mdlDoc = MDLParser.load(mdlFile,zipLocation);
201: // else mdlDoc = MDLParser.load(mdlFile,zipLocation);
202: // } catch (MDLException mdle) {
203: // mdle.printStackTrace();
204: // return new MDLDocumentImpl();
205: // }
206: // return mdlDoc;
207: // }
208: //
209: // public IMDLDocument getMDLDocument(InputStream inStream,String zipLocation) {
210: // mdlDoc = new MDLDocumentImpl();
211: // try {
212: // SAXParserFactory factory = SAXParserFactory.newInstance();
213: // factory.setNamespaceAware(true);
214: //
215: // if (zipLocation !=null)
216: // mdlDoc = MDLParser.load(inStream,zipLocation);
217: // else
218: // mdlDoc = MDLParser.load(inStream);
219: // } catch (MDLException mdle) {
220: // mdle.printStackTrace();
221: // return new MDLDocumentImpl();
222: // }
223: // return mdlDoc;
224: // }
225: /**
226: * Get a new MDL document.
227: *
228: * @return a MDL document.
229: */
230: public IMDLDocument getNewMDLDocument() {
231: IMDLDocument mdlDoc = new MDLDocumentImpl();
232: return mdlDoc;
233: }
234:
235: }
|