Java Code Examples for org.xml.sax.InputSource.setByteStream()

The following are Jave code examples for showing how to use setByteStream() of the org.xml.sax.InputSource class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: ParsingSupport.java   Source Code and License Vote up 6 votes
/**
 * Converts to InputSource and pass it.
 */    
protected TreeDocumentRoot parse(FileObject fo) throws IOException, TreeException{
    
    try {
        URL url = fo.getURL();
        InputSource in = new InputSource(url.toExternalForm());  //!!! we could try ti get encoding from MIME content type
        in.setByteStream(fo.getInputStream());
        return parse(in);
        
    } catch (IOException ex) {
        ErrorManager emgr = ErrorManager.getDefault();
        emgr.annotate(ex, Util.THIS.getString("MSG_can_not_create_URL"));
        emgr.notify(ex);
    }           
    return null;
}
 
Example 2
Project: OpenJSharp   File: SAXBufferCreator.java   Source Code and License Vote up 6 votes
public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
    if (_buffer == null) {
        createBuffer();
    }
    _buffer.setSystemId(systemId);
    reader.setContentHandler(this);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);

    try {
        setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
    } catch (SAXException e) {
    }


    if (systemId != null) {
        InputSource s = new InputSource(systemId);
        s.setByteStream(in);
        reader.parse(s);
    } else {
        reader.parse(new InputSource(in));
    }

    return getXMLStreamBuffer();
}
 
Example 3
Project: OpenJSharp   File: SAXSource.java   Source Code and License Vote up 6 votes
/**
 * Attempt to obtain a SAX InputSource object from a Source
 * object.
 *
 * @param source Must be a non-null Source reference.
 *
 * @return An InputSource, or null if Source can not be converted.
 */
public static InputSource sourceToInputSource(Source source) {

    if (source instanceof SAXSource) {
        return ((SAXSource) source).getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource ss      = (StreamSource) source;
        InputSource  isource = new InputSource(ss.getSystemId());

        isource.setByteStream(ss.getInputStream());
        isource.setCharacterStream(ss.getReader());
        isource.setPublicId(ss.getPublicId());

        return isource;
    } else {
        return null;
    }
}
 
Example 4
Project: incubator-netbeans   File: ConnectionBuilder.java   Source Code and License Vote up 5 votes
/**
 * Parses content as XML.
 * @throws IOException see {@link #connection} for subtype description; also thrown in case of malformed XML
 */
public Document parseXML() throws IOException {
    URLConnection c = connection();
    InputSource source = new InputSource(url.toString());
    source.setByteStream(c.getInputStream());
    try {
        return XMLUtil.parse(source, false, false, XMLUtil.defaultErrorHandler(), null);
    } catch (SAXException x) {
        throw new IOException(x);
    }
}
 
Example 5
Project: tomcat7   File: ContextConfig.java   Source Code and License Vote up 5 votes
@Override
public void scan(JarURLConnection jarConn) throws IOException {

    URL url = jarConn.getURL();
    URL resourceURL = jarConn.getJarFileURL();
    Jar jar = null;
    InputStream is = null;
    WebXml fragment = new WebXml();

    try {
        jar = JarFactory.newInstance(url);
        if (parseRequired || context.getXmlValidation()) {
            is = jar.getInputStream(FRAGMENT_LOCATION);
        }

        if (is == null) {
            // If there is no web-fragment.xml to process there is no
            // impact on distributable
            fragment.setDistributable(true);
        } else {
            InputSource source = new InputSource(
                    "jar:" + resourceURL.toString() + "!/" +
                    FRAGMENT_LOCATION);
            source.setByteStream(is);
            parseWebXml(source, fragment, true);
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
        fragment.setURL(url);
        if (fragment.getName() == null) {
            fragment.setName(fragment.getURL().toString());
        }
        fragment.setJarName(extractJarFileName(url));
        fragments.put(fragment.getName(), fragment);
    }
}
 
Example 6
Project: myfaces-trinidad   File: RegionMetadata.java   Source Code and License Vote up 5 votes
private static void _readRegionMetadata(
  RegionMetadata bean, InputStream in, String publicId)
{
  if (_LOG.isFiner())
  {
    _LOG.finer("Loading region-metadata from file:{0}", publicId);
  }
  try
  {
    InputSource input = new InputSource();
    input.setByteStream(in);
    input.setPublicId(publicId);

    DefaultHandler handler = new Handler(bean);
    _SAX_PARSER_FACTORY.newSAXParser().parse(input, handler);
  }
  catch (IOException ioe)
  {
    _error(publicId, ioe);
  }
  catch (ParserConfigurationException pce)
  {
    _error(publicId, pce);
  }
  catch (SAXException saxe)
  {
    _error(publicId, saxe);
  }
}
 
Example 7
Project: defense-solutions-proofs-of-concept   File: CoTAdapterService.java   Source Code and License Vote up 5 votes
private void loadXSD(String resource, FieldDefinition detailsDef )
{
	try
	{
		InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
		InputSource source = new InputSource();
		source.setByteStream(is);
		CoTDetailsDeff.parseXSD( source, detailsDef);
	}catch(Exception ex)
	{
		ex.printStackTrace();
	}
}
 
Example 8
Project: openjdk-jdk10   File: AbstractUnmarshallerImpl.java   Source Code and License Vote up 5 votes
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}
 
