serializertohtml.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,368 行 · 第 1/3 页
JAVA
1,368 行
int low = (ch & 0x3F) | 0x80; // First 6 bits, + high bit accum('%'); accum(makeHHString(high)); accum('%'); accum(makeHHString(low)); } else if( isUTF16Surrogate(ch) ) // high surrogate { // I'm sure this can be done in 3 instructions, but I choose // to try and do it exactly like it is done in the book, at least // until we are sure this is totally clean. I don't think performance // is a big issue with this particular function, though I could be // wrong. Also, the stuff below clearly does more masking than // it needs to do. // Clear high 6 bits. int highSurrogate = ((int) ch) & 0x03FF; // Middle 4 bits (wwww) + 1 // "Note that the value of wwww from the high surrogate bit pattern // is incremented to make the uuuuu bit pattern in the scalar value // so the surrogate pair don't address the BMP." int wwww = ((highSurrogate & 0x03C0) >> 6); int uuuuu = wwww+1; // next 4 bits int zzzz = (highSurrogate & 0x003C) >> 2; // low 2 bits int yyyyyy = ((highSurrogate & 0x0003) << 4) & 0x30; // Get low surrogate character. ch = stringArray[++i]; // Clear high 6 bits. int lowSurrogate = ((int) ch) & 0x03FF; // put the middle 4 bits into the bottom of yyyyyy (byte 3) yyyyyy = yyyyyy | ((lowSurrogate & 0x03C0) >> 6); // bottom 6 bits. int xxxxxx = (lowSurrogate & 0x003F); int byte1 = 0xF0 | (uuuuu >> 2); // top 3 bits of uuuuu int byte2 = 0x80 | (((uuuuu & 0x03) << 4) & 0x30) | zzzz; int byte3 = 0x80 | yyyyyy; int byte4 = 0x80 | xxxxxx; accum('%'); accum(makeHHString(byte1)); accum('%'); accum(makeHHString(byte2)); accum('%'); accum(makeHHString(byte3)); accum('%'); accum(makeHHString(byte4)); } else { int high = (ch >> 12) | 0xE0; // top 4 bits int middle = ((ch & 0x0FC0) >> 6) | 0x80; // middle 6 bits int low = (ch & 0x3F) | 0x80; // First 6 bits, + high bit accum('%'); accum(makeHHString(high)); accum('%'); accum(makeHHString(middle)); accum('%'); accum(makeHHString(low)); } } else if (canConvert(ch)) { accum(ch); } else { accum("&#"); accum(Integer.toString(ch)); accum(';'); } } else if('%' == ch) { // If the character is a '%' number number, try to avoid double-escaping. // There is a question if this is legal behavior. // Dmitri Ilyin: to check if '%' number number is invalid. It must be checked if %xx is a sign, that would be encoded// The encoded signes are in Hex form. So %xx my be in form %3C that is "<" sign. I will try to change here a little. // if( ((i+2) < len) && isASCIIDigit(stringArray[i+1]) && isASCIIDigit(stringArray[i+2]) ) // We are no longer escaping '%' /* if ( ((i+2) < len) && isHHSign(new String(stringArray,i+1,2)) ) { accum(ch); } else { if (doURLEscaping) { accum('%'); accum(makeHHString(ch)); } else*/ accum(ch); // } } // Since http://www.ietf.org/rfc/rfc2396.txt refers to the URI grammar as // not allowing quotes in the URI proper syntax, nor in the fragment // identifier, we believe that it's OK to double escape quotes. else if (ch == '"') { // Mike Kay encodes this as ", so he may know something I don't? if (doURLEscaping) accum("%22"); else accum("""); // we have to escape this, I guess. } else { accum(ch); } } } /** * Writes the specified <var>string</var> after substituting <VAR>specials</VAR>, * and UTF-16 surrogates for character references <CODE>&#xnn</CODE>. * * @param string String to convert to XML format. * @param encoding CURRENTLY NOT IMPLEMENTED. * * @throws org.xml.sax.SAXException */ public void writeAttrString(String string, String encoding) throws org.xml.sax.SAXException { final char chars[] = string.toCharArray(); final int strLen = chars.length; for (int i = 0; i < strLen; i++) { char ch = chars[i]; // System.out.println("SPECIALSSIZE: "+SPECIALSSIZE); // System.out.println("ch: "+(int)ch); // System.out.println("m_maxCharacter: "+(int)m_maxCharacter); // System.out.println("m_attrCharsMap[ch]: "+(int)m_attrCharsMap[ch]); if (canConvert(ch) && (!m_charInfo.isSpecial(ch))) { accum(ch); } else if ('<' == ch || '>' == ch) { accum(ch); // no escaping in this case, as specified in 15.2 } else if (('&' == ch) && ((i + 1) < strLen) && ('{' == chars[i + 1])) { accum(ch); // no escaping in this case, as specified in 15.2 } else { int pos = accumDefaultEntity(ch, i, chars, strLen, false); if (i != pos) { i = pos - 1; } else { if (isUTF16Surrogate(ch)) { try { i = writeUTF16Surrogate(ch, chars, i, strLen); } catch(IOException ioe) { throw new SAXException(ioe); } } // The next is kind of a hack to keep from escaping in the case // of Shift_JIS and the like. /* else if ((ch < m_maxCharacter) && (m_maxCharacter == 0xFFFF) && (ch != 160)) { accum(ch); // no escaping in this case } else */ String entityName = m_charInfo.getEntityNameForChar(ch); if (null != entityName) { accum('&'); accum(entityName); accum(';'); } else if (canConvert(ch)) { accum(ch); // no escaping in this case } else { accum("&#"); accum(Integer.toString(ch)); accum(';'); } } } } } /** * Copy an entity into the accumulation buffer. * * @param s The name of the entity. * @param pos unused. * * @return The pos argument. * * @throws org.xml.sax.SAXException */ private int copyEntityIntoBuf(String s, int pos) throws org.xml.sax.SAXException { int l = s.length(); accum('&'); for (int i = 0; i < l; i++) { accum(s.charAt(i)); } accum(';'); return pos; } /** * Receive notification of character data. * * <p>The Parser will call this method to report each chunk of * character data. SAX parsers may return all contiguous character * data in a single chunk, or they may split it into several * chunks; however, all of the characters in any single event * must come from the same external entity, so that the Locator * provides useful information.</p> * * <p>The application must not attempt to read from the array * outside of the specified range.</p> * * <p>Note that some parsers will report whitespace using the * ignorableWhitespace() method rather than this one (validating * parsers must do so).</p> * * @param chars The characters from the XML document. * @param start The start position in the array. * @param length The number of characters to read from the array. * @throws org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see #ignorableWhitespace * @see org.xml.sax.Locator * * @throws org.xml.sax.SAXException */ public void characters(char chars[], int start, int length) throws org.xml.sax.SAXException { if (m_isRawStack.peekOrFalse()) { try { writeParentTagEnd(); m_ispreserve = true; if (shouldIndent()) indent(m_currentIndent); // this.accum("<![CDATA["); // this.accum(chars, start, length); writeNormalizedChars(chars, start, length, false); // this.accum("]]>"); return; } catch (IOException ioe) { throw new org.xml.sax.SAXException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_OIERROR, null), ioe); //"IO error", ioe); } } else { super.characters(chars, start, length); } } /** * Receive notification of cdata. * * <p>The Parser will call this method to report each chunk of * character data. SAX parsers may return all contiguous character * data in a single chunk, or they may split it into several * chunks; however, all of the characters in any single event * must come from the same external entity, so that the Locator * provides useful information.</p> * * <p>The application must not attempt to read from the array * outside of the specified range.</p> * * <p>Note that some parsers will report whitespace using the * ignorableWhitespace() method rather than this one (validating * parsers must do so).</p> * * @param ch The characters from the XML document. * @param start The start position in the array. * @param length The number of characters to read from the array. * @throws org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see #ignorableWhitespace * @see org.xml.sax.Locator * * @throws org.xml.sax.SAXException */ public void cdata(char ch[], int start, int length) throws org.xml.sax.SAXException { if ((null != m_currentElementName) && (m_currentElementName.equalsIgnoreCase("SCRIPT") || m_currentElementName.equalsIgnoreCase("STYLE"))) { try { writeParentTagEnd(); m_ispreserve = true; if (shouldIndent()) indent(m_currentIndent); // this.accum(ch, start, length); writeNormalizedChars(ch, start, length, true); } catch (IOException ioe) { throw new org.xml.sax.SAXException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_OIERROR, null), ioe); //"IO error", ioe); } } /* else if(m_stripCData) // should normally always be false { try { writeParentTagEnd(); m_ispreserve = true; if (shouldIndent()) indent(m_currentIndent); // this.accum("<![CDATA["); this.accum(ch, start, length); // this.accum("]]>"); } catch(IOException ioe) { throw new org.xml.sax.SAXException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_OIERROR, null),ioe); //"IO error", ioe); } } */ else { super.cdata(ch, start, length); } } /** * Receive notification of a processing instruction. * * @param target The processing instruction target. * @param data The processing instruction data, or null if * none was supplied. * @throws org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * * @throws org.xml.sax.SAXException */ public void processingInstruction(String target, String data) throws org.xml.sax.SAXException { // Use a fairly nasty hack to tell if the next node is supposed to be // unescaped text. if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING)) { startNonEscaping(); } else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING)) { endNonEscaping(); } else { writeParentTagEnd(); if (shouldIndent()) indent(m_currentIndent); this.accum("<?" + target); if (data.length() > 0 &&!Character.isSpaceChar(data.charAt(0))) this.accum(" "); this.accum(data + ">"); // different from XML // Always output a newline char if not inside of an // element. The whitespace is not significant in that // case. if (m_elemStack.isEmpty()) outputLineSep(); m_startNewLine = true; } } /** * Receive notivication of a entityReference. * * @param name non-null reference to entity name string. * * @throws org.xml.sax.SAXException */ public void entityReference(String name) throws org.xml.sax.SAXException { this.accum("&"); this.accum(name); this.accum(";"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?