org.magicwerk.brownies.jdom.XmlSource Maven / Gradle / Ivy
/*
* Copyright 2013 by Thomas Mauch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/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.
*
* $Id$
*/
package org.magicwerk.brownies.jdom;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import org.magicwerk.brownies.core.reflect.ReflectTools;
import org.magicwerk.brownies.jdom.XmlProcessor.Validation;
import org.xml.sax.InputSource;
/**
* Specification of source for XML processing.
*
* @author Thomas Mauch
* @version $Id$
*/
public class XmlSource {
private InputSource inputSource;
private Validation validation = Validation.NONE;
private String schemaFile;
public XmlSource() {
}
public XmlSource(String url) {
setInput(url);
}
public XmlSource(XmlSource that) {
this.inputSource = that.inputSource;
this.validation = that.validation;
this.schemaFile = that.schemaFile;
}
/**
* Set URL used for reading input.
*
* @param url URL which is read to get input
* @return this (fluent interface)
*/
public XmlSource setInput(String url) {
this.inputSource = XmlProcessor.getInputSource(url);
return this;
}
/**
* Set string to be used as input.
*
* @param str string containing input
* @return this (fluent interface)
*/
public XmlSource setString(String str) {
this.inputSource = XmlProcessor.getInputSourceForString(str);
return this;
}
/**
* Note that the encoding only supports the canonical names as used in java.nio
* (see http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html)
* So use UTF-8 and ISO-8859-1 (instead of UTF8 and ISO8859_1), if null UTF-8 is used.
*
* @param url URL
* @param encoding encoding to use
* @return this (fluent interface)
*/
public XmlSource setInput(String url, String encoding) {
this.inputSource = XmlProcessor.getInputSource(url);
this.inputSource.setEncoding(encoding);
return this;
}
public XmlSource setInput(String url, Charset charset) {
this.inputSource = XmlProcessor.getInputSource(url);
this.inputSource.setEncoding(charset.name());
return this;
}
public XmlSource setInput(InputSource inputSource) {
this.inputSource = inputSource;
return this;
}
public XmlSource setValidation(Validation validation) {
this.validation = validation;
return this;
}
public XmlSource setSchemaFile(String schemaFile) {
this.validation = Validation.XSD;
this.schemaFile = schemaFile;
return this;
}
/**
* @return the inputSource
*/
public InputSource getInputSource() {
return inputSource;
}
/**
* @return the validation
*/
public Validation getValidation() {
return validation;
}
/**
* @return the schemaFile
*/
public String getSchemaFile() {
return schemaFile;
}
@Override
public String toString() {
return "XmlSource: inputSource=" + toString(inputSource);
}
static String toString(InputSource inputSource) {
String systemId = inputSource.getSystemId();
if (systemId != null) {
return "systemId= " + systemId;
}
Reader reader = inputSource.getCharacterStream();
if (reader instanceof StringReader) {
StringReader stringReader = (StringReader) reader;
return "StringReader= " + ReflectTools.getAnyFieldValue(stringReader, "str");
}
return inputSource.toString();
}
}