Example 9
Project: OpenJSharp   File: UnmarshallerImpl.java   Source Code and License Vote up 5 votes
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}
 
Example 10
Project: OpenJSharp   File: AbstractUnmarshallerImpl.java   Source Code and License Vote up 5 votes
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}
 
Example 11
Project: OpenJSharp   File: ValidatorHandlerImpl.java   Source Code and License Vote up 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 12
Project: OpenJSharp   File: BootstrapResolver.java   Source Code and License Vote up 5 votes
/** SAX resolveEntity API. */
public InputSource resolveEntity (String publicId, String systemId) {
  String resolved = null;

  if (systemId != null && systemMap.containsKey(systemId)) {
    resolved = (String) systemMap.get(systemId);
  } else if (publicId != null && publicMap.containsKey(publicId)) {
    resolved = (String) publicMap.get(publicId);
  }

  if (resolved != null) {
    try {
      InputSource iSource = new InputSource(resolved);
      iSource.setPublicId(publicId);

      // Ideally this method would not attempt to open the
      // InputStream, but there is a bug (in Xerces, at least)
      // that causes the parser to mistakenly open the wrong
      // system identifier if the returned InputSource does
      // not have a byteStream.
      //
      // It could be argued that we still shouldn't do this here,
      // but since the purpose of calling the entityResolver is
      // almost certainly to open the input stream, it seems to
      // do little harm.
      //
      URL url = new URL(resolved);
      InputStream iStream = url.openStream();
      iSource.setByteStream(iStream);

      return iSource;
    } catch (Exception e) {
      // FIXME: silently fail?
      return null;
    }
  }

  return null;
}
 
Example 13
Project: OpenJSharp   File: ResolvingXMLFilter.java   Source Code and License Vote up 5 votes
/**
 * Implements the <code>resolveEntity</code> method
 * for the SAX interface, using an underlying CatalogResolver
 * to do the real work.
 */
public InputSource resolveEntity (String publicId, String systemId) {
  allowXMLCatalogPI = false;
  String resolved = catalogResolver.getResolvedEntity(publicId, systemId);

  if (resolved == null && piCatalogResolver != null) {
    resolved = piCatalogResolver.getResolvedEntity(publicId, systemId);
  }

  if (resolved != null) {
    try {
      InputSource iSource = new InputSource(resolved);
      iSource.setPublicId(publicId);

      // Ideally this method would not attempt to open the
      // InputStream, but there is a bug (in Xerces, at least)
      // that causes the parser to mistakenly open the wrong
      // system identifier if the returned InputSource does
      // not have a byteStream.
      //
      // It could be argued that we still shouldn't do this here,
      // but since the purpose of calling the entityResolver is
      // almost certainly to open the input stream, it seems to
      // do little harm.
      //
      URL url = new URL(resolved);
      InputStream iStream = url.openStream();
      iSource.setByteStream(iStream);

      return iSource;
    } catch (Exception e) {
      catalogManager.debug.message(1, "Failed to create InputSource", resolved);
      return null;
    }
  } else {
    return null;
  }
}
 
Example 14
Project: OpenJSharp   File: CatalogResolver.java   Source Code and License Vote up 5 votes
/**
 * Implements the <code>resolveEntity</code> method
 * for the SAX interface.
 *
 * <p>Presented with an optional public identifier and a system
 * identifier, this function attempts to locate a mapping in the
 * catalogs.</p>
 *
 * <p>If such a mapping is found, the resolver attempts to open
 * the mapped value as an InputSource and return it. Exceptions are
 * ignored and null is returned if the mapped value cannot be opened
 * as an input source.</p>
 *
 * <p>If no mapping is found (or an error occurs attempting to open
 * the mapped value as an input source), null is returned and the system
 * will use the specified system identifier as if no entityResolver
 * was specified.</p>
 *
 * @param publicId  The public identifier for the entity in question.
 * This may be null.
 *
 * @param systemId  The system identifier for the entity in question.
 * XML requires a system identifier on all external entities, so this
 * value is always specified.
 *
 * @return An InputSource for the mapped identifier, or null.
 */
