📄 domelementwriter.java
字号:
// Write element out.write("<"); if (namespacePolicy.qualifyElements) { String uri = getNamespaceURI(element); String prefix = (String) nsPrefixMap.get(uri); if (prefix == null) { if (nsPrefixMap.isEmpty()) { // steal default namespace prefix = ""; } else { prefix = NS + (nextPrefix++); } nsPrefixMap.put(uri, prefix); addNSDefinition(element, uri); } if (!"".equals(prefix)) { out.write(prefix); out.write(":"); } } out.write(element.getTagName()); // Write attributes NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); out.write(" "); if (namespacePolicy.qualifyAttributes) { String uri = getNamespaceURI(attr); String prefix = (String) nsPrefixMap.get(uri); if (prefix == null) { prefix = NS + (nextPrefix++); nsPrefixMap.put(uri, prefix); addNSDefinition(element, uri); } out.write(prefix); out.write(":"); } out.write(attr.getName()); out.write("=\""); out.write(encode(attr.getValue())); out.write("\""); } // write namespace declarations ArrayList al = (ArrayList) nsURIByElement.get(element); if (al != null) { Iterator iter = al.iterator(); while (iter.hasNext()) { String uri = (String) iter.next(); String prefix = (String) nsPrefixMap.get(uri); out.write(" xmlns"); if (!"".equals(prefix)) { out.write(":"); out.write(prefix); } out.write("=\""); out.write(uri); out.write("\""); } } if (hasChildren) { out.write(">"); } else { removeNSDefinitions(element); out.write(" />"); out.write(lSep); out.flush(); } } /** * Writes a DOM tree to a stream. * * @param element the Root DOM element of the tree * @param out where to send the output * @param indent number of * @param indentWith string that should be used to indent the * corresponding tag. * @param hasChildren if true indent. * @throws IOException if an error happens while writing to the stream. */ public void closeElement(Element element, Writer out, int indent, String indentWith, boolean hasChildren) throws IOException { // If we had child elements, we need to indent before we close // the element, otherwise we're on the same line and don't need // to indent if (hasChildren) { for (int i = 0; i < indent; i++) { out.write(indentWith); } } // Write element close out.write("</"); if (namespacePolicy.qualifyElements) { String uri = getNamespaceURI(element); String prefix = (String) nsPrefixMap.get(uri); if (prefix != null && !"".equals(prefix)) { out.write(prefix); out.write(":"); } removeNSDefinitions(element); } out.write(element.getTagName()); out.write(">"); out.write(lSep); out.flush(); } /** * Escape <, > & ', " as their entities and * drop characters that are illegal in XML documents. * @param value the string to encode. * @return the encoded string. */ public String encode(String value) { StringBuffer sb = new StringBuffer(); int len = value.length(); for (int i = 0; i < len; i++) { char c = value.charAt(i); switch (c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '\'': sb.append("'"); break; case '\"': sb.append("""); break; case '&': int nextSemi = value.indexOf(";", i); if (nextSemi < 0 || !isReference(value.substring(i, nextSemi + 1))) { sb.append("&"); } else { sb.append('&'); } break; default: if (isLegalCharacter(c)) { sb.append(c); } break; } } return sb.substring(0); } /** * Drop characters that are illegal in XML documents. * * <p>Also ensure that we are not including an <code>]]></code> * marker by replacing that sequence with * <code>&#x5d;&#x5d;&gt;</code>.</p> * * <p>See XML 1.0 2.2 <a * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets"> * http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a> and * 2.7 <a * href="http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect">http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect</a>.</p> * @param value the value to be encoded. * @return the encoded value. */ public String encodedata(final String value) { StringBuffer sb = new StringBuffer(); int len = value.length(); for (int i = 0; i < len; ++i) { char c = value.charAt(i); if (isLegalCharacter(c)) { sb.append(c); } } String result = sb.substring(0); int cdEnd = result.indexOf("]]>"); while (cdEnd != -1) { sb.setLength(cdEnd); // CheckStyle:MagicNumber OFF sb.append("]]>") .append(result.substring(cdEnd + 3)); // CheckStyle:MagicNumber ON result = sb.substring(0); cdEnd = result.indexOf("]]>"); } return result; } /** * Is the given argument a character or entity reference? * @param ent the value to be checked. * @return true if it is an entity. */ public boolean isReference(String ent) { if (!(ent.charAt(0) == '&') || !ent.endsWith(";")) { return false; } if (ent.charAt(1) == '#') { if (ent.charAt(2) == 'x') { try { // CheckStyle:MagicNumber OFF Integer.parseInt(ent.substring(3, ent.length() - 1), HEX); // CheckStyle:MagicNumber ON return true; } catch (NumberFormatException nfe) { return false; } } else { try { Integer.parseInt(ent.substring(2, ent.length() - 1)); return true; } catch (NumberFormatException nfe) { return false; } } } String name = ent.substring(1, ent.length() - 1); for (int i = 0; i < knownEntities.length; i++) { if (name.equals(knownEntities[i])) { return true; } } return false; } /** * Is the given character allowed inside an XML document? * * <p>See XML 1.0 2.2 <a * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets"> * http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a>.</p> * @param c the character to test. * @return true if the character is allowed. * @since 1.10, Ant 1.5 */ public boolean isLegalCharacter(char c) { // CheckStyle:MagicNumber OFF if (c == 0x9 || c == 0xA || c == 0xD) { return true; } else if (c < 0x20) { return false; } else if (c <= 0xD7FF) { return true; } else if (c < 0xE000) { return false; } else if (c <= 0xFFFD) { return true; } // CheckStyle:MagicNumber ON return false; } private void removeNSDefinitions(Element element) { ArrayList al = (ArrayList) nsURIByElement.get(element); if (al != null) { Iterator iter = al.iterator(); while (iter.hasNext()) { nsPrefixMap.remove(iter.next()); } nsURIByElement.remove(element); } } private void addNSDefinition(Element element, String uri) { ArrayList al = (ArrayList) nsURIByElement.get(element); if (al == null) { al = new ArrayList(); nsURIByElement.put(element, al); } al.add(uri); } private static String getNamespaceURI(Node n) { String uri = n.getNamespaceURI(); if (uri == null) { // FIXME: Is "No Namespace is Empty Namespace" really OK? uri = ""; } return uri; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -