jsonxmlstreamwriter.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 442 行 · 第 1/2 页

JAVA
442
字号
        }    }    public void writeStartDocument() throws XMLStreamException {        depth = 0;        processingStack = new ArrayList<ProcessingState>();        processingStack.add(createProcessingState());    }    public void writeCharacters(char[] text, int start, int length) throws XMLStreamException {        writeCharacters(new String(text, start, length));    }    public void setDefaultNamespace(String uri) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeCData(String data) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeCharacters(String text) throws XMLStreamException {        if (processingStack.get(depth).hasAttributes) {            writeStartElement(null, "$", null);            writeCharacters(text);            writeEndElement();        } else {            try {                if (isNonString(processingStack.get(depth - 1).currentName)) {                    processingStack.get(depth).writer.write(JsonEncoder.encode(text));                } else {                    processingStack.get(depth).writer.write("\"" + JsonEncoder.encode(text) + "\"");                }                processingStack.get(depth).lastWasPrimitive = true;            } catch (IOException ex) {                Logger.getLogger(JsonXmlStreamWriter.class.getName()).log(Level.SEVERE, null, ex);            }        }    }    public void writeComment(String data) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeDTD(String dtd) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeDefaultNamespace(String uri) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeEmptyElement(String localName) throws XMLStreamException {        writeEmptyElement(null, localName, null);    }    public void writeEntityRef(String name) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeProcessingInstruction(String target) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeStartDocument(String version) throws XMLStreamException {    }    public void writeStartElement(String localName) throws XMLStreamException {        writeStartElement(null, localName, null);    }    public NamespaceContext getNamespaceContext() {        return null;    }    public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public Object getProperty(String name) throws IllegalArgumentException {        throw new UnsupportedOperationException("Not supported yet.");    }    public String getPrefix(String uri) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void setPrefix(String prefix, String uri) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeAttribute(String localName, String value) throws XMLStreamException {        writeAttribute(null, null, localName, value);    }    public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {        writeEmptyElement(null, localName, null);    }    public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {        // we do not want to deal with namespaces        // the main goal of this writer is keep the produced json as simple as possible    }    public void writeProcessingInstruction(String target, String data) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeStartDocument(String encoding, String version) throws XMLStreamException {        throw new UnsupportedOperationException("Not supported yet.");    }    public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {        writeStartElement(null, localName, null);    }    public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {        writeAttribute(null, namespaceURI, localName, value);    }    public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {        writeStartElement(localName);        writeEndElement();    }    private void printStack(String localName) {        try {            for (int d = 0; d <= depth; d++) {                mainWriter.write("\n**" + d + ":" + processingStack.get(d));            }            mainWriter.write("\n*** [" + localName + "]");        } catch (IOException ex) {            Logger.getLogger(JsonXmlStreamWriter.class.getName()).log(Level.SEVERE, null, ex);        }    }        private boolean isArrayElement(String name) {        if (null == name) {            return false;        }        return arrayElementNames.contains(name);    }    private boolean isNonString(String name) {        if (null == name) {            return false;        }        return nonStringElementNames.contains(name);    }        public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {        processingStack.get(depth).currentName = localName;        try {            boolean isNextArrayElement = processingStack.get(depth).currentName.equals(processingStack.get(depth).lastName);            if (!isNextArrayElement) {                if (isArrayElement(processingStack.get(depth).lastName) && processingStack.get(depth).hasNoElements) { // one elem array                    processingStack.get(depth).writer.write("[");                    processingStack.get(depth).lastIsArray = true;                    processingStack.get(depth).writer.write(processingStack.get(depth).lastElementWriter.getContent());                } else {                    // write previous elements from buffer                    if (null != processingStack.get(depth).lastElementWriter) {                        if (processingStack.get(depth).lastIsArray) {                            processingStack.get(depth).writer.write(",");                            processingStack.get(depth).writer.write(processingStack.get(depth).lastElementWriter.getContent());                            processingStack.get(depth).writer.write("]");                            processingStack.get(depth).hasNoElements = false;                        } else {                            processingStack.get(depth).writer.write(processingStack.get(depth).lastElementWriter.getContent());                        }                    }                    processingStack.get(depth).lastIsArray = false;                }                if (null != processingStack.get(depth).lastName) {                    if (processingStack.get(depth).lastIsArray) {                        processingStack.get(depth).writer.write("]");                        processingStack.get(depth).lastIsArray = false;                    }                    processingStack.get(depth).writer.write(",");  // next element at the same level                }                if (null == processingStack.get(depth).lastWasPrimitive) {                    processingStack.get(depth).writer.write("{"); // first sub-element                }                processingStack.get(depth).writer.write("\"" + localName + "\":");            } else { // next array element                processingStack.get(depth).writer.write(processingStack.get(depth).lastIsArray ? "," : "[");  // next element at the same level                processingStack.get(depth).lastIsArray = true;                processingStack.get(depth).writer.write(processingStack.get(depth).lastElementWriter.getContent());                processingStack.get(depth).hasNoElements = false;            }            depth++;            processingStack.add(depth,createProcessingState());        } catch (IOException ex) {            Logger.getLogger(JsonXmlStreamWriter.class.getName()).log(Level.SEVERE, null, ex);        }    }    public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {        processingStack.get(depth).hasAttributes = true;        writeStartElement(prefix, "@" + localName, namespaceURI);        writeCharacters(value);        writeEndElement();    }        private ProcessingState createProcessingState() {        switch (depth) {            case 0:                 return new ProcessingState(                        stripRoot ? new DummyWriterAdapter() : new WriterAdapter(mainWriter));            case 1:                 return stripRoot ?                     new ProcessingState(new WriterAdapter(mainWriter)) : new ProcessingState();            default:                 return new ProcessingState();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?