📄 litexmldocument.java
字号:
this.mimeType = mimeType; docContent = new StringBuilder(); init(in); } /** * {@inheritDoc} */ @Override public String toString() { try { StringWriter stringOut = new StringWriter(); sendToWriter(stringOut); stringOut.close(); return stringOut.toString(); } catch (IOException caught) { throw new UndeclaredThrowableException(caught); } } /** * {@inheritDoc} */ public MimeMediaType getMimeType() { return mimeType; } /** * {@inheritDoc} */ public String getFileExtension() { return TextDocumentCommon.Utils.getExtensionForMime(INSTANTIATOR.getSupportedFileExtensions(), getMimeType()); } /** * {@inheritDoc} */ public LiteXMLElement createElement(Object key) { return createElement(key, null); } /** * {@inheritDoc} */ public LiteXMLElement createElement(Object key, Object val) { if (!(key instanceof String)) { throw new ClassCastException(key.getClass().getName() + " not supported by createElement as key."); } if ((null != val) && !(val instanceof String)) { throw new ClassCastException(val.getClass().getName() + " not supported by createElement as value."); } return createElement((String) key, (String) val); } /** * {@inheritDoc} */ public LiteXMLElement createElement(String name) { return createElement(name, (String) null); } /** * {@inheritDoc} */ public LiteXMLElement createElement(String name, String val) { return new LiteXMLElement(this, name, val); } /** * Create a new text element as a sub-range of this document. * * @param loc The document range for the new element. * @return The newly created element. */ protected LiteXMLElement createElement(tagRange loc) { return new LiteXMLElement(this, loc); } /** * {@inheritDoc} */ public Reader getReader() { return new StringReader(toString()); } /** * {@inheritDoc} */ @Override public LiteXMLDocument getRoot() { return this; } /** * {@inheritDoc} */ public InputStream getStream() throws IOException { String charset = mimeType.getParameter("charset"); if (charset == null) { return new ByteArrayInputStream(toString().getBytes()); } else { return new ByteArrayInputStream(toString().getBytes(charset)); } } /** * {@inheritDoc} */ public void sendToWriter(Writer writer) throws IOException { String charset = mimeType.getParameter("charset"); if (charset == null) { writer.write("<?xml version=\"1.0\"?>\n"); } else { writer.write("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\n"); } tagRange result = getDocType(docContent, true); if (result.isValid()) { writer.write(docContent.substring(result.startTag.start, result.startTag.end + 1)); writer.write('\n'); } printNice(writer, 0, true); } /** * {@inheritDoc} */ public void sendToStream(OutputStream stream) throws IOException { String charset = mimeType.getParameter("charset"); Writer osw; if (charset == null) { osw = new OutputStreamWriter(stream); } else { osw = new OutputStreamWriter(stream, charset); } Writer out = new BufferedWriter(osw); sendToWriter(out); out.flush(); } /** * Initialises LiteXMLDocument. */ protected void init(Reader in) throws IOException { loc = new tagRange(); char[] smallBuffer = new char[512]; do { int readCount = in.read(smallBuffer); if (readCount < 0) { break; } if (readCount > 0) { docContent.append(smallBuffer, 0, readCount); } } while (true); // startTag will contain the xml declaration loc.startTag.start = 0; loc.startTag.end = docContent.indexOf(">"); // body is everything after the xml declaration loc.body.start = loc.startTag.end + 1; loc.body.end = docContent.length() - 1; // end is the end of the doc. loc.endTag.start = loc.body.end; loc.endTag.end = loc.body.end; tagRange docType = getDocType(getDocument().docContent, false); if (docType.isValid()) { loc = getTagRanges(getDocument().docContent, docContent.substring(docType.body.start, docType.body.end + 1) , docType.endTag); } else { loc = getTagRanges(getDocument().docContent, null, loc.body); } if (!loc.isValid()) { throw new RuntimeException("Parsing error in source document."); } if (!loc.startTag.equals(loc.endTag)) { addChildTags(loc.body, this); // now add the subtags } if (paranoidConsistencyChecking) { checkConsistency(); } } protected tagRange getDocType(final StringBuilder source, boolean wholeElement) { final String xmldoctype = "!DOCTYPE"; int start = 0; int end = getDocument().docContent.length() - 1; tagRange ranges = getTagRanges(source, xmldoctype, new charRange(start, end)); if (!ranges.startTag.isValid()) { return ranges; } // the rest of the document will be the "end" ranges.endTag.start = ranges.body.start; ranges.endTag.end = ranges.body.end; if (wholeElement) { // this will be an empty element ranges.body.start = ranges.startTag.end + 1; ranges.body.end = ranges.endTag.start - 1; } else { ranges.body.start = ranges.startTag.start + 1 + xmldoctype.length() - 1 + 1; ranges.startTag.end = ranges.body.start - 1; while ((ranges.body.start < end) && // immediately followed by a delimiter or the end of the tag Character.isWhitespace(source.charAt(ranges.body.start))) { ranges.body.start++; } ranges.body.end = ranges.body.start; while ((ranges.body.end + 1) < end) { // immediately followed by a delimiter or the end of the tag char possibleEnd = source.charAt(ranges.body.end + 1); if (Character.isWhitespace(possibleEnd) || ('/' == possibleEnd) || ('>' == possibleEnd)) { break; } ranges.body.end++; } } return ranges; } /** * {@inheritDoc} */ @Override LiteXMLDocument getDocument() { return this; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -