123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017 |
- /* XMLStreamWriterImpl.java --
- Copyright (C) 2005 Free Software Foundation, Inc.
- This file is part of GNU Classpath.
- GNU Classpath is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
- GNU Classpath is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNU Classpath; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301 USA.
- Linking this library statically or dynamically with other modules is
- making a combined work based on this library. Thus, the terms and
- conditions of the GNU General Public License cover the whole
- combination.
- As a special exception, the copyright holders of this library give you
- permission to link this library with independent modules to produce an
- executable, regardless of the license terms of these independent
- modules, and to copy and distribute the resulting executable under
- terms of your choice, provided that you also meet, for each linked
- independent module, the terms and conditions of the license of that
- module. An independent module is a module which is not derived from
- or based on this library. If you modify this library, you may extend
- this exception to your version of the library, but you are not
- obligated to do so. If you do not wish to do so, delete this
- exception statement from your version. */
- package gnu.xml.stream;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Enumeration;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Set;
- import javax.xml.XMLConstants;
- import javax.xml.namespace.NamespaceContext;
- import javax.xml.stream.XMLStreamException;
- import javax.xml.stream.XMLStreamWriter;
- import org.xml.sax.helpers.NamespaceSupport;
- /**
- * Simple XML stream writer.
- *
- * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
- */
- public class XMLStreamWriterImpl
- implements XMLStreamWriter
- {
- /**
- * The underlying character stream to write to.
- */
- protected final Writer writer;
- /**
- * The encoding being used.
- * Note that this must match the encoding of the character stream.
- */
- protected final String encoding;
- /**
- * Whether prefix defaulting is being used.
- * If true and a prefix has not been defined for a namespace specified on
- * an element or an attribute, a new prefix and namespace declaration will
- * be created.
- */
- protected final boolean prefixDefaulting;
- /**
- * The namespace context used to determine the namespace-prefix mappings
- * in scope.
- */
- protected NamespaceContext namespaceContext;
- /**
- * The stack of elements in scope.
- * Used to close the remaining elements.
- */
- private LinkedList elements;
- /**
- * Whether a start element has been opened but not yet closed.
- */
- private boolean inStartElement;
- /**
- * Whether we are in an empty element.
- */
- private boolean emptyElement;
- private NamespaceSupport namespaces;
- private int count = 0;
- private boolean xml11;
- private boolean hasXML11RestrictedChars;
- /**
- * Constructor.
- * @see #writer
- * @see #encoding
- * @see #prefixDefaulting
- */
- protected XMLStreamWriterImpl(Writer writer, String encoding,
- boolean prefixDefaulting)
- {
- this.writer = writer;
- this.encoding = encoding;
- this.prefixDefaulting = prefixDefaulting;
- elements = new LinkedList();
- namespaces = new NamespaceSupport();
- }
- /**
- * Write the end of a start-element event.
- * This will close the element if it was defined to be an empty element.
- */
- private void endStartElement()
- throws IOException
- {
- if (!inStartElement)
- return;
- if (emptyElement)
- {
- writer.write('/');
- elements.removeLast();
- namespaces.popContext();
- emptyElement = false;
- }
- writer.write('>');
- inStartElement = false;
- }
- public void writeStartElement(String localName)
- throws XMLStreamException
- {
- try
- {
- if (!isName(localName))
- throw new IllegalArgumentException("illegal Name: " + localName);
- endStartElement();
- namespaces.pushContext();
- writer.write('<');
- writer.write(localName);
- elements.addLast(new String[] { null, localName });
- inStartElement = true;
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeStartElement(String namespaceURI, String localName)
- throws XMLStreamException
- {
- try
- {
- if (namespaceURI != null && !isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- if (!isName(localName))
- throw new IllegalArgumentException("illegal Name: " + localName);
- endStartElement();
- namespaces.pushContext();
- String prefix = getPrefix(namespaceURI);
- boolean isDeclared = (prefix != null);
- if (!isDeclared)
- {
- if (prefixDefaulting)
- prefix = createPrefix(namespaceURI);
- else
- throw new XMLStreamException("namespace " + namespaceURI +
- " has not been declared");
- }
- writer.write('<');
- if (!"".equals(prefix))
- {
- writer.write(prefix);
- writer.write(':');
- }
- writer.write(localName);
- inStartElement = true;
- if (!isDeclared)
- {
- writeNamespaceImpl(prefix, namespaceURI);
- }
- elements.addLast(new String[] { prefix, localName });
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- /**
- * Creates a new unique prefix in the document.
- * Subclasses may override this method to provide a suitably unique prefix
- * for the given namespace.
- * @param namespaceURI the namespace URI
- */
- protected String createPrefix(String namespaceURI)
- {
- Set prefixes = new HashSet();
- for (Enumeration e = namespaces.getPrefixes(); e.hasMoreElements(); )
- prefixes.add(e.nextElement());
- String ret;
- do
- {
- ret = "ns" + (count++);
- }
- while (prefixes.contains(ret));
- return ret;
- }
- public void writeStartElement(String prefix, String localName,
- String namespaceURI)
- throws XMLStreamException
- {
- try
- {
- if (namespaceURI != null && !isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- if (prefix != null && !isPrefix(prefix))
- throw new IllegalArgumentException("illegal NCName: " + prefix);
- if (!isNCName(localName))
- throw new IllegalArgumentException("illegal NCName: " + localName);
- endStartElement();
- namespaces.pushContext();
- String currentPrefix = getPrefix(namespaceURI);
- boolean isCurrent = prefix.equals(currentPrefix);
- writer.write('<');
- if (!"".equals(prefix))
- {
- writer.write(prefix);
- writer.write(':');
- }
- writer.write(localName);
- if (prefixDefaulting && !isCurrent)
- {
- writeNamespaceImpl(prefix, namespaceURI);
- }
- elements.addLast(new String[] { prefix, localName });
- inStartElement = true;
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeEmptyElement(String namespaceURI, String localName)
- throws XMLStreamException
- {
- writeStartElement(namespaceURI, localName);
- emptyElement = true;
- }
- public void writeEmptyElement(String prefix, String localName,
- String namespaceURI)
- throws XMLStreamException
- {
- writeStartElement(prefix, localName, namespaceURI);
- emptyElement = true;
- }
- public void writeEmptyElement(String localName)
- throws XMLStreamException
- {
- writeStartElement(localName);
- emptyElement = true;
- }
- public void writeEndElement()
- throws XMLStreamException
- {
- if (elements.isEmpty())
- throw new IllegalStateException("no matching start element");
- try
- {
- endStartElement();
- String[] element = (String[]) elements.removeLast();
- namespaces.popContext();
- writer.write('<');
- writer.write('/');
- if (element[0] != null && !"".equals(element[0]))
- {
- writer.write(element[0]);
- writer.write(':');
- }
- writer.write(element[1]);
- writer.write('>');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeEndDocument()
- throws XMLStreamException
- {
- while (!elements.isEmpty())
- writeEndElement();
- }
- public void close()
- throws XMLStreamException
- {
- flush();
- }
- public void flush()
- throws XMLStreamException
- {
- try
- {
- writer.flush();
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeAttribute(String localName, String value)
- throws XMLStreamException
- {
- if (!inStartElement)
- throw new IllegalStateException();
- try
- {
- if (!isName(localName))
- throw new IllegalArgumentException("illegal Name: " + localName);
- if (!isChars(value))
- throw new IllegalArgumentException("illegal character: " + value);
- writer.write(' ');
- writer.write(localName);
- writer.write('=');
- writer.write('"');
- if (hasXML11RestrictedChars)
- writeEncodedWithRestrictedChars(value, true);
- else
- writeEncoded(value, true);
- writer.write('"');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeAttribute(String prefix, String namespaceURI,
- String localName, String value)
- throws XMLStreamException
- {
- if (!inStartElement)
- throw new IllegalStateException();
- try
- {
- if (namespaceURI != null && !isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- if (prefix != null && !isPrefix(prefix))
- throw new IllegalArgumentException("illegal NCName: " + prefix);
- if (!isNCName(localName))
- throw new IllegalArgumentException("illegal NCName: " + localName);
- if (!isChars(value))
- throw new IllegalArgumentException("illegal character: " + value);
- String currentPrefix = getPrefix(namespaceURI);
- if (currentPrefix == null)
- {
- if (prefixDefaulting)
- writeNamespaceImpl(prefix, namespaceURI);
- else
- throw new XMLStreamException("namespace " + namespaceURI +
- " is not bound");
- }
- else if (!currentPrefix.equals(prefix))
- throw new XMLStreamException("namespace " + namespaceURI +
- " is bound to prefix " +
- currentPrefix);
- writer.write(' ');
- if (!"".equals(prefix))
- {
- writer.write(prefix);
- writer.write(':');
- }
- writer.write(localName);
- writer.write('=');
- writer.write('"');
- if (hasXML11RestrictedChars)
- writeEncodedWithRestrictedChars(value, true);
- else
- writeEncoded(value, true);
- writer.write('"');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeAttribute(String namespaceURI, String localName,
- String value)
- throws XMLStreamException
- {
- if (!inStartElement)
- throw new IllegalStateException();
- try
- {
- if (namespaceURI != null && !isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- if (!isName(localName))
- throw new IllegalArgumentException("illegal Name: " + localName);
- if (!isChars(value))
- throw new IllegalArgumentException("illegal character: " + value);
- String prefix = getPrefix(namespaceURI);
- if (prefix == null)
- {
- if (prefixDefaulting)
- {
- prefix = XMLConstants.DEFAULT_NS_PREFIX;
- writeNamespaceImpl(prefix, namespaceURI);
- }
- else
- throw new XMLStreamException("namespace " + namespaceURI +
- " is not bound");
- }
- writer.write(' ');
- if (!"".equals(prefix))
- {
- writer.write(prefix);
- writer.write(':');
- }
- writer.write(localName);
- writer.write('=');
- writer.write('"');
- if (hasXML11RestrictedChars)
- writeEncodedWithRestrictedChars(value, true);
- else
- writeEncoded(value, true);
- writer.write('"');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeNamespace(String prefix, String namespaceURI)
- throws XMLStreamException
- {
- if (prefix == null || "".equals(prefix) || "xmlns".equals(prefix))
- {
- writeDefaultNamespace(namespaceURI);
- return;
- }
- if (!inStartElement)
- throw new IllegalStateException();
- try
- {
- if (!isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- if (!isPrefix(prefix))
- throw new IllegalArgumentException("illegal NCName: " + prefix);
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- writeNamespaceImpl(prefix, namespaceURI);
- }
- private void writeNamespaceImpl(String prefix, String namespaceURI)
- throws XMLStreamException
- {
- try
- {
- if (prefix == null)
- prefix = XMLConstants.DEFAULT_NS_PREFIX;
- setPrefix(prefix, namespaceURI);
- writer.write(' ');
- writer.write("xmlns");
- if (!XMLConstants.DEFAULT_NS_PREFIX.equals(prefix))
- {
- writer.write(':');
- writer.write(prefix);
- }
- writer.write('=');
- writer.write('"');
- writer.write(namespaceURI);
- writer.write('"');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeDefaultNamespace(String namespaceURI)
- throws XMLStreamException
- {
- if (!inStartElement)
- throw new IllegalStateException();
- if (!isURI(namespaceURI))
- throw new IllegalArgumentException("illegal URI: " + namespaceURI);
- writeNamespaceImpl(XMLConstants.DEFAULT_NS_PREFIX, namespaceURI);
- }
- public void writeComment(String data)
- throws XMLStreamException
- {
- if (data == null)
- return;
- try
- {
- if (!isChars(data))
- throw new IllegalArgumentException("illegal XML character: " + data);
- if (data.indexOf("--") != -1)
- throw new IllegalArgumentException("illegal comment: " + data);
- endStartElement();
- writer.write("<!--");
- if (hasXML11RestrictedChars)
- {
- int[] seq = UnicodeReader.toCodePointArray(data);
- for (int i = 0; i < seq.length; i++)
- {
- int c = seq[i];
- if (XMLParser.isXML11RestrictedChar(c))
- writer.write("&#x" + Integer.toHexString(c) + ";");
- else
- writer.write(Character.toChars(i));
- }
- }
- else
- writer.write(data);
- writer.write("-->");
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeProcessingInstruction(String target)
- throws XMLStreamException
- {
- writeProcessingInstruction(target, null);
- }
- public void writeProcessingInstruction(String target, String data)
- throws XMLStreamException
- {
- try
- {
- if (!isName(target) || "xml".equalsIgnoreCase(target))
- throw new IllegalArgumentException("illegal PITarget: " + target);
- if (data != null && !isChars(data))
- throw new IllegalArgumentException("illegal XML character: " + data);
- endStartElement();
- writer.write('<');
- writer.write('?');
- writer.write(target);
- if (data != null)
- {
- writer.write(' ');
- if (hasXML11RestrictedChars)
- {
- int[] seq = UnicodeReader.toCodePointArray(data);
- for (int i = 0; i < seq.length; i++)
- {
- int c = seq[i];
- if (XMLParser.isXML11RestrictedChar(c))
- writer.write("&#x" + Integer.toHexString(c) + ";");
- else
- writer.write(Character.toChars(i));
- }
- }
- else
- writer.write(data);
- }
- writer.write('?');
- writer.write('>');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeCData(String data)
- throws XMLStreamException
- {
- try
- {
- if (!isChars(data) || hasXML11RestrictedChars)
- throw new IllegalArgumentException("illegal XML character: " + data);
- if (data.indexOf("]]") != -1)
- throw new IllegalArgumentException("illegal CDATA section: " + data);
- endStartElement();
- writer.write("<![CDATA[");
- writer.write(data);
- writer.write("]]>");
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeDTD(String dtd)
- throws XMLStreamException
- {
- try
- {
- // XXX: Should we parse the doctypedecl at this point to ensure
- // wellformedness?
- writer.write("<!DOCTYPE ");
- writer.write(dtd);
- writer.write('>');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeEntityRef(String name)
- throws XMLStreamException
- {
- try
- {
- if (!isName(name))
- throw new IllegalArgumentException("illegal Name: " + name);
- endStartElement();
- writer.write('&');
- writer.write(name);
- writer.write(';');
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeStartDocument()
- throws XMLStreamException
- {
- writeStartDocument(null, null);
- }
- public void writeStartDocument(String version)
- throws XMLStreamException
- {
- writeStartDocument(null, version);
- }
- public void writeStartDocument(String encoding, String version)
- throws XMLStreamException
- {
- if (version == null)
- version = "1.0";
- else if ("1.1".equals(version))
- xml11 = true;
- encoding = this.encoding; // YES: the parameter must be ignored
- if (encoding == null)
- encoding = "UTF-8";
- if (!"1.0".equals(version) && !"1.1".equals(version))
- throw new IllegalArgumentException(version);
- try
- {
- writer.write("<?xml version=\"");
- writer.write(version);
- writer.write("\" encoding=\"");
- writer.write(encoding);
- writer.write("\"?>");
- writer.write(System.getProperty("line.separator"));
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeCharacters(String text)
- throws XMLStreamException
- {
- if (text == null)
- return;
- try
- {
- if (!isChars(text))
- throw new IllegalArgumentException("illegal XML character: " + text);
- endStartElement();
- if (hasXML11RestrictedChars)
- writeEncodedWithRestrictedChars(text, false);
- else
- writeEncoded(text, false);
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- }
- public void writeCharacters(char[] text, int start, int len)
- throws XMLStreamException
- {
- writeCharacters(new String(text, start, len));
- }
- public String getPrefix(String uri)
- throws XMLStreamException
- {
- String prefix = namespaces.getPrefix(uri);
- if (prefix == null && namespaceContext != null)
- prefix = namespaceContext.getPrefix(uri);
- return prefix;
- }
- public void setPrefix(String prefix, String uri)
- throws XMLStreamException
- {
- try
- {
- if (!isURI(uri))
- throw new IllegalArgumentException("illegal URI: " + uri);
- if (!isPrefix(prefix))
- throw new IllegalArgumentException("illegal NCName: " + prefix);
- }
- catch (IOException e)
- {
- XMLStreamException e2 = new XMLStreamException(e);
- e2.initCause(e);
- throw e2;
- }
- if (!namespaces.declarePrefix(prefix, uri))
- throw new XMLStreamException("illegal prefix " + prefix);
- }
- public void setDefaultNamespace(String uri)
- throws XMLStreamException
- {
- if (!isURI(uri))
- throw new IllegalArgumentException("illegal URI: " + uri);
- if (!namespaces.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX, uri))
- throw new XMLStreamException("illegal default namespace prefix");
- }
- public void setNamespaceContext(NamespaceContext context)
- throws XMLStreamException
- {
- namespaceContext = context;
- }
- public NamespaceContext getNamespaceContext()
- {
- return namespaceContext;
- }
- public Object getProperty(String name)
- throws IllegalArgumentException
- {
- throw new IllegalArgumentException(name);
- }
- /**
- * Write the specified text, ensuring that the content is suitably encoded
- * for XML.
- * @param text the text to write
- * @param inAttr whether we are in an attribute value
- */
- private void writeEncoded(String text, boolean inAttr)
- throws IOException
- {
- char[] chars = text.toCharArray();
- int start = 0;
- int end = chars.length;
- int len = 0;
- for (int i = start; i < end; i++)
- {
- char c = chars[i];
- if (c == '<' || c == '>' || c == '&')
- {
- writer.write(chars, start, len);
- if (c == '<')
- writer.write("<");
- else if (c == '>')
- writer.write(">");
- else
- writer.write("&");
- start = i + 1;
- len = 0;
- }
- else if (inAttr && (c == '"' || c == '\''))
- {
- writer.write(chars, start, len);
- if (c == '"')
- writer.write(""");
- else
- writer.write("'");
- start = i + 1;
- len = 0;
- }
- else
- len++;
- }
- if (len > 0)
- writer.write(chars, start, len);
- }
- /**
- * Writes the specified text, in the knowledge that some of the
- * characters are XML 1.1 restricted characters.
- */
- private void writeEncodedWithRestrictedChars(String text, boolean inAttr)
- throws IOException
- {
- int[] seq = UnicodeReader.toCodePointArray(text);
- for (int i = 0; i < seq.length; i++)
- {
- int c = seq[i];
- switch (c)
- {
- case 0x3c: // '<'
- writer.write("<");
- break;
- case 0x3e: // '>'
- writer.write(">");
- break;
- case 0x26: // '&'
- writer.write("&");
- break;
- case 0x22: // '"'
- if (inAttr)
- writer.write(""");
- else
- writer.write(c);
- break;
- case 0x27: // '\''
- if (inAttr)
- writer.write("'");
- else
- writer.write(c);
- break;
- default:
- if (XMLParser.isXML11RestrictedChar(c))
- writer.write("&#x" + Integer.toHexString(c) + ";");
- else
- {
- char[] chars = Character.toChars(c);
- writer.write(chars, 0, chars.length);
- }
- }
- }
- }
- private boolean isName(String text)
- throws IOException
- {
- if (text == null)
- return false;
- int[] seq = UnicodeReader.toCodePointArray(text);
- if (seq.length < 1)
- return false;
- if (!XMLParser.isNameStartCharacter(seq[0], xml11))
- return false;
- for (int i = 1; i < seq.length; i++)
- {
- if (!XMLParser.isNameCharacter(seq[i], xml11))
- return false;
- }
- return true;
- }
- private boolean isPrefix(String text)
- throws IOException
- {
- if (XMLConstants.DEFAULT_NS_PREFIX.equals(text)) {
- return true;
- }
- return isNCName(text);
- }
- private boolean isNCName(String text)
- throws IOException
- {
- if (text == null)
- return false;
- int[] seq = UnicodeReader.toCodePointArray(text);
- if (seq.length < 1)
- return false;
- if (!XMLParser.isNameStartCharacter(seq[0], xml11) || seq[0] == 0x3a)
- return false;
- for (int i = 1; i < seq.length; i++)
- {
- if (!XMLParser.isNameCharacter(seq[i], xml11) || seq[i] == 0x3a)
- return false;
- }
- return true;
- }
- private boolean isChars(String text)
- throws IOException
- {
- if (text == null)
- return false;
- int[] seq = UnicodeReader.toCodePointArray(text);
- hasXML11RestrictedChars = false;
- if (xml11)
- {
- for (int i = 0; i < seq.length; i++)
- {
- if (!XMLParser.isXML11Char(seq[i]))
- return false;
- if (XMLParser.isXML11RestrictedChar(seq[i]))
- hasXML11RestrictedChars = true;
- }
- }
- else
- {
- for (int i = 0; i < seq.length; i++)
- {
- if (!XMLParser.isChar(seq[i]))
- return false;
- }
- }
- return true;
- }
- private boolean isURI(String text)
- {
- if (text == null)
- return false;
- char[] chars = text.toCharArray();
- if (chars.length < 1)
- return false;
- for (int i = 0; i < chars.length; i++)
- {
- if (chars[i] < 0x20 || chars[i] >= 0x7f)
- return false;
- }
- return true;
- }
- }
|