📄 xmlwriter.java
字号:
public void writeEmptyElement ( String uri, String localName, String qName, Attributes atts ) throws SAXException { if (canonical) { startElement (uri, localName, qName, atts); endElement (uri, localName, qName); } else { try { writeStartTag (qName, atts, true); } catch (IOException e) { fatal ("can't write", e); } } } /** <b>SAX2</b>: indicates the end of an element */ final public void endElement (String uri, String localName, String qName) throws SAXException { if (qName == null || "".equals (qName)) throw new IllegalArgumentException ("no XML name"); try { elementNestLevel--; if (entityNestLevel != 0) return; if (xhtml && isEmptyElementTag (qName)) return; rawWrite ("</"); rawWrite (qName); rawWrite ('>'); if (prettyPrinting) { if (!space.empty ()) space.pop (); else fatal ("stack discipline", null); } if (elementNestLevel == 0) inEpilogue = true; } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX1</b>: reports content characters */ final public void characters (char ch [], int start, int length) throws SAXException { if (locator == null) locator = new LocatorImpl (); try { if (entityNestLevel != 0) return; if (inCDATA) { escapeChars (ch, start, length, CTX_UNPARSED); } else { escapeChars (ch, start, length, CTX_CONTENT); } } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX1</b>: reports ignorable whitespace */ final public void ignorableWhitespace (char ch [], int start, int length) throws SAXException { if (locator == null) locator = new LocatorImpl (); try { if (entityNestLevel != 0) return; // don't forget to map NL to CRLF, CR, etc escapeChars (ch, start, length, CTX_CONTENT); } catch (IOException e) { fatal ("can't write", e); } } /** * <b>SAX1</b>: reports a PI. * This doesn't check for illegal target names, such as "xml" or "XML", * or namespace-incompatible ones like "big:dog"; the caller is * responsible for ensuring those names are legal. */ final public void processingInstruction (String target, String data) throws SAXException { if (locator == null) locator = new LocatorImpl (); // don't print internal subset for XHTML if (xhtml && startedDoctype) return; // ancient HTML browsers might render these ... their loss. // to prevent: "if (xhtml) return;". try { if (entityNestLevel != 0) return; if (canonical && inEpilogue) newline (); rawWrite ("<?"); rawWrite (target); rawWrite (' '); escapeChars (data.toCharArray (), -1, -1, CTX_UNPARSED); rawWrite ("?>"); if (elementNestLevel == 0 && !(canonical && inEpilogue)) newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX1</b>: indicates a non-expanded entity reference */ public void skippedEntity (String name) throws SAXException { try { rawWrite ("&"); rawWrite (name); rawWrite (";"); } catch (IOException e) { fatal ("can't write", e); } } // SAX2 LexicalHandler /** <b>SAX2</b>: called before parsing CDATA characters */ final public void startCDATA () throws SAXException { if (locator == null) locator = new LocatorImpl (); if (canonical) return; try { inCDATA = true; if (entityNestLevel == 0) rawWrite ("<![CDATA["); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX2</b>: called after parsing CDATA characters */ final public void endCDATA () throws SAXException { if (canonical) return; try { inCDATA = false; if (entityNestLevel == 0) rawWrite ("]]>"); } catch (IOException e) { fatal ("can't write", e); } } /** * <b>SAX2</b>: called when the doctype is partially parsed * Note that this, like other doctype related calls, is ignored * when XHTML is in use. */ final public void startDTD (String name, String publicId, String systemId) throws SAXException { if (locator == null) locator = new LocatorImpl (); if (xhtml) return; try { inDoctype = startedDoctype = true; if (canonical) return; rawWrite ("<!DOCTYPE "); rawWrite (name); rawWrite (' '); if (!expandingEntities) { if (publicId != null) rawWrite ("PUBLIC '" + publicId + "' '" + systemId + "' "); else if (systemId != null) rawWrite ("SYSTEM '" + systemId + "' "); } rawWrite ('['); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX2</b>: called after the doctype is parsed */ final public void endDTD () throws SAXException { inDoctype = false; if (canonical || xhtml) return; try { rawWrite ("]>"); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** * <b>SAX2</b>: called before parsing a general entity in content */ final public void startEntity (String name) throws SAXException { try { boolean writeEOL = true; // Predefined XHTML entities (for characters) will get // mapped back later. if (xhtml || expandingEntities) return; entityNestLevel++; if (name.equals ("[dtd]")) return; if (entityNestLevel != 1) return; if (!name.startsWith ("%")) { writeEOL = false; rawWrite ('&'); } rawWrite (name); rawWrite (';'); if (writeEOL) newline (); } catch (IOException e) { fatal ("can't write", e); } } /** * <b>SAX2</b>: called after parsing a general entity in content */ final public void endEntity (String name) throws SAXException { if (xhtml || expandingEntities) return; entityNestLevel--; } /** * <b>SAX2</b>: called when comments are parsed. * When XHTML is used, the old HTML tradition of using comments * to for inline CSS, or for JavaScript code is discouraged. * This is because XML processors are encouraged to discard, on * the grounds that comments are for users (and perhaps text * editors) not programs. Instead, use external scripts */ final public void comment (char ch [], int start, int length) throws SAXException { if (locator == null) locator = new LocatorImpl (); // don't print internal subset for XHTML if (xhtml && startedDoctype) return; // don't print comment in doctype for canon xml if (canonical && inDoctype) return; try { boolean indent; if (prettyPrinting && space.empty ()) fatal ("stack discipline", null); indent = prettyPrinting && "default".equals (space.peek ()); if (entityNestLevel != 0) return; if (indent) doIndent (); if (canonical && inEpilogue) newline (); rawWrite ("<!--"); escapeChars (ch, start, length, CTX_UNPARSED); rawWrite ("-->"); if (indent) doIndent (); if (elementNestLevel == 0 && !(canonical && inEpilogue)) newline (); } catch (IOException e) { fatal ("can't write", e); } } // SAX1 DTDHandler /** <b>SAX1</b>: called on notation declarations */ final public void notationDecl (String name, String publicId, String systemId) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) return; if (entityNestLevel != 0) return; rawWrite ("<!NOTATION " + name + " "); if (publicId != null) rawWrite ("PUBLIC \"" + publicId + '"'); else rawWrite ("SYSTEM "); if (systemId != null) rawWrite ('"' + systemId + '"'); rawWrite (">"); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX1</b>: called on unparsed entity declarations */ final public void unparsedEntityDecl (String name, String publicId, String systemId, String notationName) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) { // FIXME: write to temporary buffer, and make the start // of the root element write these declarations. return; } if (entityNestLevel != 0) return; rawWrite ("<!ENTITY " + name + " "); if (publicId != null) rawWrite ("PUBLIC \"" + publicId + '"'); else rawWrite ("SYSTEM "); rawWrite ('"' + systemId + '"'); rawWrite (" NDATA " + notationName + ">"); newline (); } catch (IOException e) { fatal ("can't write", e); } } // SAX2 DeclHandler /** <b>SAX2</b>: called on attribute declarations */ final public void attributeDecl (String eName, String aName, String type, String mode, String value) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) return; if (entityNestLevel != 0) return; rawWrite ("<!ATTLIST " + eName + ' ' + aName + ' '); rawWrite (type); rawWrite (' '); if (mode != null) rawWrite (mode + ' '); if (value != null) writeQuotedValue (value, CTX_ATTRIBUTE); rawWrite ('>'); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX2</b>: called on element declarations */ final public void elementDecl (String name, String model) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) return; if (entityNestLevel != 0) return; rawWrite ("<!ELEMENT " + name + ' ' + model + '>'); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX2</b>: called on external entity declarations */ final public void externalEntityDecl ( String name, String publicId, String systemId) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) return; if (entityNestLevel != 0) return; rawWrite ("<!ENTITY "); if (name.startsWith ("%")) { rawWrite ("% "); rawWrite (name.substring (1)); } else rawWrite (name); if (publicId != null) rawWrite (" PUBLIC \"" + publicId + '"'); else rawWrite (" SYSTEM "); rawWrite ('"' + systemId + "\">"); newline (); } catch (IOException e) { fatal ("can't write", e); } } /** <b>SAX2</b>: called on internal entity declarations */ final public void internalEntityDecl (String name, String value) throws SAXException { if (xhtml) return; try { // At this time, only SAX2 callbacks start these. if (!startedDoctype) return; if (entityNestLevel != 0) return; rawWrite ("<!ENTITY "); if (name.startsWith ("%")) { rawWrite ("% "); rawWrite (name.substring (1)); } else rawWrite (name); rawWrite (' '); writeQuotedValue (value, CTX_ENTITY); rawWrite ('>'); newline (); } catch (IOException e) { fatal ("can't write", e); } } private void writeQuotedValue (String value, int code) throws SAXException, IOException { char buf [] = value.toCharArray ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -