📄 xmlstreamwriterimpl.java
字号:
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 { // Really thoroughly pointless method... try { if (!isName(dtd)) throw new IllegalArgumentException("illegal Name: " + dtd); 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 (!isNCName(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 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; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -