1 /* SAXSerializer.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu
.xml
.transform
;
40 import java
.util
.HashMap
;
41 import java
.util
.Iterator
;
42 import java
.util
.LinkedList
;
43 import org
.w3c
.dom
.Attr
;
44 import org
.w3c
.dom
.DocumentType
;
45 import org
.w3c
.dom
.NamedNodeMap
;
46 import org
.w3c
.dom
.Node
;
47 import org
.xml
.sax
.Attributes
;
48 import org
.xml
.sax
.ContentHandler
;
49 import org
.xml
.sax
.SAXException
;
50 import org
.xml
.sax
.ext
.LexicalHandler
;
53 * Serializes a DOM node to a sequence of SAX events.
55 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
61 transient NamedNodeMap attrs
;
62 transient LinkedList namespaces
= new LinkedList();
64 boolean isDefined(String prefix
, String uri
)
66 for (Iterator i
= namespaces
.iterator(); i
.hasNext(); )
68 HashMap ctx
= (HashMap
) i
.next();
69 if (uri
.equals(ctx
.get(prefix
)))
77 void define(String prefix
, String uri
)
79 for (Iterator i
= namespaces
.iterator(); i
.hasNext(); )
81 HashMap ctx
= (HashMap
) i
.next();
82 if (ctx
.containsKey(prefix
))
84 HashMap newCtx
= new HashMap();
85 newCtx
.put(prefix
, uri
);
86 namespaces
.addFirst(newCtx
);
91 if (namespaces
.isEmpty())
98 ctx
= (HashMap
) namespaces
.getFirst();
100 ctx
.put(prefix
, uri
);
103 void undefine(String prefix
, String uri
)
105 for (Iterator i
= namespaces
.iterator(); i
.hasNext(); )
107 HashMap ctx
= (HashMap
) i
.next();
108 if (uri
.equals(ctx
.get(prefix
)))
113 namespaces
.remove(ctx
);
120 public int getLength()
122 return attrs
.getLength();
125 public String
getURI(int index
)
127 return attrs
.item(index
).getNamespaceURI();
130 public String
getLocalName(int index
)
132 return attrs
.item(index
).getLocalName();
135 public String
getQName(int index
)
137 return attrs
.item(index
).getNodeName();
140 public String
getType(int index
)
142 Attr attr
= (Attr
) attrs
.item(index
);
143 return attr
.isId() ?
"ID" : "CDATA";
146 public String
getValue(int index
)
148 return attrs
.item(index
).getNodeValue();
151 public int getIndex(String uri
, String localName
)
153 int len
= attrs
.getLength();
154 for (int i
= 0; i
< len
; i
++)
156 Node attr
= attrs
.item(i
);
157 String a_uri
= attr
.getNamespaceURI();
158 String a_localName
= attr
.getLocalName();
159 if (((a_uri
== null && uri
== null) ||
160 (a_uri
!= null && a_uri
.equals(uri
))) &&
161 a_localName
.equals(localName
))
169 public int getIndex(String qName
)
171 int len
= attrs
.getLength();
172 for (int i
= 0; i
< len
; i
++)
174 Node attr
= attrs
.item(i
);
175 String a_name
= attr
.getNodeName();
176 if (a_name
.equals(qName
))
184 public String
getType(String uri
, String localName
)
186 Attr attr
= (Attr
) attrs
.getNamedItemNS(uri
, localName
);
187 return attr
.isId() ?
"ID" : "CDATA";
190 public String
getType(String qName
)
192 Attr attr
= (Attr
) attrs
.getNamedItem(qName
);
193 return attr
.isId() ?
"ID" : "CDATA";
196 public String
getValue(String uri
, String localName
)
198 return attrs
.getNamedItemNS(uri
, localName
).getNodeValue();
201 public String
getValue(String qName
)
203 return attrs
.getNamedItem(qName
).getNodeValue();
206 void serialize(Node node
, ContentHandler ch
, LexicalHandler lh
)
209 attrs
= node
.getAttributes();
211 Node next
= node
.getNextSibling();
212 switch (node
.getNodeType())
214 case Node
.ELEMENT_NODE
:
215 String uri
= node
.getNamespaceURI();
216 String prefix
= node
.getPrefix();
217 boolean defined
= isDefined(prefix
, uri
);
221 ch
.startPrefixMapping(prefix
, uri
);
223 String localName
= node
.getLocalName();
224 String qName
= node
.getNodeName();
225 ch
.startElement(uri
, localName
, qName
, this);
226 children
= node
.getFirstChild();
227 if (children
!= null)
229 serialize(children
, ch
, lh
);
231 ch
.endElement(uri
, localName
, qName
);
234 ch
.endPrefixMapping(prefix
);
235 undefine(prefix
, uri
);
239 char[] chars
= node
.getNodeValue().toCharArray();
240 ch
.characters(chars
, 0, chars
.length
);
242 case Node
.CDATA_SECTION_NODE
:
243 char[] cdata
= node
.getNodeValue().toCharArray();
247 ch
.characters(cdata
, 0, cdata
.length
);
252 ch
.characters(cdata
, 0, cdata
.length
);
255 case Node
.COMMENT_NODE
:
258 char[] comment
= node
.getNodeValue().toCharArray();
259 lh
.comment(comment
, 0, comment
.length
);
262 case Node
.DOCUMENT_NODE
:
263 case Node
.DOCUMENT_FRAGMENT_NODE
:
265 children
= node
.getFirstChild();
266 if (children
!= null)
268 serialize(children
, ch
, lh
);
272 case Node
.DOCUMENT_TYPE_NODE
:
275 DocumentType doctype
= (DocumentType
) node
;
276 String publicId
= doctype
.getPublicId();
277 String systemId
= doctype
.getSystemId();
278 lh
.startDTD(node
.getNodeName(), publicId
, systemId
);
279 NamedNodeMap entities
= doctype
.getEntities();
280 int len
= entities
.getLength();
281 for (int i
= 0; i
< len
; i
++)
283 Node entity
= entities
.item(i
);
284 String entityName
= entity
.getNodeName();
285 lh
.startEntity(entityName
);
286 lh
.endEntity(entityName
);
291 case Node
.PROCESSING_INSTRUCTION_NODE
:
292 ch
.processingInstruction(node
.getNodeName(), node
.getNodeValue());
294 case Node
.ENTITY_REFERENCE_NODE
:
295 ch
.skippedEntity(node
.getNodeName());
301 serialize(next
, ch
, lh
);