001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.dataretrieval;
021:
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axiom.om.OMException;
025: import org.apache.axiom.om.OMFactory;
026: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
027: import org.apache.axiom.om.util.StAXUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import javax.xml.stream.XMLStreamException;
032: import javax.xml.stream.XMLStreamReader;
033: import java.io.InputStream;
034:
035: public class DataRetrievalUtil {
036: private static final Log log = LogFactory
037: .getLog(DataRetrievalUtil.class);
038:
039: private static DataRetrievalUtil instance = null;
040:
041: public static DataRetrievalUtil getInstance() {
042: if (instance == null) {
043: instance = new DataRetrievalUtil();
044: }
045: return instance;
046: }
047:
048: /**
049: * Loading xml file content and convert to OMElement.
050: *
051: * @param file - file path relative to the Service Repository
052: * @return OMElement format of the xml file content
053: * @throws DataRetrievalException
054: */
055:
056: public OMElement buildOM(ClassLoader classLoader, String file)
057: throws DataRetrievalException {
058: OMElement element = null;
059: InputStream servicexmlStream = null;
060: try {
061: servicexmlStream = getInputStream(classLoader, file);
062:
063: element = convertToOMElement(servicexmlStream);
064: } catch (Exception e) {
065: throw new DataRetrievalException(
066: "Failed to load from file, " + file, e);
067: }
068:
069: return element;
070: }
071:
072: /**
073: * Convert servicexmlStream to OMElement
074: *
075: * @param servicexmlStream InputStream contain xml content
076: * @return OMElement format of the xml content
077: * @throws XMLStreamException
078: */
079:
080: public static OMElement convertToOMElement(
081: InputStream servicexmlStream) throws XMLStreamException,
082: OMException {
083: OMElement element = null;
084:
085: XMLStreamReader xmlReader = StAXUtils
086: .createXMLStreamReader(servicexmlStream);
087: OMFactory fac = OMAbstractFactory.getOMFactory();
088: StAXOMBuilder staxOMBuilder = new StAXOMBuilder(fac, xmlReader);
089: element = staxOMBuilder.getDocumentElement();
090: element.build();
091: return element;
092: }
093:
094: private static InputStream getInputStream(ClassLoader classLoader,
095: String file) throws XMLStreamException {
096:
097: InputStream servicexmlStream = classLoader
098: .getResourceAsStream(file);
099:
100: if (servicexmlStream == null) {
101: String message = "File does not exist in the Service Repository! File="
102: + file;
103: if (log.isDebugEnabled()) {
104: log.debug(message);
105: }
106: throw new XMLStreamException(message);
107: }
108: return servicexmlStream;
109:
110: }
111:
112: }
|