
org.testifyproject.apache.commons.io.output.XmlStreamWriter Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org.testifyproject/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testifyproject.apache.org.testifyproject.testifyprojectmons.org.testifyproject.testifyproject.output;
import java.org.testifyproject.testifyproject.File;
import java.org.testifyproject.testifyproject.FileNotFoundException;
import java.org.testifyproject.testifyproject.FileOutputStream;
import java.org.testifyproject.testifyproject.IOException;
import java.org.testifyproject.testifyproject.OutputStream;
import java.org.testifyproject.testifyproject.OutputStreamWriter;
import java.org.testifyproject.testifyproject.StringWriter;
import java.org.testifyproject.testifyproject.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.testifyproject.apache.org.testifyproject.testifyprojectmons.org.testifyproject.testifyproject.input.XmlStreamReader;
/**
* Character stream that handles all the necessary Voodo to figure out the
* charset encoding of the XML document written to the stream.
*
* @version $Id: XmlStreamWriter.java 1304052 2012-03-22 20:55:29Z ggregory $
* @see XmlStreamReader
* @since 2.0
*/
public class XmlStreamWriter extends Writer {
private static final int BUFFER_SIZE = 4096;
private final OutputStream out;
private final String org.testifyproject.testifyprojectfaultEncoding;
private StringWriter xmlPrologWriter = new StringWriter(BUFFER_SIZE);
private Writer writer;
private String encoding;
/**
* Construct an new XML stream writer for the specified output stream
* with a org.testifyproject.testifyprojectfault encoding of UTF-8.
*
* @param out The output stream
*/
public XmlStreamWriter(OutputStream out) {
this(out, null);
}
/**
* Construct an new XML stream writer for the specified output stream
* with the specified org.testifyproject.testifyprojectfault encoding.
*
* @param out The output stream
* @param org.testifyproject.testifyprojectfaultEncoding The org.testifyproject.testifyprojectfault encoding if not encoding could be org.testifyproject.testifyprojecttected
*/
public XmlStreamWriter(OutputStream out, String org.testifyproject.testifyprojectfaultEncoding) {
this.out = out;
this.org.testifyproject.testifyprojectfaultEncoding = org.testifyproject.testifyprojectfaultEncoding != null ? org.testifyproject.testifyprojectfaultEncoding : "UTF-8";
}
/**
* Construct an new XML stream writer for the specified file
* with a org.testifyproject.testifyprojectfault encoding of UTF-8.
*
* @param file The file to write to
* @throws FileNotFoundException if there is an error creating or
* opening the file
*/
public XmlStreamWriter(File file) throws FileNotFoundException {
this(file, null);
}
/**
* Construct an new XML stream writer for the specified file
* with the specified org.testifyproject.testifyprojectfault encoding.
*
* @param file The file to write to
* @param org.testifyproject.testifyprojectfaultEncoding The org.testifyproject.testifyprojectfault encoding if not encoding could be org.testifyproject.testifyprojecttected
* @throws FileNotFoundException if there is an error creating or
* opening the file
*/
public XmlStreamWriter(File file, String org.testifyproject.testifyprojectfaultEncoding) throws FileNotFoundException {
this(new FileOutputStream(file), org.testifyproject.testifyprojectfaultEncoding);
}
/**
* Return the org.testifyproject.testifyprojecttected encoding.
*
* @return the org.testifyproject.testifyprojecttected encoding
*/
public String getEncoding() {
return encoding;
}
/**
* Return the org.testifyproject.testifyprojectfault encoding.
*
* @return the org.testifyproject.testifyprojectfault encoding
*/
public String getDefaultEncoding() {
return org.testifyproject.testifyprojectfaultEncoding;
}
/**
* Close the underlying writer.
*
* @throws IOException if an error occurs closing the underlying writer
*/
@Override
public void close() throws IOException {
if (writer == null) {
encoding = org.testifyproject.testifyprojectfaultEncoding;
writer = new OutputStreamWriter(out, encoding);
writer.write(xmlPrologWriter.toString());
}
writer.close();
}
/**
* Flush the underlying writer.
*
* @throws IOException if an error occurs flushing the underlying writer
*/
@Override
public void flush() throws IOException {
if (writer != null) {
writer.flush();
}
}
/**
* Detect the encoding.
*
* @param cbuf the buffer to write the characters from
* @param off The start offset
* @param len The number of characters to write
* @throws IOException if an error occurs org.testifyproject.testifyprojecttecting the encoding
*/
private void org.testifyproject.testifyprojecttectEncoding(char[] cbuf, int off, int len)
throws IOException {
int size = len;
StringBuffer xmlProlog = xmlPrologWriter.getBuffer();
if (xmlProlog.length() + len > BUFFER_SIZE) {
size = BUFFER_SIZE - xmlProlog.length();
}
xmlPrologWriter.write(cbuf, off, size);
// try to org.testifyproject.testifyprojecttermine encoding
if (xmlProlog.length() >= 5) {
if (xmlProlog.substring(0, 5).equals("");
if (xmlPrologEnd > 0) {
// ok, full XML prolog written: let's extract encoding
Matcher m = ENCODING_PATTERN.matcher(xmlProlog.substring(0,
xmlPrologEnd));
if (m.find()) {
encoding = m.group(1).toUpperCase();
encoding = encoding.substring(1, encoding.length() - 1);
} else {
// no encoding found in XML prolog: using org.testifyproject.testifyprojectfault
// encoding
encoding = org.testifyproject.testifyprojectfaultEncoding;
}
} else {
if (xmlProlog.length() >= BUFFER_SIZE) {
// no encoding found in first characters: using org.testifyproject.testifyprojectfault
// encoding
encoding = org.testifyproject.testifyprojectfaultEncoding;
}
}
} else {
// no XML prolog: using org.testifyproject.testifyprojectfault encoding
encoding = org.testifyproject.testifyprojectfaultEncoding;
}
if (encoding != null) {
// encoding has been chosen: let's do it
xmlPrologWriter = null;
writer = new OutputStreamWriter(out, encoding);
writer.write(xmlProlog.toString());
if (len > size) {
writer.write(cbuf, off + size, len - size);
}
}
}
}
/**
* Write the characters to the underlying writer, org.testifyproject.testifyprojecttecing encoding.
*
* @param cbuf the buffer to write the characters from
* @param off The start offset
* @param len The number of characters to write
* @throws IOException if an error occurs org.testifyproject.testifyprojecttecting the encoding
*/
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (xmlPrologWriter != null) {
org.testifyproject.testifyprojecttectEncoding(cbuf, off, len);
} else {
writer.write(cbuf, off, len);
}
}
static final Pattern ENCODING_PATTERN = XmlStreamReader.ENCODING_PATTERN;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy