01: /*
02: * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.wso2.esb.transport.util;
18:
19: import org.apache.axiom.om.OMElement;
20: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
21: import org.apache.axis2.context.ConfigurationContext;
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: import javax.xml.stream.*;
26: import java.io.ByteArrayInputStream;
27: import java.io.ByteArrayOutputStream;
28: import java.io.OutputStream;
29:
30: /**
31: *
32: */
33: public class RequestProcessorUtil {
34: private static Log log = LogFactory
35: .getLog(RequestProcessorUtil.class);
36:
37: /**
38: * @param byteArrayOutStream
39: * @param out
40: * @param annotatedXsl
41: * @param contextRoot
42: * @param annotation : If annotation is false PI would not be attached.
43: */
44: public static void writeDocument(
45: ByteArrayOutputStream byteArrayOutStream, OutputStream out,
46: String annotatedXsl, String contextRoot, boolean annotation) {
47: XMLStreamWriter writer;
48: try {
49: ByteArrayInputStream bais = new ByteArrayInputStream(
50: byteArrayOutStream.toByteArray());
51: XMLStreamReader reader = XMLInputFactory.newInstance()
52: .createXMLStreamReader(bais);
53: StAXOMBuilder builder = new StAXOMBuilder(reader);
54: OMElement docElem = builder.getDocumentElement();
55: writer = XMLOutputFactory.newInstance()
56: .createXMLStreamWriter(out);
57: if (annotatedXsl != null && annotation) {
58: writer.writeProcessingInstruction("xml-stylesheet",
59: " type=\"text/xsl\" href=\""
60: + (contextRoot.equals("/") ? ""
61: : contextRoot) + "/styles/"
62: + annotatedXsl + "\"");
63: }
64: docElem.serialize(writer);
65: writer.flush();
66: } catch (XMLStreamException e) {
67: log.error(e);
68: }
69: }
70:
71: public static String getServiceContextPath(
72: ConfigurationContext configCtx) {
73: String serviceContextPath = configCtx.getServiceContextPath();
74: if (!configCtx.getContextRoot().equals("/")
75: && !serviceContextPath.startsWith("/")) {
76: serviceContextPath = "/" + serviceContextPath;
77: }
78: return serviceContextPath;
79: }
80: }
|