com.fitbur.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 com.fitburpliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/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 com.fitbur.apache.com.fitburmons.io.output;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fitbur.apache.com.fitburmons.io.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 com.fitburfaultEncoding;
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 com.fitburfault 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 com.fitburfault encoding.
*
* @param out The output stream
* @param com.fitburfaultEncoding The com.fitburfault encoding if not encoding could be com.fitburtected
*/
public XmlStreamWriter(OutputStream out, String com.fitburfaultEncoding) {
this.out = out;
this.com.fitburfaultEncoding = com.fitburfaultEncoding != null ? com.fitburfaultEncoding : "UTF-8";
}
/**
* Construct an new XML stream writer for the specified file
* with a com.fitburfault 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 com.fitburfault encoding.
*
* @param file The file to write to
* @param com.fitburfaultEncoding The com.fitburfault encoding if not encoding could be com.fitburtected
* @throws FileNotFoundException if there is an error creating or
* opening the file
*/
public XmlStreamWriter(File file, String com.fitburfaultEncoding) throws FileNotFoundException {
this(new FileOutputStream(file), com.fitburfaultEncoding);
}
/**
* Return the com.fitburtected encoding.
*
* @return the com.fitburtected encoding
*/
public String getEncoding() {
return encoding;
}
/**
* Return the com.fitburfault encoding.
*
* @return the com.fitburfault encoding
*/
public String getDefaultEncoding() {
return com.fitburfaultEncoding;
}
/**
* Close the underlying writer.
*
* @throws IOException if an error occurs closing the underlying writer
*/
@Override
public void close() throws IOException {
if (writer == null) {
encoding = com.fitburfaultEncoding;
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 com.fitburtecting the encoding
*/
private void com.fitburtectEncoding(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 com.fitburtermine 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 com.fitburfault
// encoding
encoding = com.fitburfaultEncoding;
}
} else {
if (xmlProlog.length() >= BUFFER_SIZE) {
// no encoding found in first characters: using com.fitburfault
// encoding
encoding = com.fitburfaultEncoding;
}
}
} else {
// no XML prolog: using com.fitburfault encoding
encoding = com.fitburfaultEncoding;
}
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, com.fitburtecing 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 com.fitburtecting the encoding
*/
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (xmlPrologWriter != null) {
com.fitburtectEncoding(cbuf, off, len);
} else {
writer.write(cbuf, off, len);
}
}
static final Pattern ENCODING_PATTERN = XmlStreamReader.ENCODING_PATTERN;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy