serializertoxml.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,458 行 · 第 1/5 页
JAVA
2,458 行
writer.write(m_lineSep, 0, m_lineSepLen); m_inDoctype = false; } writer.write("<!ELEMENT "); writer.write(name); writer.write(' '); writer.write(model); writer.write('>'); writer.write(m_lineSep, 0, m_lineSepLen); } catch(IOException ioe) { throw new SAXException(ioe); } } /** NEEDSDOC Field m_elemName */ private String m_elemName = ""; /** * Report an attribute type declaration. * * <p>Only the effective (first) declaration for an attribute will * be reported. The type will be one of the strings "CDATA", * "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", * "ENTITIES", or "NOTATION", or a parenthesized token group with * the separator "|" and all whitespace removed.</p> * * @param eName The name of the associated element. * @param aName The name of the attribute. * @param type A string representing the attribute type. * @param valueDefault A string representing the attribute default * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if * none of these applies. * @param value A string representing the attribute's default value, * or null if there is none. * @exception SAXException The application may raise an exception. */ public void attributeDecl( String eName, String aName, String type, String valueDefault, String value) throws SAXException { // Do not inline external DTD if (m_inExternalDTD) return; try { final Writer writer = m_writer; if (m_inDoctype) { writer.write(" ["); writer.write(m_lineSep, 0, m_lineSepLen); m_inDoctype = false; } writer.write("<!ATTLIST "); writer.write(eName); writer.write(" "); writer.write(aName); writer.write(" "); writer.write(type); if(valueDefault!=null) { writer.write(" "); writer.write(valueDefault); } //m_writer.write(" "); //m_writer.write(value); writer.write(">"); writer.write(m_lineSep, 0, m_lineSepLen); } catch(IOException ioe) { throw new SAXException(ioe); } } /** * Report an internal entity declaration. * * <p>Only the effective (first) declaration for each entity * will be reported.</p> * * @param name The name of the entity. If it is a parameter * entity, the name will begin with '%'. * @param value The replacement text of the entity. * @exception SAXException The application may raise an exception. * @see #externalEntityDecl * @see org.xml.sax.DTDHandler#unparsedEntityDecl */ public void internalEntityDecl(String name, String value) throws SAXException { // Do not inline external DTD if (m_inExternalDTD) return; try { if (m_inDoctype) { m_writer.write(" ["); m_writer.write(m_lineSep, 0, m_lineSepLen); m_inDoctype = false; } outputEntityDecl(name, value); } catch(IOException ioe) { throw new SAXException(ioe); } } /** * Report a parsed external entity declaration. * * <p>Only the effective (first) declaration for each entity * will be reported.</p> * * @param name The name of the entity. If it is a parameter * entity, the name will begin with '%'. * @param publicId The declared public identifier of the entity, or * null if none was declared. * @param systemId The declared system identifier of the entity. * @exception SAXException The application may raise an exception. * @see #internalEntityDecl * @see org.xml.sax.DTDHandler#unparsedEntityDecl */ public void externalEntityDecl( String name, String publicId, String systemId) throws SAXException{} /** * Handle one of the default entities, return false if it * is not a default entity. * * @param ch character to be escaped. * @param i index into character array. * @param chars non-null reference to character array. * @param len length of chars. * @param escLF true if the linefeed should be escaped. * * @return i+1 if the character was written, else i. * * @throws org.xml.sax.SAXException */ protected int accumDefaultEntity( char ch, int i, char[] chars, int len, boolean escLF) throws org.xml.sax.SAXException { try { if (!escLF && CharInfo.S_LINEFEED == ch) { m_writer.write(m_lineSep, 0, m_lineSepLen); } else { if (m_charInfo.isSpecial(ch)) { String entityRef = m_charInfo.getEntityNameForChar(ch); if (null != entityRef) { final Writer writer = m_writer; writer.write('&'); writer.write(entityRef); writer.write(';'); } else return i; } else return i; } return i + 1; } catch(IOException ioe) { throw new SAXException(ioe); } } /** * Escape and m_writer.write a character. * * @param ch character to be escaped. * @param i index into character array. * @param chars non-null reference to character array. * @param len length of chars. * @param escLF true if the linefeed should be escaped. * * @return i+1 if the character was written, else i. * * @throws org.xml.sax.SAXException */ protected int accumDefaultEscape( char ch, int i, char[] chars, int len, boolean escLF) throws org.xml.sax.SAXException { int pos = accumDefaultEntity(ch, i, chars, len, escLF); if (i == pos) { pos++; try { if (0xd800 <= ch && ch < 0xdc00) { // UTF-16 surrogate int next; if (i + 1 >= len) { throw new org.xml.sax.SAXException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_INVALID_UTF16_SURROGATE, new Object[]{ Integer.toHexString(ch) })); //"Invalid UTF-16 surrogate detected: " //+Integer.toHexString(ch)+ " ?"); } else { next = chars[++i]; if (!(0xdc00 <= next && next < 0xe000)) throw new org.xml.sax.SAXException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_INVALID_UTF16_SURROGATE, new Object[]{ Integer.toHexString(ch) + " " + Integer.toHexString(next) })); //"Invalid UTF-16 surrogate detected: " //+Integer.toHexString(ch)+" "+Integer.toHexString(next)); next = ((ch - 0xd800) << 10) + next - 0xdc00 + 0x00010000; } m_writer.write("&#"); m_writer.write(Integer.toString(next)); m_writer.write(";"); /*} else if (null != ctbc && !ctbc.canConvert(ch)) { sb.append("&#x"); sb.append(Integer.toString((int)ch, 16)); sb.append(";");*/ } else { if (!canConvert(ch) || (m_charInfo.isSpecial(ch))) { m_writer.write("&#"); m_writer.write(Integer.toString(ch)); m_writer.write(";"); } else { m_writer.write(ch); } } } catch(IOException ioe) { throw new SAXException(ioe); } } return pos; } /** * Opaque reference to the sun.io.CharToByteConverter for this * encoding. */ Object m_charToByteConverter = null; /** * Method reference to the sun.io.CharToByteConverter#canConvert method * for this encoding. Invalid if m_charToByteConverter is null. */ java.lang.reflect.Method m_canConvertMeth; /** * Boolean that tells if we already tried to get the converter. */ boolean m_triedToGetConverter = false; /** * Tell if this character can be written without escaping. */ public boolean canConvert(char ch) { if(ch < 127) { if(ch >= 0x20 || (0x0A == ch || 0x0D == ch || 0x09 == ch) ) return true; else return false; } if(null == m_charToByteConverter && false == m_triedToGetConverter) { m_triedToGetConverter = true; try { m_charToByteConverter = Encodings.getCharToByteConverter(m_encoding); if(null != m_charToByteConverter) { Class argsTypes[] = new Class[1]; argsTypes[0] = Character.TYPE; Class convClass = m_charToByteConverter.getClass(); m_canConvertMeth = convClass.getMethod("canConvert", argsTypes); } } catch(Exception e) { // This is just an assert: no action at the moment. System.err.println("Warning: "+e.getMessage()); } } if(null != m_charToByteConverter) { try { Object args[] = new Object[1]; args[0] = new Character( ch ); Boolean bool = (Boolean)m_canConvertMeth.invoke(m_charToByteConverter, args); return bool.booleanValue() ? !Character.isISOControl(ch) : false; } catch(java.lang.reflect.InvocationTargetException ite) { // This is just an assert: no action at the moment. System.err.println("Warning: InvocationTargetException in canConvert!"); } catch(java.lang.IllegalAccessException iae) { // This is just an assert: no action at the moment. System.err.println("Warning: IllegalAccessException in canConvert!"); } } // fallback! return ( ch <= m_maxCharacter ); } /** * Returns the specified <var>string</var> after substituting <VAR>specials</VAR>, * and UTF-16 surrogates for chracter 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 { try { final char[] stringChars = string.toCharArray(); final int len = stringChars.length; final Writer writer = m_writer; for (int i = 0; i < len; i++) { char ch = stringChars[i]; if (canConvert(ch) && (!m_charInfo.isSpecial(ch))) { writer.write(ch); } else { // I guess the parser doesn't normalize cr/lf in attributes. -sb if ((CharInfo.S_CARRIAGERETURN == ch) && ((i + 1) < len) && (CharInfo.S_LINEFEED == stringChars[i + 1])) { i++; ch = CharInfo.S_LINEFEED; } accumDefaultEscape(ch, i, stringChars, len, true); } } } catch(IOException ioe) { throw new SAXException(ioe); } } /** * Tell if, based on space preservation constraints and the doIndent property, * if an indent should occur. * * @return True if an indent should occur. */ protected boolean shouldIndent() { return m_doIndent && (!m_ispreserve &&!m_isprevtext); } /** * Prints <var>n</var> spaces. * @param pw The character output stream to use. * @param n Number of spaces to print. * * @throws org.xml.sax.SAXException if an error occurs when writing. */ public void printSpace(int n) throws org.xml.sax.SAXException { try { for (int i = 0; i < n; i++) { m_writer.write(' '); } } catch(IOException ioe) { throw new SAXException(ioe); } } /** * Prints a newline character and <var>n</var> spaces. * @param pw The character output stream to use. * @param n Number of spaces to print. * * @throws org.xml.sax.SAXException if an error occurs during writing. */ public void indent(int n) throws org.xml.sax.SAXException { if (m_startNewLine) outputLineSep(); if (m_doIndent) { printSpace(n); } } /** * Specifies an output stream to which the document should be * serialized. This method should not be called while the * serializer is in the process of serializing a document. * <p> * The encoding specified in the output properties is used, or * if no encoding was specified, the default for the selected * output method. * * @param output The output stream */ public void setOutputStream(OutputStream output) { try { init(output, m_format); } catch (UnsupportedEncodingException uee) { // Should have been warned in init, I guess... } } /** * Get the output stream where the events will be serialized to. * * @return reference to the result stream, or null of only a writer was * set. */ public OutputStream getOutputStre
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?