2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package gr.ebs.gss.server.webdav;
20 import java.io.OutputStreamWriter;
21 import java.io.PrintWriter;
22 import java.io.UnsupportedEncodingException;
23 import java.io.Writer;
25 import org.w3c.dom.Attr;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.NamedNodeMap;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
32 * A sample DOM writer. This sample program illustrates how to
33 * traverse a DOM tree in order to print a document that is parsed.
35 public class DOMWriter {
41 /** Default Encoding */
43 PRINTWRITER_ENCODING = "UTF8";
48 private static String MIME2JAVA_ENCODINGS[] =
49 { "Default", "UTF-8", "US-ASCII", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4",
50 "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-9", "ISO-2022-JP",
51 "SHIFT_JIS", "EUC-JP","GB2312", "BIG5", "EUC-KR", "ISO-2022-KR", "KOI8-R", "EBCDIC-CP-US",
52 "EBCDIC-CP-CA", "EBCDIC-CP-NL", "EBCDIC-CP-DK", "EBCDIC-CP-NO", "EBCDIC-CP-FI", "EBCDIC-CP-SE",
53 "EBCDIC-CP-IT", "EBCDIC-CP-ES", "EBCDIC-CP-GB", "EBCDIC-CP-FR", "EBCDIC-CP-AR1",
54 "EBCDIC-CP-HE", "EBCDIC-CP-CH", "EBCDIC-CP-ROECE","EBCDIC-CP-YU",
55 "EBCDIC-CP-IS", "EBCDIC-CP-AR2", "UTF-16"
58 /** Output qualified names */
59 private boolean qualifiedNames = true;
62 protected PrintWriter out;
64 /** Canonical output. */
65 protected boolean canonical;
71 * @throws UnsupportedEncodingException
73 public DOMWriter(String encoding, boolean _canonical)
74 throws UnsupportedEncodingException {
75 out = new PrintWriter(new OutputStreamWriter(System.out, encoding));
76 canonical = _canonical;
77 } // <init>(String,boolean)
83 /** Default constructor.
85 * @throws UnsupportedEncodingException
87 public DOMWriter(boolean _canonical) throws UnsupportedEncodingException {
88 this( getWriterEncoding(), _canonical);
95 public DOMWriter(Writer writer, boolean _canonical) {
96 out = new PrintWriter(writer);
97 canonical = _canonical;
101 * @return the qualifiedNames
103 public boolean getQualifiedNames() {
104 return qualifiedNames;
108 * @param _qualifiedNames
110 public void setQualifiedNames(boolean _qualifiedNames) {
111 qualifiedNames = _qualifiedNames;
115 * @return the PrintWriter encoding
117 public static String getWriterEncoding( ) {
118 return PRINTWRITER_ENCODING;
119 }// getWriterEncoding
124 public static void setWriterEncoding( String encoding ) {
125 if( encoding.equalsIgnoreCase( "DEFAULT" ) )
126 PRINTWRITER_ENCODING = "UTF8";
127 else if( encoding.equalsIgnoreCase( "UTF-16" ) )
128 PRINTWRITER_ENCODING = "Unicode";
130 PRINTWRITER_ENCODING = MIME2Java.convert( encoding );
131 }// setWriterEncoding
136 * @return true if the encoding is valid
138 public static boolean isValidJavaEncoding( String encoding ) {
139 for ( int i = 0; i < MIME2JAVA_ENCODINGS.length; i++ )
140 if ( encoding.equals( MIME2JAVA_ENCODINGS[i] ) )
144 }// isValidJavaEncoding
147 /** Prints the specified node, recursively.
150 public void print(Node node) {
152 // is there anything to do?
156 int type = node.getNodeType();
159 case Node.DOCUMENT_NODE: {
161 String Encoding = getWriterEncoding();
162 if( Encoding.equalsIgnoreCase( "DEFAULT" ) )
164 else if( Encoding.equalsIgnoreCase( "Unicode" ) )
167 Encoding = MIME2Java.reverse( Encoding );
169 out.println("<?xml version=\"1.0\" encoding=\""+
172 print(((Document)node).getDocumentElement());
177 // print element with attributes
178 case Node.ELEMENT_NODE: {
181 out.print(node.getNodeName());
183 out.print(node.getLocalName());
184 Attr attrs[] = sortAttributes(node.getAttributes());
185 for ( int i = 0; i < attrs.length; i++ ) {
186 Attr attr = attrs[i];
189 out.print(attr.getNodeName());
191 out.print(attr.getLocalName());
194 out.print(normalize(attr.getNodeValue()));
198 NodeList children = node.getChildNodes();
199 if ( children != null ) {
200 int len = children.getLength();
201 for ( int i = 0; i < len; i++ )
202 print(children.item(i));
207 // handle entity reference nodes
208 case Node.ENTITY_REFERENCE_NODE: {
210 NodeList children = node.getChildNodes();
211 if ( children != null ) {
212 int len = children.getLength();
213 for ( int i = 0; i < len; i++ )
214 print(children.item(i));
219 out.print(node.getNodeName());
221 out.print(node.getLocalName());
227 // print cdata sections
228 case Node.CDATA_SECTION_NODE: {
230 out.print(normalize(node.getNodeValue()));
232 out.print("<![CDATA[");
233 out.print(node.getNodeValue());
240 case Node.TEXT_NODE: {
241 out.print(normalize(node.getNodeValue()));
245 // print processing instruction
246 case Node.PROCESSING_INSTRUCTION_NODE: {
249 out.print(node.getNodeName());
251 out.print(node.getLocalName());
253 String data = node.getNodeValue();
254 if ( data != null && data.length() > 0 ) {
263 if ( type == Node.ELEMENT_NODE ) {
266 out.print(node.getNodeName());
268 out.print(node.getLocalName());
276 /** Returns a sorted list of attributes.
280 protected Attr[] sortAttributes(NamedNodeMap attrs) {
282 int len = attrs != null ? attrs.getLength() : 0;
283 Attr array[] = new Attr[len];
284 for ( int i = 0; i < len; i++ )
285 array[i] = (Attr)attrs.item(i);
286 for ( int i = 0; i < len - 1; i++ ) {
289 name = array[i].getNodeName();
291 name = array[i].getLocalName();
293 for ( int j = i + 1; j < len; j++ ) {
294 String curName = null;
296 curName = array[j].getNodeName();
298 curName = array[j].getLocalName();
299 if ( curName.compareTo(name) < 0 ) {
305 Attr temp = array[i];
306 array[i] = array[index];
313 } // sortAttributes(NamedNodeMap):Attr[]
316 /** Normalizes the given string.
320 @SuppressWarnings("fallthrough")
321 protected String normalize(String s) {
322 StringBuffer str = new StringBuffer();
324 int len = s != null ? s.length() : 0;
325 for ( int i = 0; i < len; i++ ) {
326 char ch = s.charAt(i);
341 str.append(""");
348 str.append(Integer.toString(ch));
352 // else, default append char
360 return str.toString();
362 } // normalize(String):String
367 private static void printValidJavaEncoding() {
368 System.err.println( " ENCODINGS:" );
369 System.err.print( " " );
371 i < MIME2JAVA_ENCODINGS.length; i++) {
372 System.err.print( MIME2JAVA_ENCODINGS[i] + " " );
374 System.err.println();
375 System.err.print( " " );
379 } // printJavaEncoding()