canonicalizerbase.java
来自「JAVA 所有包」· Java 代码 · 共 880 行 · 第 1/2 页
JAVA
880 行
*/ outputTextToWriter(nextSibling.getNodeValue(), writer); currentNode=nextSibling; sibling=currentNode.getNextSibling(); } } break; case Node.ELEMENT_NODE : Element currentElement = (Element) currentNode; //Add a level to the nssymbtable. So latter can be pop-back. String name=null; currentNodeIsVisible=isVisible(currentNode); if (currentNodeIsVisible) { ns.outputNodePush(); writer.write('<'); name=currentElement.getTagName(); writeStringToUtf8(name,writer); } else { ns.push(); } Iterator attrs = handleAttributes(currentElement,ns); if (attrs!=null) { //we output all Attrs which are available while (attrs.hasNext()) { Attr attr = (Attr) attrs.next(); outputAttrToWriter(attr.getNodeName(),attr.getNodeValue(), writer); } } if (currentNodeIsVisible) { writer.write('>'); } sibling= currentNode.getFirstChild(); if (sibling==null) { if (currentNodeIsVisible) { writer.write(_END_TAG); writeStringToUtf8(name,writer); writer.write('>'); //We fineshed with this level, pop to the previous definitions. ns.outputNodePop(); } else { ns.pop(); } if (parentNode != null) { sibling= currentNode.getNextSibling(); } } else { parentNode=currentElement; } break; } while (sibling==null && parentNode!=null) { if (isVisible(parentNode)) { writer.write(_END_TAG); writeStringToUtf8(((Element)parentNode).getTagName(),writer); writer.write('>'); //We fineshed with this level, pop to the previous definitions. ns.outputNodePop(); } else { ns.pop(); } if (parentNode==endnode) return; sibling=parentNode.getNextSibling(); parentNode=parentNode.getParentNode(); if (!(parentNode instanceof Element)) { parentNode=null; } } if (sibling==null) return; currentNode=sibling; sibling=currentNode.getNextSibling(); } while(true); } boolean isVisible(Node currentNode) { if (nodeFilter!=null) { Iterator it=nodeFilter.iterator(); while (it.hasNext()) { if (!((NodeFilter)it.next()).isNodeInclude(currentNode)) return false; } } if ((this._xpathNodeSet!=null) && !this._xpathNodeSet.contains(currentNode)) return false; return true; } /** * Adds to ns the definitons from the parent elements of el * @param el * @param ns */ final static void getParentNameSpaces(Element el,NameSpaceSymbTable ns) { List parents=new ArrayList(); Node n1=el.getParentNode(); if (!(n1 instanceof Element)) { return; } //Obtain all the parents of the elemnt Element parent=(Element) el.getParentNode(); while (parent!=null) { parents.add(parent); Node n=parent.getParentNode(); if (!(n instanceof Element )) { break; } parent=(Element)n; } //Visit them in reverse order. ListIterator it=parents.listIterator(parents.size()); while (it.hasPrevious()) { Element ele=(Element)it.previous(); if (!ele.hasAttributes()) { continue; } NamedNodeMap attrs = ele.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr N = (Attr) attrs.item(i); if (!Constants.NamespaceSpecNS.equals(N.getNamespaceURI())) { //Not a namespace definition, ignore. continue; } String NName=N.getLocalName(); String NValue=N.getNodeValue(); if (XML.equals(NName) && Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) { continue; } ns.addMapping(NName,NValue,N); } } Attr nsprefix; if (((nsprefix=ns.getMappingWithoutRendered("xmlns"))!=null) && "".equals(nsprefix.getValue())) { ns.addMappingAndRender("xmlns","",nullNode); } } /** * Outputs an Attribute to the internal Writer. * * The string value of the node is modified by replacing * <UL> * <LI>all ampersands (&) with <CODE>&amp;</CODE></LI> * <LI>all open angle brackets (<) with <CODE>&lt;</CODE></LI> * <LI>all quotation mark characters with <CODE>&quot;</CODE></LI> * <LI>and the whitespace characters <CODE>#x9</CODE>, #xA, and #xD, with character * references. The character references are written in uppercase * hexadecimal with no leading zeroes (for example, <CODE>#xD</CODE> is represented * by the character reference <CODE>&#xD;</CODE>)</LI> * </UL> * * @param name * @param value * @param writer * @throws IOException */ static final void outputAttrToWriter(final String name, final String value, final OutputStream writer) throws IOException { writer.write(' '); writeStringToUtf8(name,writer); writer.write(equalsStr); byte []toWrite; final int length = value.length(); for (int i=0;i < length; i++) { char c = value.charAt(i); switch (c) { case '&' : toWrite=_AMP_; //writer.write(_AMP_); break; case '<' : toWrite=_LT_; //writer.write(_LT_); break; case '"' : toWrite=_QUOT_; //writer.write(_QUOT_); break; case 0x09 : // '\t' toWrite=__X9_; //writer.write(__X9_); break; case 0x0A : // '\n' toWrite=__XA_; //writer.write(__XA_); break; case 0x0D : // '\r' toWrite=__XD_; //writer.write(__XD_); break; default : writeCharToUtf8(c,writer); //this._writer.write(c); continue; } writer.write(toWrite); } writer.write('\"'); } final static void writeCharToUtf8(final char c,final OutputStream out) throws IOException{ char ch; if (/*(c >= 0x0001) &&*/ (c <= 0x007F)) { out.write(c); return; } int bias; int write; if (c > 0x07FF) { ch=(char)(c>>>12); write=0xE0; if (ch>0) { write |= ( ch & 0x0F); } out.write(write); write=0x80; bias=0x3F; } else { write=0xC0; bias=0x1F; } ch=(char)(c>>>6); if (ch>0) { write|= (ch & bias); } out.write(write); out.write(0x80 | ((c) & 0x3F)); } final static void writeStringToUtf8(final String str,final OutputStream out) throws IOException{ final int length=str.length(); int i=0; char c; while (i<length) { c=str.charAt(i++); if (/*(c >= 0x0001) &&*/ (c <= 0x007F)) { out.write(c); continue; } char ch; int bias; int write; if (c > 0x07FF) { ch=(char)(c>>>12); write=0xE0; if (ch>0) { write |= ( ch & 0x0F); } out.write(write); write=0x80; bias=0x3F; } else { write=0xC0; bias=0x1F; } ch=(char)(c>>>6); if (ch>0) { write|= (ch & bias); } out.write(write); out.write(0x80 | ((c) & 0x3F)); continue; } } /** * Outputs a PI to the internal Writer. * * @param currentPI * @param writer where to write the things * @throws IOException */ static final void outputPItoWriter(ProcessingInstruction currentPI, OutputStream writer) throws IOException { final int position = getPositionRelativeToDocumentElement(currentPI); if (position == NODE_AFTER_DOCUMENT_ELEMENT) { writer.write('\n'); } writer.write(_BEGIN_PI); final String target = currentPI.getTarget(); int length = target.length(); for (int i = 0; i < length; i++) { char c=target.charAt(i); if (c==0x0D) { writer.write(__XD_); } else { writeCharToUtf8(c,writer); } } final String data = currentPI.getData(); length = data.length(); if (length > 0) { writer.write(' '); for (int i = 0; i < length; i++) { char c=data.charAt(i); if (c==0x0D) { writer.write(__XD_); } else { writeCharToUtf8(c,writer); } } } writer.write(_END_PI); if (position == NODE_BEFORE_DOCUMENT_ELEMENT) { writer.write('\n'); } } /** * Method outputCommentToWriter * * @param currentComment * @param writer writer where to write the things * @throws IOException */ static final void outputCommentToWriter(Comment currentComment, OutputStream writer) throws IOException { final int position = getPositionRelativeToDocumentElement(currentComment); if (position == NODE_AFTER_DOCUMENT_ELEMENT) { writer.write('\n'); } writer.write(_BEGIN_COMM); final String data = currentComment.getData(); final int length = data.length(); for (int i = 0; i < length; i++) { char c=data.charAt(i); if (c==0x0D) { writer.write(__XD_); } else { writeCharToUtf8(c,writer); } } writer.write(_END_COMM); if (position == NODE_BEFORE_DOCUMENT_ELEMENT) { writer.write('\n'); } } /** * Outputs a Text of CDATA section to the internal Writer. * * @param text * @param writer writer where to write the things * @throws IOException */ static final void outputTextToWriter(final String text, final OutputStream writer) throws IOException { final int length = text.length(); byte []toWrite; for (int i = 0; i < length; i++) { char c = text.charAt(i); switch (c) { case '&' : toWrite=_AMP_; //writer.write(_AMP_); break; case '<' : toWrite=_LT_; //writer.write(_LT_); break; case '>' : toWrite=_GT_; //writer.write(_GT_); break; case 0xD : toWrite=__XD_; //writer.write(__XD_); break; default : writeCharToUtf8(c,writer); continue; } writer.write(toWrite); } } /** * Obtain the attributes to output for this node in XPathNodeSet c14n. * * @param E * @param ns * @return the attributes nodes to output. * @throws CanonicalizationException */ abstract Iterator handleAttributes(Element E, NameSpaceSymbTable ns ) throws CanonicalizationException; /** * Obtain the attributes to output for this node in a Subtree c14n. * * @param E * @param ns * @return the attributes nodes to output. * @throws CanonicalizationException */ abstract Iterator handleAttributesSubtree(Element E, NameSpaceSymbTable ns) throws CanonicalizationException; /** * @param _writer The _writer to set. */ public void setWriter(OutputStream _writer) { this._writer = _writer; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?