Source Code Cross Referenced for XMLUtil.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » webapps » session » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.cocoon.webapps.session.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.