📄 processor.java
字号:
public final void startCDATA() throws SAXException {
}
public final void endCDATA() throws SAXException {
}
private final void writeAttributes(final Attributes atts)
throws IOException
{
StringBuffer sb = new StringBuffer();
int len = atts.getLength();
for (int i = 0; i < len; i++) {
sb.append(' ')
.append(atts.getLocalName(i))
.append("=\"")
.append(esc(atts.getValue(i)))
.append('\"');
}
w.write(sb.toString());
}
/**
* Encode string with escaping.
*
* @param str string to encode.
* @return encoded string
*/
private final String esc(final String str) {
StringBuffer sb = new StringBuffer(str.length());
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
switch (ch) {
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\"':
sb.append(""");
break;
default:
if (ch > 0x7f) {
sb.append("&#")
.append(Integer.toString(ch))
.append(';');
} else {
sb.append(ch);
}
}
}
return sb.toString();
}
private final void writeIdent() throws IOException {
int n = ident;
while (n > 0) {
if (n > OFF.length) {
w.write(OFF);
n -= OFF.length;
} else {
w.write(OFF, 0, n);
n = 0;
}
}
}
private final void closeElement() throws IOException {
if (openElement) {
w.write(">\n");
}
openElement = false;
}
}
/**
* A {@link org.xml.sax.ContentHandler ContentHandler} that splits XML
* documents into smaller chunks. Each chunk is processed by the nested
* {@link org.xml.sax.ContentHandler ContentHandler} obtained from
* {@link java.net.ContentHandlerFactory ContentHandlerFactory}. This is
* useful for running XSLT engine against large XML document that will
* hardly fit into the memory all together. <p> TODO use complete path for
* subdocumentRoot
*/
private final static class InputSlicingHandler extends DefaultHandler {
private String subdocumentRoot;
private ContentHandler rootHandler;
private ContentHandlerFactory subdocumentHandlerFactory;
private boolean subdocument = false;
private ContentHandler subdocumentHandler;
/**
* Constructs a new {@link InputSlicingHandler SubdocumentHandler}
* object.
*
* @param subdocumentRoot name/path to the root element of the
* subdocument
* @param rootHandler content handler for the entire document
* (subdocument envelope).
* @param subdocumentHandlerFactory a
* {@link ContentHandlerFactory ContentHandlerFactory} used to
* create {@link ContentHandler ContentHandler} instances for
* subdocuments.
*/
public InputSlicingHandler(
final String subdocumentRoot,
final ContentHandler rootHandler,
final ContentHandlerFactory subdocumentHandlerFactory)
{
this.subdocumentRoot = subdocumentRoot;
this.rootHandler = rootHandler;
this.subdocumentHandlerFactory = subdocumentHandlerFactory;
}
public final void startElement(
final String namespaceURI,
final String localName,
final String qName,
final Attributes list) throws SAXException
{
if (subdocument) {
subdocumentHandler.startElement(namespaceURI,
localName,
qName,
list);
} else if (localName.equals(subdocumentRoot)) {
subdocumentHandler = subdocumentHandlerFactory.createContentHandler();
subdocumentHandler.startDocument();
subdocumentHandler.startElement(namespaceURI,
localName,
qName,
list);
subdocument = true;
} else if (rootHandler != null) {
rootHandler.startElement(namespaceURI, localName, qName, list);
}
}
public final void endElement(
final String namespaceURI,
final String localName,
final String qName) throws SAXException
{
if (subdocument) {
subdocumentHandler.endElement(namespaceURI, localName, qName);
if (localName.equals(subdocumentRoot)) {
subdocumentHandler.endDocument();
subdocument = false;
}
} else if (rootHandler != null) {
rootHandler.endElement(namespaceURI, localName, qName);
}
}
public final void startDocument() throws SAXException {
if (rootHandler != null) {
rootHandler.startDocument();
}
}
public final void endDocument() throws SAXException {
if (rootHandler != null) {
rootHandler.endDocument();
}
}
public final void characters(
final char[] buff,
final int offset,
final int size) throws SAXException
{
if (subdocument) {
subdocumentHandler.characters(buff, offset, size);
} else if (rootHandler != null) {
rootHandler.characters(buff, offset, size);
}
}
}
/**
* A {@link org.xml.sax.ContentHandler ContentHandler} that splits XML
* documents into smaller chunks. Each chunk is processed by the nested
* {@link org.xml.sax.ContentHandler ContentHandler} obtained from
* {@link java.net.ContentHandlerFactory ContentHandlerFactory}. This is
* useful for running XSLT engine against large XML document that will
* hardly fit into the memory all together.
*
* <p> TODO use complete path for subdocumentRoot
*/
private static final class OutputSlicingHandler extends DefaultHandler {
private String subdocumentRoot;
private ContentHandlerFactory subdocumentHandlerFactory;
private EntryElement entryElement;
private boolean isXml;
private boolean subdocument = false;
private ContentHandler subdocumentHandler;
/**
* Constructs a new {@link OutputSlicingHandler SubdocumentHandler}
* object.
*
* @param subdocumentHandlerFactory a
* {@link ContentHandlerFactory ContentHandlerFactory} used to
* create {@link ContentHandler ContentHandler} instances for
* subdocuments.
* @param entryElement TODO.
* @param isXml TODO.
*/
public OutputSlicingHandler(
final ContentHandlerFactory subdocumentHandlerFactory,
final EntryElement entryElement,
final boolean isXml)
{
this.subdocumentRoot = "class";
this.subdocumentHandlerFactory = subdocumentHandlerFactory;
this.entryElement = entryElement;
this.isXml = isXml;
}
public final void startElement(
final String namespaceURI,
final String localName,
final String qName,
final Attributes list) throws SAXException
{
if (subdocument) {
subdocumentHandler.startElement(namespaceURI,
localName,
qName,
list);
} else if (localName.equals(subdocumentRoot)) {
String name = list.getValue("name");
if (name == null || name.length() == 0) {
throw new SAXException("Class element without name attribute.");
}
try {
entryElement.openEntry(isXml
? name.concat(".class.xml")
: name.concat(".class"));
} catch (IOException ex) {
throw new SAXException(ex.toString(), ex);
}
subdocumentHandler = subdocumentHandlerFactory.createContentHandler();
subdocumentHandler.startDocument();
subdocumentHandler.startElement(namespaceURI,
localName,
qName,
list);
subdocument = true;
}
}
public final void endElement(
final String namespaceURI,
final String localName,
final String qName) throws SAXException
{
if (subdocument) {
subdocumentHandler.endElement(namespaceURI, localName, qName);
if (localName.equals(subdocumentRoot)) {
subdocumentHandler.endDocument();
subdocument = false;
try {
entryElement.closeEntry();
} catch (IOException ex) {
throw new SAXException(ex.toString(), ex);
}
}
}
}
public final void startDocument() throws SAXException {
}
public final void endDocument() throws SAXException {
}
public final void characters(
final char[] buff,
final int offset,
final int size) throws SAXException
{
if (subdocument) {
subdocumentHandler.characters(buff, offset, size);
}
}
}
private static interface EntryElement {
OutputStream openEntry(String name) throws IOException;
void closeEntry() throws IOException;
}
private static final class SingleDocElement implements EntryElement {
private OutputStream os;
public SingleDocElement(final OutputStream os) {
this.os = os;
}
public OutputStream openEntry(final String name) throws IOException {
return os;
}
public void closeEntry() throws IOException {
os.flush();
}
}
private static final class ZipEntryElement implements EntryElement {
private ZipOutputStream zos;
public ZipEntryElement(final ZipOutputStream zos) {
this.zos = zos;
}
public OutputStream openEntry(final String name) throws IOException {
ZipEntry entry = new ZipEntry(name);
zos.putNextEntry(entry);
return zos;
}
public void closeEntry() throws IOException {
zos.flush();
zos.closeEntry();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -