Java Code Examples for org.xml.sax.InputSource#setEncoding()
The following examples show how to use
org.xml.sax.InputSource#setEncoding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMarshaller.java From spring-analysis-note with MIT License | 6 votes |
/** * Template method for handling {@code StreamSource}s. * <p>This implementation delegates to {@code unmarshalInputStream} or {@code unmarshalReader}. * @param streamSource the {@code StreamSource} * @return the object graph * @throws IOException if an I/O exception occurs * @throws XmlMappingException if the given source cannot be mapped to an object */ protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException { if (streamSource.getInputStream() != null) { if (isProcessExternalEntities() && isSupportDtd()) { return unmarshalInputStream(streamSource.getInputStream()); } else { InputSource inputSource = new InputSource(streamSource.getInputStream()); inputSource.setEncoding(getDefaultEncoding()); return unmarshalSaxSource(new SAXSource(inputSource)); } } else if (streamSource.getReader() != null) { if (isProcessExternalEntities() && isSupportDtd()) { return unmarshalReader(streamSource.getReader()); } else { return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader()))); } } else { return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId()))); } }
Example 2
Source File: XmlFeedReader.java From vespa with Apache License 2.0 | 6 votes |
public static void read(InputStream inputStream, FeedClient feedClient, AtomicInteger numSent) throws Exception { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); // XXE prevention: parserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); parserFactory.setValidating(false); parserFactory.setNamespaceAware(false); SAXParser parser = parserFactory.newSAXParser(); SAXClientFeeder saxClientFeeder = new SAXClientFeeder(feedClient, numSent); InputSource inputSource = new InputSource(); inputSource.setEncoding(StandardCharsets.UTF_8.displayName()); inputSource.setByteStream(inputStream); // This is to send events about CDATA to the saxClientFeeder // (https://docs.oracle.com/javase/tutorial/jaxp/sax/events.html) parser.setProperty("http://xml.org/sax/properties/lexical-handler", saxClientFeeder); parser.parse(inputSource, saxClientFeeder); }
Example 3
Source File: DeviceWebDriver.java From xframium-java with GNU General Public License v3.0 | 6 votes |
public void readXML( String pageSource ) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); InputStreamReader streamReader = new InputStreamReader( new ByteArrayInputStream( pageSource.getBytes() ), "UTF-8" ); InputSource inputSource = new InputSource( streamReader ); inputSource.setEncoding( "UTF-8" ); cachedDocument = dBuilder.parse( inputSource ); cachingEnabled = true; } catch ( Exception e ) { log.warn( "CACHING HAS BEEN DISABLED", e ); cachingEnabled = false; cachedDocument = null; } }
Example 4
Source File: XmlStorage.java From jolie with GNU Lesser General Public License v2.1 | 6 votes |
private Value valueFromFile() throws FaultException { Value value = Value.create(); try { try( InputStream istream = new FileInputStream( xmlFile ) ) { DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); InputSource src = new InputSource( new InputStreamReader( istream ) ); if( charset != null ) { src.setEncoding( charset.name() ); } Document doc = builder.parse( src ); jolie.xml.XmlUtils.documentToValue( doc, value, false ); } } catch( Exception e ) { throw new FaultException( "StorageFault", e.getMessage() ); } return value; }
Example 5
Source File: FileService.java From jolie with GNU Lesser General Public License v2.1 | 6 votes |
private void readXMLIntoValueForStoring( InputStream istream, Value value, Charset charset ) throws IOException { try { DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); InputSource src = new InputSource( new InputStreamReader( istream ) ); if( charset != null ) { src.setEncoding( charset.name() ); } Document doc = builder.parse( src ); jolie.xml.XmlUtils.storageDocumentToValue( doc, value.getFirstChild( doc.getDocumentElement().getNodeName() ) ); } catch( ParserConfigurationException | SAXException e ) { throw new IOException( e ); } }
Example 6
Source File: MaeXMLParser.java From mae-annotation with GNU General Public License v3.0 | 5 votes |
private void parse(File utf8file) throws IOException, SAXException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Reader r = new InputStreamReader(new FileInputStream(utf8file), StandardCharsets.UTF_8); InputSource source = new InputSource(r); source.setEncoding(StandardCharsets.UTF_8.name()); saxParser.parse(source, xmlHandler); } catch (ParserConfigurationException e) { e.printStackTrace(); } }
Example 7
Source File: ValidatorHandlerImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * 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; }
Example 8
Source File: HttpProtocol.java From jolie with GNU Lesser General Public License v2.1 | 5 votes |
private void parseXML( HttpMessage message, Value value, String charset ) throws IOException { try { if( message.size() > 0 ) { DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); InputSource src = new InputSource( new ByteArrayInputStream( message.content() ) ); src.setEncoding( charset ); Document doc = builder.parse( src ); XmlUtils.documentToValue( doc, value, false ); } } catch( ParserConfigurationException | SAXException pce ) { throw new IOException( pce ); } }
Example 9
Source File: ValidatorHandlerImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * 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; }
Example 10
Source File: HTTPLinkCheck.java From xframium-java with GNU General Public License v3.0 | 5 votes |
/** * Checks if a given xml is a valid format * @param inputDocument - String * @return boolean */ private Document getDocument( URL inputUrl, URL referencingUrl ) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputStream iS = toHTML( inputUrl.openStream() ); if ( iS != null ) { InputStreamReader streamReader = new InputStreamReader( iS, "UTF-8" ); InputSource inputSource = new InputSource( streamReader ); inputSource.setEncoding( "UTF-8" ); return builder.parse( inputSource ); } else return null; } catch( FileNotFoundException t) { brokenLinks.add( inputUrl.toString() + " <-- " + referencingUrl.toString() ); System.err.println( "Broken Link: " + inputUrl.toString() + " referenced from " + referencingUrl.toString() ); return null; } catch( Exception e ) { return null; } }
Example 11
Source File: XmlRpcResponse.java From smarthome with Eclipse Public License 2.0 | 5 votes |
/** * Decodes a XML-RPC message from the given InputStream. */ public XmlRpcResponse(InputStream is, String encoding) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); InputSource inputSource = new InputSource(is); inputSource.setEncoding(encoding); saxParser.parse(inputSource, new XmlRpcHandler()); }
Example 12
Source File: XmlBeanDefinitionReader.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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(); } } }
Example 13
Source File: XmlBeanDefinitionReader.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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(); } } }
Example 14
Source File: ValidatorHandlerImpl.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
/** * 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; }
Example 15
Source File: CompactParseable.java From hottub with GNU General Public License v2.0 | 4 votes |
public ParsedPattern parseExternal(String uri, SchemaBuilder sb, Scope scope, String inheritedNs) throws BuildException, IllegalSchemaException { InputSource tem = new InputSource(uri); tem.setEncoding(in.getEncoding()); return new CompactSyntax(this, makeReader(tem), uri, sb, eh, inheritedNs).parse(scope); }
Example 16
Source File: XmlReport.java From livingdoc-core with GNU General Public License v3.0 | 4 votes |
private XmlReport(InputSource source) throws SAXException, IOException { source.setEncoding("UTF-8"); dom = newDocumentBuilder().parse(source); root = dom.getDocumentElement(); }
Example 17
Source File: CompactParseable.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public ParsedPattern parseExternal(String uri, SchemaBuilder sb, Scope scope, String inheritedNs) throws BuildException, IllegalSchemaException { InputSource tem = new InputSource(uri); tem.setEncoding(in.getEncoding()); return new CompactSyntax(this, makeReader(tem), uri, sb, eh, inheritedNs).parse(scope); }
Example 18
Source File: CompactParseable.java From hottub with GNU General Public License v2.0 | 4 votes |
public ParsedPattern parseInclude(String uri, SchemaBuilder sb, IncludedGrammar g, String inheritedNs) throws BuildException, IllegalSchemaException { InputSource tem = new InputSource(uri); tem.setEncoding(in.getEncoding()); return new CompactSyntax(this, makeReader(tem), uri, sb, eh, inheritedNs).parseInclude(g); }
Example 19
Source File: Web.java From appinventor-extensions with Apache License 2.0 | 4 votes |
/** * Decodes the given XML string to produce a dictionary structure. The dictionary includes the * special keys `$tag`, `$localName`, `$namespace`, `$namespaceUri`, `$attributes`, and `$content`, * as well as a key for each unique tag for every node, which points to a list of elements of * the same structure as described here. * * The `$tag` key is the full tag name, e.g., foo:bar. The `$localName` is the local portion of * the name (everything after the colon `:` character). If a namespace is given (everything before * the colon `:` character), it is provided in `$namespace` and the corresponding URI is given * in `$namespaceUri`. The attributes are stored in a dictionary in `$attributes` and the * child nodes are given as a list under `$content`. * * **More Information on Special Keys** * * Consider the following XML document: * * ```xml * <ex:Book xmlns:ex="http://example.com/"> * <ex:title xml:lang="en">On the Origin of Species</ex:title> * <ex:author>Charles Darwin</ex:author> * </ex:Book> * ``` * * When parsed, the `$tag` key will be `"ex:Book"`, the `$localName` key will be `"Book"`, the * `$namespace` key will be `"ex"`, `$namespaceUri` will be `"http://example.com/"`, the * `$attributes` key will be a dictionary `{}` (xmlns is removed for the namespace), and the * `$content` will be a list of two items representing the decoded `<ex:title>` and `<ex:author>` * elements. The first item, which corresponds to the `<ex:title>` element, will have an * `$attributes` key containing the dictionary `{"xml:lang": "en"}`. For each `name=value` * attribute on an element, a key-value pair mapping `name` to `value` will exist in the * `$attributes` dictionary. In addition to these special keys, there will also be `"ex:title"` * and `"ex:author"` to allow lookups faster than having to traverse the `$content` list. * * @param XmlText the JSON text to decode * @return the decoded text */ @SimpleFunction(description = "Decodes the given XML into a set of nested dictionaries that " + "capture the structure and data contained in the XML. See the help for more details.") public Object XMLTextDecodeAsDictionary(String XmlText) { try { XmlParser p = new XmlParser(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); InputSource is = new InputSource(new StringReader(XmlText)); is.setEncoding("UTF-8"); parser.parse(is, p); return p.getRoot(); } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); form.dispatchErrorOccurredEvent(this, "XMLTextDecodeAsDictionary", ErrorMessages.ERROR_WEB_JSON_TEXT_DECODE_FAILED, e.getMessage()); return new YailDictionary(); } }
Example 20
Source File: CompactParseable.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public ParsedPattern parseExternal(String uri, SchemaBuilder sb, Scope scope, String inheritedNs) throws BuildException, IllegalSchemaException { InputSource tem = new InputSource(uri); tem.setEncoding(in.getEncoding()); return new CompactSyntax(this, makeReader(tem), uri, sb, eh, inheritedNs).parse(scope); }