001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.frameworks.generation.pluggable;
016:
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.net.URLConnection;
020:
021: import javax.xml.bind.JAXBContext;
022: import javax.xml.bind.JAXBException;
023: import javax.xml.bind.Marshaller;
024: import javax.xml.bind.Unmarshaller;
025: import javax.xml.bind.Validator;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import com.metaboss.enterprise.bs.BSException;
031: import com.metaboss.enterprise.bs.BSIllegalArgumentException;
032: import com.metaboss.enterprise.bs.BSServiceProviderException;
033: import com.metaboss.sdlctools.frameworks.generation.pluggable.plandetails.GenerationStep;
034:
035: /** This class is an internal parsing helper wrapped around JAXB */
036: class Parser {
037: private static final Log sLogger = LogFactory.getLog(Parser.class);
038:
039: private static JAXBContext cJAXBContext = null;
040: private static Unmarshaller cUnmarshaller = null;
041: private static Marshaller cMarshaller = null;
042: private static Validator cValidator = null;
043:
044: // Helper. Provides single point of access to JAXBContext
045: private static JAXBContext getJAXBContext() throws JAXBException {
046: if (cJAXBContext == null) {
047: // create a JAXBContext capable of handling classes generated into the primer.po package
048: cJAXBContext = JAXBContext
049: .newInstance("com.metaboss.sdlctools.frameworks.generation.pluggable.plandetails");
050: }
051: return cJAXBContext;
052: }
053:
054: // Helper. Provides single point of access to Unmarshaller
055: private static Unmarshaller getUnmarshaller() throws JAXBException {
056: if (cUnmarshaller == null) {
057: // create an Unmarshaller
058: cUnmarshaller = getJAXBContext().createUnmarshaller();
059: }
060: return cUnmarshaller;
061: }
062:
063: // Helper. Provides single point of access to Marshaller
064: private static Marshaller getMarshaller() throws JAXBException {
065: if (cMarshaller == null) {
066: // create an Marshaller
067: cMarshaller = getJAXBContext().createMarshaller();
068: cMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
069: Boolean.TRUE);
070: }
071: return cMarshaller;
072: }
073:
074: // Helper. Provides single point of access to Validator
075: private static Validator getValidator() throws JAXBException {
076: if (cValidator == null) {
077: // create an Marshaller
078: cValidator = getJAXBContext().createValidator();
079: }
080: return cValidator;
081: }
082:
083: /** Returns the parsed root element from the given URL or throws exception if there was a problem */
084: public static GenerationStep parseGenerationPlan(
085: URLConnection pGenerationPlan) throws BSException {
086: InputStream lInputStream = null;
087: try {
088: lInputStream = pGenerationPlan.getInputStream();
089: if (lInputStream != null) {
090: GenerationStep lRet = (GenerationStep) getUnmarshaller()
091: .unmarshal(lInputStream);
092: return lRet;
093: } else
094: throw new BSIllegalArgumentException(
095: "Unable to open stream to URL "
096: + pGenerationPlan.getURL()
097: .toExternalForm());
098: } catch (IOException e) {
099: throw new BSServiceProviderException(
100: "Error reading from URL "
101: + pGenerationPlan.getURL().toExternalForm(),
102: e);
103: } catch (JAXBException e) {
104: throw new BSServiceProviderException(
105: "Error parsing document from URL "
106: + pGenerationPlan.getURL().toExternalForm(),
107: e);
108: } finally {
109: try {
110: if (lInputStream != null)
111: lInputStream.close();
112: } catch (IOException e) {
113: }
114: }
115: }
116: }
|