本文整理汇总了Java中org.xml.sax.InputSource.setEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java InputSource.setEncoding方法的具体用法?Java InputSource.setEncoding怎么用?Java InputSource.setEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xml.sax.InputSource
的用法示例。
在下文中一共展示了InputSource.setEncoding方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: load
import org.xml.sax.InputSource; //导入方法依赖的package包/类
public static SyntaxScheme load(Font baseFont, InputStream in)
throws IOException {
SyntaxSchemeLoader parser = null;
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
parser = new SyntaxSchemeLoader(baseFont);
parser.baseFont = baseFont;
reader.setContentHandler(parser);
InputSource is = new InputSource(in);
is.setEncoding("UTF-8");
reader.parse(is);
} catch (SAXException se) {
throw new IOException(se.toString());
}
return parser.scheme;
}
示例2: parseText
import org.xml.sax.InputSource; //导入方法依赖的package包/类
public static Document parseText(String text) throws DocumentException, SAXException {
SAXReader reader = new SAXReader(false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
reader.setFeature("http://apache.org/xml/features/validation/schema", false);
String encoding = getEncoding(text);
InputSource source = new InputSource(new StringReader(text));
source.setEncoding(encoding);
Document result = reader.read(source);
// if the XML parser doesn't provide a way to retrieve the encoding,
// specify it manually
if (result.getXMLEncoding() == null) {
result.setXMLEncoding(encoding);
}
return result;
}
示例3: modifyXml
import org.xml.sax.InputSource; //导入方法依赖的package包/类
@Override
public String modifyXml(Reader reader, HtmlContentHandler writer)
{
InputSource s = new InputSource();
s.setEncoding(Constants.UTF8);
s.setCharacterStream(reader);
try
{
XMLReader r = new Parser();
r.setContentHandler(writer);
r.parse(s);
return writer.getOutput();
}
catch( Exception e )
{
throw new RuntimeException(e);
}
}
示例4: inform
import org.xml.sax.InputSource; //导入方法依赖的package包/类
@Override
public void inform(ResourceLoader loader) throws IOException {
InputStream stream = null;
try {
if (dictFile != null) // the dictionary can be empty.
dictionary = getWordSet(loader, dictFile, false);
// TODO: Broken, because we cannot resolve real system id
// ResourceLoader should also supply method like ClassLoader to get resource URL
stream = loader.openResource(hypFile);
final InputSource is = new InputSource(stream);
is.setEncoding(encoding); // if it's null let xml parser decide
is.setSystemId(hypFile);
if (luceneMatchVersion.onOrAfter(Version.LUCENE_4_4_0)) {
hyphenator = HyphenationCompoundWordTokenFilter.getHyphenationTree(is);
} else {
hyphenator = Lucene43HyphenationCompoundWordTokenFilter.getHyphenationTree(is);
}
} finally {
IOUtils.closeWhileHandlingException(stream);
}
}
示例5: load
import org.xml.sax.InputSource; //导入方法依赖的package包/类
public static void load(ThemeNULL theme, InputStream in) throws IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
try {
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
XmlHandler handler = new XmlHandler();
handler.theme = theme;
reader.setEntityResolver(handler);
reader.setContentHandler(handler);
reader.setDTDHandler(handler);
reader.setErrorHandler(handler);
InputSource is = new InputSource(in);
is.setEncoding("UTF-8");
reader.parse(is);
} catch (/*SAX|ParserConfiguration*/Exception se) {
se.printStackTrace();
throw new IOException(se.toString());
}
}
示例6: examineSOAPResponse
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* Convenience method to examine and parse SOAP response. Caller handles
* whatever exception may be thrown. Note that the Content-Type from the
* SHEX server will be application/soap+xml, whereas the Content-Type from
* MEX will be "text/xml". Accordingly we seek to parse either.
*
* @param conn
* @param inp
* @param handler
* @param contentType
* @return Pair of String, Boolean. The String is an accumulated message
* (which may be blank) and the boolean is true (parsed), false
* (failed parse)
* @throws Exception
*/
private Pair<String, Boolean> examineSOAPResponse(HttpURLConnection conn, InputStream inp, DefaultHandler handler,
String contentType) throws Exception
{
String responseMessage = "";
boolean parsedResponse = false;
String responseContentType = conn.getContentType();
// which ever xml variant the contentType is, "text/xml",
// "application/soap+xml"
if( responseContentType != null
&& (responseContentType.toLowerCase().startsWith(contentType) || responseContentType.toLowerCase()
.contains(DEFAULT_CONTENT_TYPE)) )
{
SAXParser saxParser = factory.newSAXParser();
if( inp != null )
{
InputSource inpsrc = new InputSource(inp);
inpsrc.setEncoding("UTF-8");
try
{
saxParser.parse(inpsrc, handler);
parsedResponse = true;
}
catch( Exception parsee )
{
// sigh - leave the boolean false and let the caller try as
// plain text, but add what error info we have
responseMessage += "saxParserException:\n";
responseMessage += parsee.getLocalizedMessage();
responseMessage += '\n';
}
}
}
return new Pair<String, Boolean>(responseMessage, parsedResponse);
}
示例7: loadBeanDefinitions
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* Load bean definitions from the specified XML file.
* @param encodedResource the resource descriptor for the XML file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isInfoEnabled()) {
logger.info("Loading XML bean definitions from " + encodedResource.getResource());
}
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet<EncodedResource>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try {
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
示例8: Macro
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* Loads a macro from a file on disk.
*
* @param file The file from which to load the macro.
* @throws IOException If the file does not exist or an I/O exception occurs
* while reading the file.
* @see #saveToFile(String)
* @see #saveToFile(File)
*/
public Macro(File file) throws IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
//InputSource is = new InputSource(new FileReader(file));
InputSource is = new InputSource(new UnicodeReader(
new FileInputStream(file), FILE_ENCODING));
is.setEncoding(FILE_ENCODING);
doc = db.parse(is);//db.parse(file);
} catch (Exception e) {
e.printStackTrace();
String desc = e.getMessage();
if (desc==null) {
desc = e.toString();
}
throw new IOException("Error parsing XML: " + desc);
}
macroRecords = new ArrayList<MacroRecord>();
// Traverse the XML tree.
boolean parsedOK = initializeFromXMLFile(doc.getDocumentElement());
if (!parsedOK) {
name = null;
macroRecords.clear();
macroRecords = null;
throw new IOException("Error parsing XML!");
}
}
示例9: readerXml
import org.xml.sax.InputSource; //导入方法依赖的package包/类
public static Element readerXml(String body,String encode) throws DocumentException {
SAXReader reader = new SAXReader(false);
InputSource source = new InputSource(new StringReader(body));
source.setEncoding(encode);
Document doc = reader.read(source);
Element element = doc.getRootElement();
return element;
}
示例10: toMap
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* 转XMLmap
* @author
* @param xmlBytes
* @param charset
* @return
* @throws Exception
*/
public static Map<String, String> toMap(byte[] xmlBytes,String charset) throws Exception{
SAXReader reader = new SAXReader(false);
InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
source.setEncoding(charset);
Document doc = reader.read(source);
Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
return params;
}
示例11: resolveEntity
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
示例12: onAsyncStream
import org.xml.sax.InputSource; //导入方法依赖的package包/类
@Override
protected final void onAsyncStream(InputStream stream) throws Exception
{
TaskXMLHandler handler = new TaskXMLHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputSource is = new InputSource(stream);
if (mEncoding != null) {
is.setEncoding(mEncoding);
}
parser.parse(is, handler);
}
示例13: resolveEntity
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(sysId, baseSystemId) : sysId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
示例14: toInputSource
import org.xml.sax.InputSource; //导入方法依赖的package包/类
public InputSource toInputSource() {
final InputSource is = new InputSource(new ByteArrayInputStream(data));
is.setEncoding(charset.name());
return is;
}
示例15: call
import org.xml.sax.InputSource; //导入方法依赖的package包/类
/**
* @param params
* @return
* @throws LmsUserNotFoundException, MoodleException
*/
public XmlDocument call(Map<String, String> params) throws LmsUserNotFoundException
{
final Request request = new Request(url);
request.setAccept("application/xml; charset=utf-8");
request.setMethod(Method.POST);
final List<NameValue> nvs = Lists.newArrayList();
for( Entry<String, String> entry : params.entrySet() )
{
nvs.add(new NameValue(entry.getKey(), entry.getValue()));
}
request.setHtmlForm(nvs);
try( Response response = httpService.getWebContent(request, configService.getProxyDetails()) )
{
final EntityStrippingWriter writer = new EntityStrippingWriter(new StringWriter());
final InputSource s = new InputSource();
s.setEncoding(Constants.UTF8);
s.setCharacterStream(new UnicodeReader(response.getInputStream(), "UTF-8"));
final XMLReader r = new Parser();
r.setContentHandler(writer);
r.parse(s);
final XmlDocument xml = new XmlDocument(writer.getOutput());
// Used for testing
if( LOGGER.isDebugEnabled() )
{
LOGGER.debug("Read from server:");
LOGGER.debug(xml.toString());
}
// check to see if XML contains EXCEPTION as first node!
final Node exception = xml.node("EXCEPTION");
if( exception != null )
{
String message = xml.nodeValue("MESSAGE", exception);
if( message.startsWith(ERROR_USER_NOT_FOUND) )
{
String username = message.substring(ERROR_USER_NOT_FOUND.length());
throw new LmsUserNotFoundException(username,
CurrentLocale.get(getKey("connector.error"), username));
}
throw new MoodleException("Error contacting Moodle: " + message);
}
return xml;
}
catch( Exception e )
{
throw Throwables.propagate(e);
}
}