/[classpath]/classpath/gnu/xml/stream/XMLStreamWriterImpl.java
ViewVC logotype

Diff of /classpath/gnu/xml/stream/XMLStreamWriterImpl.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.2.1 by gnu_andrew, Sat Sep 10 15:31:38 2005 UTC revision 1.1.2.2 by gnu_andrew, Sun Nov 27 21:00:36 2005 UTC
# Line 39  package gnu.xml.stream; Line 39  package gnu.xml.stream;
39    
40  import java.io.IOException;  import java.io.IOException;
41  import java.io.Writer;  import java.io.Writer;
42    import java.util.Enumeration;
43    import java.util.HashSet;
44  import java.util.LinkedList;  import java.util.LinkedList;
45    import java.util.Set;
46    
47  import javax.xml.XMLConstants;  import javax.xml.XMLConstants;
48  import javax.xml.namespace.NamespaceContext;  import javax.xml.namespace.NamespaceContext;
# Line 57  public class XMLStreamWriterImpl Line 60  public class XMLStreamWriterImpl
60    implements XMLStreamWriter    implements XMLStreamWriter
61  {  {
62    
63      /**
64       * The underlying character stream to write to.
65       */
66    protected final Writer writer;    protected final Writer writer;
67    
68      /**
69       * The encoding being used.
70       * Note that this must match the encoding of the character stream.
71       */
72    protected final String encoding;    protected final String encoding;
73    
74      /**
75       * Whether prefix defaulting is being used.
76       * If true and a prefix has not been defined for a namespace specified on
77       * an element or an attribute, a new prefix and namespace declaration will
78       * be created.
79       */
80    protected final boolean prefixDefaulting;    protected final boolean prefixDefaulting;
81    
82      /**
83       * The namespace context used to determine the namespace-prefix mappings
84       * in scope.
85       */
86    protected NamespaceContext namespaceContext;    protected NamespaceContext namespaceContext;
87        
88      /**
89       * The stack of elements in scope.
90       * Used to close the remaining elements.
91       */
92    private LinkedList elements;    private LinkedList elements;
93    
94      /**
95       * Whether a start element has been opened but not yet closed.
96       */
97    private boolean inStartElement;    private boolean inStartElement;
98    
99      /**
100       * Whether we are in an empty element.
101       */
102    private boolean emptyElement;    private boolean emptyElement;
103      
104    private NamespaceSupport namespaces;    private NamespaceSupport namespaces;
105      private int count = 0;
106    
107      /**
108       * Constructor.
109       * @see #writer
110       * @see #encoding
111       * @see #prefixDefaulting
112       */
113    protected XMLStreamWriterImpl(Writer writer, String encoding,    protected XMLStreamWriterImpl(Writer writer, String encoding,
114                                  boolean prefixDefaulting)                                  boolean prefixDefaulting)
115    {    {
# Line 77  public class XMLStreamWriterImpl Line 120  public class XMLStreamWriterImpl
120      namespaces = new NamespaceSupport();      namespaces = new NamespaceSupport();
121    }    }
122    
123      /**
124       * Write the end of a start-element event.
125       * This will close the element if it was defined to be an empty element.
126       */
127    private void endStartElement()    private void endStartElement()
128      throws IOException      throws IOException
129    {    {
# Line 128  public class XMLStreamWriterImpl Line 175  public class XMLStreamWriterImpl
175          if (!isDeclared)          if (!isDeclared)
176            {            {
177              if (prefixDefaulting)              if (prefixDefaulting)
178                prefix = XMLConstants.DEFAULT_NS_PREFIX;                prefix = createPrefix(namespaceURI);
179              else              else
180                throw new XMLStreamException("namespace " + namespaceURI +                throw new XMLStreamException("namespace " + namespaceURI +
181                                             " has not been declared");                                             " has not been declared");
# Line 140  public class XMLStreamWriterImpl Line 187  public class XMLStreamWriterImpl
187              writer.write(':');              writer.write(':');
188            }            }
189          writer.write(localName);          writer.write(localName);
190          if (prefixDefaulting && !isDeclared)          inStartElement = true;
191            if (!isDeclared)
192            {            {
193              writeNamespace(prefix, namespaceURI);              writeNamespace(prefix, namespaceURI);
194            }            }
195                    
196          elements.addLast(new String[] { prefix, localName });          elements.addLast(new String[] { prefix, localName });
         inStartElement = true;  
197        }        }
198      catch (IOException e)      catch (IOException e)
199        {        {
# Line 156  public class XMLStreamWriterImpl Line 203  public class XMLStreamWriterImpl
203        }        }
204    }    }
205    
206      /**
207       * Creates a new unique prefix in the document.
208       * Subclasses may override this method to provide a suitably unique prefix
209       * for the given namespace.
210       * @param namespaceURI the namespace URI
211       */
212      protected String createPrefix(String namespaceURI)
213      {
214        Set prefixes = new HashSet();
215        for (Enumeration e = namespaces.getPrefixes(); e.hasMoreElements(); )
216          prefixes.add(e.nextElement());
217        String ret;
218        do
219          {
220            ret = "ns" + (count++);
221          }
222        while (prefixes.contains(ret));
223        return ret;
224      }
225    
226    public void writeStartElement(String prefix, String localName,    public void writeStartElement(String prefix, String localName,
227                                  String namespaceURI)                                  String namespaceURI)
228      throws XMLStreamException      throws XMLStreamException
# Line 656  public class XMLStreamWriterImpl Line 723  public class XMLStreamWriterImpl
723      throw new IllegalArgumentException(name);      throw new IllegalArgumentException(name);
724    }    }
725    
726      /**
727       * Write the specified text, ensuring that the content is suitably encoded
728       * for XML.
729       * @param text the text to write
730       * @param inAttr whether we are in an attribute value
731       */
732    private void writeEncoded(String text, boolean inAttr)    private void writeEncoded(String text, boolean inAttr)
733      throws IOException      throws IOException
734    {    {

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26