View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
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  package net.sf.exorcist.core;
17  
18  import org.apache.xerces.util.XMLChar;
19  import org.xml.sax.Attributes;
20  import org.xml.sax.ContentHandler;
21  import org.xml.sax.SAXException;
22  import org.xml.sax.helpers.AttributesImpl;
23  
24  /***
25   * TODO
26   */
27  public class XmlWriter {
28  
29      private String namespace;
30  
31      private String prefix;
32  
33      private ContentHandler handler;
34  
35      public void setNamespace(String namespace) {
36          this.namespace = namespace;
37      }
38  
39      public void setPrefix(String prefix) {
40          this.prefix = prefix;
41      }
42  
43      public void setContentHandler(ContentHandler handler) {
44          this.handler = handler;
45      }
46  
47      public void startDocument() throws SAXException {
48          handler.startDocument();
49          handler.startPrefixMapping(prefix, namespace);
50      }
51  
52      public void endDocument() throws SAXException {
53          handler.endPrefixMapping(prefix);
54          handler.endDocument();
55      }
56  
57      public void addElement(String name, String value) throws SAXException {
58          startElement(name, null);
59          characters(value);
60          endElement(name);
61      }
62  
63      public void startElement(String name, Attributes attributes)
64              throws SAXException {
65          if (prefix == null || prefix.length() == 0) {
66              handler.startElement(namespace, name, name, attributes);
67          } else {
68              handler.startElement(
69                      namespace, name, prefix + ":" + name, attributes);
70          }
71      }
72  
73      public void endElement(String name) throws SAXException {
74          if (prefix == null || prefix.length() == 0) {
75              handler.endElement(namespace, name, name);
76          } else {
77              handler.endElement(namespace, name, prefix + ":" + name);
78          }
79      }
80  
81      public Attributes makeAttribute(String name, String value) {
82          AttributesImpl attribute = new AttributesImpl();
83          if (value != null) {
84              value = new String(makeCharacters(value));
85              if (prefix == null || prefix.length() == 0) {
86                  attribute.addAttribute(namespace, name, name, "CDATA", value);
87              } else {
88                  attribute.addAttribute(
89                          namespace, name, prefix + ":" + name, "CDATA", value);
90              }
91          }
92          return attribute;
93      }
94  
95      public void characters(String value) throws SAXException {
96          if (value != null) {
97              char[] characters = makeCharacters(value);
98              handler.characters(characters, 0, characters.length);
99          }
100     }
101 
102     private char[] makeCharacters(String value) {
103         char[] characters = value.toCharArray();
104         for (int i = 0; i < characters.length; i++) {
105             if (XMLChar.isInvalid(characters[i])) {
106                 characters[i] = ' ';
107             }
108         }
109         return characters;
110     }
111 
112 }