OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [xml/] [transform/] [SAXSerializer.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* SAXSerializer.java --
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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)
9
any later version.
10
 
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.
15
 
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
19
02110-1301 USA.
20
 
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
24
combination.
25
 
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. */
37
 
38
package gnu.xml.transform;
39
 
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;
51
 
52
/**
53
 * Serializes a DOM node to a sequence of SAX events.
54
 *
55
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56
 */
57
class SAXSerializer
58
  implements Attributes
59
{
60
 
61
  transient NamedNodeMap attrs;
62
  transient LinkedList namespaces = new LinkedList();
63
 
64
  boolean isDefined(String prefix, String uri)
65
  {
66
    for (Iterator i = namespaces.iterator(); i.hasNext(); )
67
      {
68
        HashMap ctx = (HashMap) i.next();
69
        if (uri.equals(ctx.get(prefix)))
70
          {
71
            return true;
72
          }
73
      }
74
    return false;
75
  }
76
 
77
  void define(String prefix, String uri)
78
  {
79
    for (Iterator i = namespaces.iterator(); i.hasNext(); )
80
      {
81
        HashMap ctx = (HashMap) i.next();
82
        if (ctx.containsKey(prefix))
83
          {
84
            HashMap newCtx = new HashMap();
85
            newCtx.put(prefix, uri);
86
            namespaces.addFirst(newCtx);
87
            return;
88
          }
89
      }
90
    HashMap ctx;
91
    if (namespaces.isEmpty())
92
      {
93
        ctx = new HashMap();
94
        namespaces.add(ctx);
95
      }
96
    else
97
      {
98
        ctx = (HashMap) namespaces.getFirst();
99
      }
100
    ctx.put(prefix, uri);
101
  }
102
 
103
  void undefine(String prefix, String uri)
104
  {
105
    for (Iterator i = namespaces.iterator(); i.hasNext(); )
106
      {
107
        HashMap ctx = (HashMap) i.next();
108
        if (uri.equals(ctx.get(prefix)))
109
          {
110
            ctx.remove(prefix);
111
            if (ctx.isEmpty())
112
              {
113
                namespaces.remove(ctx);
114
              }
115
            return;
116
          }
117
      }
118
  }
119
 
120
  public int getLength()
121
  {
122
    return attrs.getLength();
123
  }
124
 
125
  public String getURI(int index)
126
  {
127
    return attrs.item(index).getNamespaceURI();
128
  }
129
 
130
  public String getLocalName(int index)
131
  {
132
    return attrs.item(index).getLocalName();
133
  }
134
 
135
  public String getQName(int index)
136
  {
137
    return attrs.item(index).getNodeName();
138
  }
139
 
140
  public String getType(int index)
141
  {
142
    Attr attr = (Attr) attrs.item(index);
143
    return attr.isId() ? "ID" : "CDATA";
144
  }
145
 
146
  public String getValue(int index)
147
  {
148
    return attrs.item(index).getNodeValue();
149
  }
150
 
151
  public int getIndex(String uri, String localName)
152
  {
153
    int len = attrs.getLength();
154
    for (int i = 0; i < len; i++)
155
      {
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))
162
          {
163
            return i;
164
          }
165
      }
166
    return -1;
167
  }
168
 
169
  public int getIndex(String qName)
170
  {
171
    int len = attrs.getLength();
172
    for (int i = 0; i < len; i++)
173
      {
174
        Node attr = attrs.item(i);
175
        String a_name = attr.getNodeName();
176
        if (a_name.equals(qName))
177
          {
178
            return i;
179
          }
180
      }
181
    return -1;
182
  }
183
 
184
  public String getType(String uri, String localName)
185
  {
186
    Attr attr = (Attr) attrs.getNamedItemNS(uri, localName);
187
    return attr.isId() ? "ID" : "CDATA";
188
  }
189
 
190
  public String getType(String qName)
191
  {
192
    Attr attr = (Attr) attrs.getNamedItem(qName);
193
    return attr.isId() ? "ID" : "CDATA";
194
  }
195
 
196
  public String getValue(String uri, String localName)
197
  {
198
    return attrs.getNamedItemNS(uri, localName).getNodeValue();
199
  }
200
 
201
  public String getValue(String qName)
202
  {
203
    return attrs.getNamedItem(qName).getNodeValue();
204
  }
205
 
206
  void serialize(Node node, ContentHandler ch, LexicalHandler lh)
207
    throws SAXException
208
  {
209
    attrs = node.getAttributes();
210
    Node children;
211
    Node next = node.getNextSibling();
212
    switch (node.getNodeType())
213
      {
214
      case Node.ELEMENT_NODE:
215
        String uri = node.getNamespaceURI();
216
        String prefix = node.getPrefix();
217
        boolean defined = isDefined(prefix, uri);
218
        if (!defined)
219
          {
220
            define(prefix, uri);
221
            ch.startPrefixMapping(prefix, uri);
222
          }
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)
228
          {
229
            serialize(children, ch, lh);
230
          }
231
        ch.endElement(uri, localName, qName);
232
        if (!defined)
233
          {
234
            ch.endPrefixMapping(prefix);
235
            undefine(prefix, uri);
236
          }
237
        break;
238
      case Node.TEXT_NODE:
239
        char[] chars = node.getNodeValue().toCharArray();
240
        ch.characters(chars, 0, chars.length);
241
        break;
242
      case Node.CDATA_SECTION_NODE:
243
        char[] cdata = node.getNodeValue().toCharArray();
244
        if (lh != null)
245
          {
246
            lh.startCDATA();
247
            ch.characters(cdata, 0, cdata.length);
248
            lh.endCDATA();
249
          }
250
        else
251
          {
252
            ch.characters(cdata, 0, cdata.length);
253
          }
254
        break;
255
      case Node.COMMENT_NODE:
256
        if (lh != null)
257
          {
258
            char[] comment = node.getNodeValue().toCharArray();
259
            lh.comment(comment, 0, comment.length);
260
          }
261
        break;
262
      case Node.DOCUMENT_NODE:
263
      case Node.DOCUMENT_FRAGMENT_NODE:
264
        ch.startDocument();
265
        children = node.getFirstChild();
266
        if (children != null)
267
          {
268
            serialize(children, ch, lh);
269
          }
270
        ch.endDocument();
271
        break;
272
      case Node.DOCUMENT_TYPE_NODE:
273
        if (lh != null)
274
          {
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++)
282
              {
283
                Node entity = entities.item(i);
284
                String entityName = entity.getNodeName();
285
                lh.startEntity(entityName);
286
                lh.endEntity(entityName);
287
              }
288
            lh.endDTD();
289
          }
290
        break;
291
      case Node.PROCESSING_INSTRUCTION_NODE:
292
        ch.processingInstruction(node.getNodeName(), node.getNodeValue());
293
        break;
294
      case Node.ENTITY_REFERENCE_NODE:
295
        ch.skippedEntity(node.getNodeName());
296
        break;
297
      }
298
    attrs = null;
299
    if (next != null)
300
      {
301
        serialize(next, ch, lh);
302
      }
303
  }
304
 
305
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.