001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.webapps.session.xml;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.util.Properties;
023:
024: import javax.xml.transform.OutputKeys;
025:
026: import org.apache.avalon.framework.component.ComponentException;
027: import org.apache.avalon.framework.component.ComponentManager;
028: import org.apache.avalon.framework.component.ComponentSelector;
029: import org.apache.avalon.framework.parameters.Parameters;
030: import org.apache.cocoon.ProcessingException;
031: import org.apache.cocoon.components.CocoonComponentManager;
032: import org.apache.cocoon.components.source.SourceUtil;
033: import org.apache.cocoon.serialization.Serializer;
034: import org.apache.cocoon.xml.XMLUtils;
035: import org.apache.cocoon.xml.dom.DOMStreamer;
036: import org.apache.excalibur.source.ModifiableSource;
037: import org.apache.excalibur.source.Source;
038: import org.apache.excalibur.source.SourceException;
039: import org.apache.excalibur.source.SourceParameters;
040: import org.apache.excalibur.source.SourceResolver;
041: import org.w3c.dom.DocumentFragment;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.helpers.DefaultHandler;
044:
045: /**
046: * A utility class which will soon be removed...
047: *
048: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
049: * @deprecated This block is deprecated and will be removed in future versions.
050: * @version CVS $Id: XMLUtil.java 485224 2006-12-10 17:24:05Z cziegeler $
051: */
052: public final class XMLUtil {
053:
054: /**
055: * Convert umlaute to entities
056: */
057: public static String encode(String value) {
058: StringBuffer buffer = new StringBuffer(value);
059: for (int i = 0; i < buffer.length(); i++) {
060: if (buffer.charAt(i) > 127) {
061: buffer.replace(i, i + 1, "__"
062: + ((int) buffer.charAt(i)) + ";");
063: }
064: }
065: return buffer.toString();
066: }
067:
068: /**
069: * Convert entities to umlaute
070: */
071: public static String decode(String value) {
072: StringBuffer buffer = new StringBuffer(value);
073: int pos;
074: boolean found;
075: for (int i = 0; i < buffer.length(); i++) {
076: if (buffer.charAt(i) == '_' && buffer.charAt(i + 1) == '_') {
077: pos = i + 2;
078: found = false;
079: while (buffer.charAt(pos) >= '0'
080: && buffer.charAt(pos) <= '9') {
081: found = true;
082: pos++;
083: }
084: if (found == true && pos > i + 2
085: && buffer.charAt(pos) == ';') {
086: int ent = new Integer(buffer.substring(i + 2, pos))
087: .intValue();
088: buffer.replace(i, pos + 1, "" + (char) ent);
089: }
090: }
091: }
092: return buffer.toString();
093: }
094:
095: /**
096: * Write a DOM Fragment to a source.
097: * If the source is a ModifiableSource the interface is used.
098: * If not, the source is invoked with an additional parameter named
099: * "content" containing the XML.
100: *
101: * @param location URI of the Source
102: * @param typeParameters Type of Source query. Currently, only
103: * <code>method</code> parameter (value typically <code>GET</code> or
104: * <code>POST</code>) is recognized. May be <code>null</code>.
105: * @param parameters Parameters (e.g. URL params) of the source.
106: * May be <code>null</code>
107: * @param frag DOM fragment to serialize to the Source
108: * @param resolver Resolver for the source.
109: * @param serializerName The serializer to use
110: * @throws ProcessingException
111: */
112: public static void writeDOM(String location,
113: Parameters typeParameters, SourceParameters parameters,
114: DocumentFragment frag, SourceResolver resolver,
115: String serializerName) throws ProcessingException {
116: Source source = null;
117:
118: try {
119: source = SourceUtil.getSource(location, typeParameters,
120: parameters, resolver);
121: if (source instanceof ModifiableSource) {
122: ModifiableSource ws = (ModifiableSource) source;
123:
124: frag.normalize();
125:
126: if (null != serializerName) {
127: ComponentManager manager = CocoonComponentManager
128: .getSitemapComponentManager();
129:
130: ComponentSelector selector = null;
131: Serializer serializer = null;
132: OutputStream oStream = null;
133: try {
134: selector = (ComponentSelector) manager
135: .lookup(Serializer.ROLE + "Selector");
136: serializer = (Serializer) selector
137: .select(serializerName);
138: oStream = ws.getOutputStream();
139: serializer.setOutputStream(oStream);
140: serializer.startDocument();
141: DOMStreamer streamer = new DOMStreamer(
142: serializer);
143: streamer.stream(frag);
144: serializer.endDocument();
145: } catch (ComponentException e) {
146: throw new ProcessingException(
147: "Unable to lookup serializer.", e);
148: } finally {
149: if (oStream != null) {
150: oStream.flush();
151: try {
152: oStream.close();
153: } catch (Exception ignore) {
154: }
155: }
156: if (selector != null) {
157: selector.release(serializer);
158: manager.release(selector);
159: }
160: }
161: } else {
162: Properties props = XMLUtils
163: .createPropertiesForXML(false);
164: props.put(OutputKeys.ENCODING, "ISO-8859-1");
165: final String content = XMLUtils.serializeNode(frag,
166: props);
167: OutputStream oStream = ws.getOutputStream();
168:
169: oStream.write(content.getBytes());
170: oStream.flush();
171: oStream.close();
172: }
173: } else {
174: String content;
175: if (null != serializerName) {
176: ComponentManager manager = CocoonComponentManager
177: .getSitemapComponentManager();
178:
179: ComponentSelector selector = null;
180: Serializer serializer = null;
181: ByteArrayOutputStream oStream = new ByteArrayOutputStream();
182: try {
183: selector = (ComponentSelector) manager
184: .lookup(Serializer.ROLE + "Selector");
185: serializer = (Serializer) selector
186: .select(serializerName);
187: serializer.setOutputStream(oStream);
188: serializer.startDocument();
189: DOMStreamer streamer = new DOMStreamer(
190: serializer);
191: streamer.stream(frag);
192: serializer.endDocument();
193: } catch (ComponentException e) {
194: throw new ProcessingException(
195: "Unable to lookup serializer.", e);
196: } finally {
197: oStream.flush();
198: try {
199: oStream.close();
200: } catch (Exception ignore) {
201: // do nothing
202: }
203: if (selector != null) {
204: selector.release(serializer);
205: manager.release(selector);
206: }
207: }
208: content = oStream.toString();
209: } else {
210: Properties props = XMLUtils
211: .createPropertiesForXML(false);
212: props.put(OutputKeys.ENCODING, "ISO-8859-1");
213: content = XMLUtils.serializeNode(frag, props);
214: }
215:
216: if (parameters == null) {
217: parameters = new SourceParameters();
218: } else {
219: parameters = (SourceParameters) parameters.clone();
220: }
221: parameters.setSingleParameterValue("content", content);
222:
223: source = SourceUtil.getSource(location, typeParameters,
224: parameters, resolver);
225: SourceUtil.toSAX(source, new DefaultHandler());
226: }
227: } catch (SourceException e) {
228: throw SourceUtil.handle(e);
229: } catch (IOException e) {
230: throw new ProcessingException(e);
231: } catch (SAXException e) {
232: throw new ProcessingException(e);
233: } finally {
234: resolver.release(source);
235: }
236: }
237: }
|