public InputSource resolveEntity (String publicId, String systemId) {
  String resolved = getResolvedEntity(publicId, systemId);

  if (resolved != null) {
    try {
      InputSource iSource = new InputSource(resolved);
      iSource.setPublicId(publicId);

      // Ideally this method would not attempt to open the
      // InputStream, but there is a bug (in Xerces, at least)
      // that causes the parser to mistakenly open the wrong
      // system identifier if the returned InputSource does
      // not have a byteStream.
      //
      // It could be argued that we still shouldn't do this here,
      // but since the purpose of calling the entityResolver is
      // almost certainly to open the input stream, it seems to
      // do little harm.
      //
      URL url = new URL(resolved);
      InputStream iStream = url.openStream();
      iSource.setByteStream(iStream);

      return iSource;
    } catch (Exception e) {
      catalogManager.debug.message(1, "Failed to create InputSource", resolved);
      return null;
    }
  }

  return null;
}
 
Example 15
Project: defense-solutions-proofs-of-concept   File: CoTAdapterService.java   Source Code and License Vote up 5 votes
private void loadXSD(String resource, FieldDefinition detailsDef )
{
	try
	{
		InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
		InputSource source = new InputSource();
		source.setByteStream(is);
		CoTDetailsDeff.parseXSD( source, detailsDef);
	}catch(Exception ex)
	{
		ex.printStackTrace();
	}
}
 
Example 16
Project: apache-tomcat-7.0.73-with-comment   File: ContextConfig.java   Source Code and License Vote up 5 votes
@Override
public void scan(JarURLConnection jarConn) throws IOException {

    URL url = jarConn.getURL();
    URL resourceURL = jarConn.getJarFileURL();
    Jar jar = null;
    InputStream is = null;
    WebXml fragment = new WebXml();

    try {
        jar = JarFactory.newInstance(url);
        if (parseRequired || context.getXmlValidation()) {
            is = jar.getInputStream(FRAGMENT_LOCATION);
        }

        if (is == null) {
            // If there is no web-fragment.xml to process there is no
            // impact on distributable
            fragment.setDistributable(true);
        } else {
            InputSource source = new InputSource(
                    "jar:" + resourceURL.toString() + "!/" +
                    FRAGMENT_LOCATION);
            source.setByteStream(is);
            parseWebXml(source, fragment, true);
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
        fragment.setURL(url);
        if (fragment.getName() == null) {
            fragment.setName(fragment.getURL().toString());
        }
        fragment.setJarName(extractJarFileName(url));
        fragments.put(fragment.getName(), fragment);
    }
}
 
Example 17
Project: apache-tomcat-7.0.73-with-comment   File: ContextConfig.java   Source Code and License Vote up 5 votes
@Override
public void scan(File file) throws IOException {

    InputStream stream = null;
    WebXml fragment = new WebXml();

    try {
        File fragmentFile = new File(file, FRAGMENT_LOCATION);
        if (fragmentFile.isFile()) {
            stream = new FileInputStream(fragmentFile);
            InputSource source =
                new InputSource(fragmentFile.toURI().toURL().toString());
            source.setByteStream(stream);
            parseWebXml(source, fragment, true);
        } else {
            // If there is no web.xml, normal folder no impact on
            // distributable
            fragment.setDistributable(true);
        }
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
        fragment.setURL(file.toURI().toURL());
        if (fragment.getName() == null) {
            fragment.setName(fragment.getURL().toString());
        }
        fragment.setJarName(file.getName());
        fragments.put(fragment.getName(), fragment);
    }
}
 
Example 18
Project: defense-solutions-proofs-of-concept   File: CoTInboundAdapterDefinition.java   Source Code and License Vote up 5 votes
private void loadXSD(String resource, FieldDefinition detailsDef )
{
	try
	{
		InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
		InputSource source = new InputSource();
		source.setByteStream(is);
		CoTDetailsDeff.parseXSD( source, detailsDef);
	}catch(Exception ex)
	{
		ex.printStackTrace();
	}
}
 
Example 19
Project: openjdk-jdk10   File: XmlUtil.java   Source Code and License Vote up 5 votes
private static InputSource toInputSource(StreamSource src) {
    InputSource is = new InputSource();
    is.setByteStream(src.getInputStream());
    is.setCharacterStream(src.getReader());
    is.setPublicId(src.getPublicId());
    is.setSystemId(src.getSystemId());
    return is;
}
 
Example 20
Project: openjdk-jdk10   File: UnmarshallerImpl.java   Source Code and License Vote up 5 votes
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}