All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.xml.bind.v2.runtime.output;

import java.io.IOException;
import java.io.OutputStream;

import java.io.StringWriter;
import javax.xml.stream.XMLStreamException;

import com.sun.xml.bind.DatatypeConverterImpl;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import com.sun.xml.bind.v2.runtime.Name;
import com.sun.xml.bind.v2.runtime.XMLSerializer;
import com.sun.xml.bind.v2.runtime.MarshallerImpl;

import org.xml.sax.SAXException;

/**
 * {@link XmlOutput} implementation specialized for UTF-8.
 *
 * @author Kohsuke Kawaguchi
 * @author Paul Sandoz
 */
public class UTF8XmlOutput extends XmlOutputAbstractImpl {
    protected final OutputStream out;

    /** prefixes encoded. */
    private Encoded[] prefixes = new Encoded[8];

    /**
     * Of the {@link #prefixes}, number of filled entries.
     * This is almost the same as {@link NamespaceContextImpl#count()},
     * except that it allows us to handle contextual in-scope namespace bindings correctly.
     */
    private int prefixCount;

    /** local names encoded in UTF-8. All entries are pre-filled. */
    private final Encoded[] localNames;

    /** Temporary buffer used to encode text. */
    /* 
     * TODO
     * The textBuffer could write directly to the _octetBuffer
     * when encoding a string if Encoder is modified.
     * This will avoid an additional memory copy.
     */
    private final Encoded textBuffer = new Encoded();

    /** Buffer of octets for writing. */
    // TODO: Obtain buffer size from property on the JAXB context
    protected final byte[] octetBuffer = new byte[1024];
    
    /** Index in buffer to write to. */
    protected int octetBufferIndex;

    /**
     * Set to true to indicate that we need to write {@code '>'}
     * to close a start tag. Deferring the write of this char
     * allows us to write {@code "/>"} for empty elements.
     */
    protected boolean closeStartTagPending = false;

    /**
     * @see MarshallerImpl#header
     */
    private String header;

    private CharacterEscapeHandler escapeHandler = null;

    /**
     *
     * @param localNames
     *      local names encoded in UTF-8.
     */
    public UTF8XmlOutput(OutputStream out, Encoded[] localNames, CharacterEscapeHandler escapeHandler) {
        this.out = out;
        this.localNames = localNames;
        for( int i=0; i'} to close the start tag, if necessary.
     */
    protected final void closeStartTag() throws IOException {
        if(closeStartTagPending) {
            write('>');
            closeStartTagPending = false;
        }
    }

    public void beginStartTag(int prefix, String localName) throws IOException {
        closeStartTag();
        int base= pushNsDecls();
        write('<');
        writeName(prefix,localName);
        writeNsDecls(base);
    }

    @Override
    public void beginStartTag(Name name) throws IOException {
        closeStartTag();
        int base = pushNsDecls();
        write('<');
        writeName(name);
        writeNsDecls(base);
    }

    private int pushNsDecls() {
        int total = nsContext.count();
        NamespaceContextImpl.Element ns = nsContext.getCurrent();

        if(total > prefixes.length) {
            // reallocate
            int m = Math.max(total,prefixes.length*2);
            Encoded[] buf = new Encoded[m];
            System.arraycopy(prefixes,0,buf,0,prefixes.length);
            for( int i=prefixes.length; i');
        }
    }

    public void endTag(int prefix, String localName) throws IOException {
        if(closeStartTagPending) {
            write(EMPTY_TAG);
            closeStartTagPending = false;
        } else {
            write(CLOSE_TAG);
            writeName(prefix,localName);
            write('>');
        }
    }

    public void text(String value, boolean needSP) throws IOException {
        closeStartTag();
        if(needSP)
            write(' ');
        doText(value,false);
    }

    public void text(Pcdata value, boolean needSP) throws IOException {
        closeStartTag();
        if(needSP)
            write(' ');
        value.writeTo(this);
    }

    private void doText(String value,boolean isAttribute) throws IOException {
        if (escapeHandler != null) {
            StringWriter sw = new StringWriter();
            escapeHandler.escape(value.toCharArray(), 0, value.length(), isAttribute, sw);
            textBuffer.set(sw.toString());
        } else {
            textBuffer.setEscape(value, isAttribute);
        }
        textBuffer.write(this);
    }

    public final void text(int value) throws IOException {
        closeStartTag();
        /*
         * TODO
         * Change to use the octet buffer directly
         */

        // max is -2147483648 and 11 digits
        boolean minus = (value<0);
        textBuffer.ensureSize(11);
        byte[] buf = textBuffer.buf;
        int idx = 11;

        do {
            int r = value%10;
            if(r<0) r = -r;
            buf[--idx] = (byte)('0'|r);    // really measn 0x30+r but 0<=r<10, so bit-OR would do.
            value /= 10;
        } while(value!=0);

        if(minus)   buf[--idx] = (byte)'-';

        write(buf,idx,11-idx);
    }

    /**
     * Writes the given byte[] as base64 encoded binary to the output.
     *
     * 

* Being defined on this class allows this method to access the buffer directly, * which translates to a better performance. */ public void text(byte[] data, int dataLen) throws IOException { closeStartTag(); int start = 0; while(dataLen>0) { // how many bytes (in data) can we write without overflowing the buffer? int batchSize = Math.min(((octetBuffer.length-octetBufferIndex)/4)*3,dataLen); // write the batch octetBufferIndex = DatatypeConverterImpl._printBase64Binary(data,start,batchSize,octetBuffer,octetBufferIndex); if(batchSize * This method can be used somewhat like the {@code text} method, * but it doesn't perform character escaping. */ public final void write(int i) throws IOException { if (octetBufferIndex < octetBuffer.length) { octetBuffer[octetBufferIndex++] = (byte)i; } else { out.write(octetBuffer); octetBufferIndex = 1; octetBuffer[0] = (byte)i; } } protected final void write(byte[] b) throws IOException { write(b, 0, b.length); } protected final void write(byte[] b, int start, int length) throws IOException { if ((octetBufferIndex + length) < octetBuffer.length) { System.arraycopy(b, start, octetBuffer, octetBufferIndex, length); octetBufferIndex += length; } else { out.write(octetBuffer, 0, octetBufferIndex); out.write(b, start, length); octetBufferIndex = 0; } } protected final void flushBuffer() throws IOException { out.write(octetBuffer, 0, octetBufferIndex); octetBufferIndex = 0; } static byte[] toBytes(String s) { byte[] buf = new byte[s.length()]; for( int i=s.length()-1; i>=0; i-- ) buf[i] = (byte)s.charAt(i); return buf; } // per instance copy to prevent an attack where malicious OutputStream // rewrites the byte array. private final byte[] XMLNS_EQUALS = _XMLNS_EQUALS.clone(); private final byte[] XMLNS_COLON = _XMLNS_COLON.clone(); private final byte[] EQUALS = _EQUALS.clone(); private final byte[] CLOSE_TAG = _CLOSE_TAG.clone(); private final byte[] EMPTY_TAG = _EMPTY_TAG.clone(); private final byte[] XML_DECL = _XML_DECL.clone(); // masters private static final byte[] _XMLNS_EQUALS = toBytes(" xmlns=\""); private static final byte[] _XMLNS_COLON = toBytes(" xmlns:"); private static final byte[] _EQUALS = toBytes("=\""); private static final byte[] _CLOSE_TAG = toBytes(""); private static final byte[] _XML_DECL = toBytes(""); // no need to copy private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy