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: DefaultDocLocationResolver.java 6617 2007-04-11 01:04:44Z zjin $
023: *
024: */
025: package com.bostechcorp.cbesb.common.mdl;
026:
027: import org.xml.sax.InputSource;
028: import java.io.FileInputStream;
029: import java.io.File;
030:
031: import com.bostechcorp.cbesb.common.util.ErrorUtil;
032: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
033:
034: /**
035: * The default implementation of DocLocationResolver that will be used if a
036: * different implementation is not specified in the MDLDocumentFactory.
037: *
038: */
039: public class DefaultDocLocationResolver extends DocLocationResolverBase
040: implements IDocLocationResolver {
041:
042: /* (non-Javadoc)
043: * @see com.bostechcorp.cbesb.common.mdl.IDocLocationResolver#resolveFormatSpec(java.lang.String)
044: */
045: public InputSource resolveFormatSpec(String formatSpec)
046: throws MDLException {
047: try {
048: InputSource inputSrc = null;
049: String absPath = getAbsolutePathFromFormatSpec(formatSpec);
050: FileInputStream fis = new FileInputStream(absPath);
051: inputSrc = new InputSource(fis);
052: return inputSrc;
053: } catch (Exception e) {
054: ErrorUtil.printError("Exception in resolveFormatSpec(): ",
055: e);
056: throw new MDLException(e);
057: }
058: }
059:
060: /**
061: * Resolve the locations of referenced MDL files.
062: *
063: * @param docLocation It is an absolute path or relative path.
064: * @param currentWorkingDirectory The current working directory.
065: * @return InputSource
066: */
067: public InputSource resolveDocLocation(String docLocation,
068: String currentWorkingDirectory) throws MDLException {
069: try {
070: InputSource inputSrc = null;
071: String absPath = getAbsolutePathFromDocLocation(
072: docLocation, currentWorkingDirectory);
073: FileInputStream fis = new FileInputStream(absPath);
074: inputSrc = new InputSource(fis);
075: return inputSrc;
076: } catch (Exception e) {
077: ErrorUtil.printError("Exception in resolveDocLocation(): ",
078: e);
079: throw new MDLException(e);
080: }
081: }
082:
083: public String getCacheIDFromFormatSpec(String formatSpec)
084: throws MDLException {
085: return getAbsolutePathFromFormatSpec(formatSpec);
086: }
087:
088: public String getCacheIDFromDocLocation(String docLocation,
089: String currentWorkingDirectory) throws MDLException {
090: return getAbsolutePathFromDocLocation(docLocation,
091: currentWorkingDirectory);
092: }
093:
094: public String getAbsolutePathFromFormatSpec(String formatSpec)
095: throws MDLException {
096: try {
097: return EsbPathHelper.getFullPathForDef(formatSpec);
098: } catch (Exception e) {
099: ErrorUtil
100: .printError(
101: "Exception in getAbsolutePathFromFormatSpec(): ",
102: e);
103: throw new MDLException(e);
104: }
105: }
106:
107: public String getAbsolutePathFromDocLocation(String docLocation,
108: String currentWorkingDirectory) throws MDLException {
109: File docLocFile = new File(docLocation);
110: if (!docLocFile.isAbsolute()) {
111: File baseDir = new File(currentWorkingDirectory);
112: docLocFile = new File(baseDir, docLocation);
113: }
114: return docLocFile.getAbsolutePath();
115: }
116: }
|