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

The following are Jave code examples for showing how to use setPublicId() 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: openjdk-jdk10   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 2
Project: sponge   File: DefaultConfigurationManager.java   Source Code and License Vote up 6 votes
@Override
public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException {
    if (ConfigurationConstants.CONFIG_SCHEMA.equals(systemId)) {
        InputStream stream = ConfigurationManager.class.getResourceAsStream(ConfigurationConstants.CONFIG_SCHEMA_LOCATION);
        if (stream != null) {
            InputSource source = new InputSource(stream);
            source.setPublicId(publicId);
            source.setSystemId(systemId);

            return source;
        } else {
            throw new SAXException("Cannot find schema " + ConfigurationConstants.CONFIG_SCHEMA);
        }
    }

    return super.resolveEntity(publicId, systemId);
}
 
Example 3
Project: OpenJSharp   File: XMLCatalogResolver.java   Source Code and License Vote up 6 votes
/**
 * <p>Resolves an external entity. If the entity cannot be
 * resolved, this method should return <code>null</code>. This
 * method returns an input source if an entry was found in the
 * catalog for the given external identifier. It should be
 * overrided if other behaviour is required.</p>
 *
 * @param publicId the public identifier, or <code>null</code> if none was supplied
 * @param systemId the system identifier
 *
 * @throws SAXException any SAX exception, possibly wrapping another exception
 * @throws IOException thrown if some i/o error occurs
 */
public InputSource resolveEntity(String publicId, String systemId)
     throws SAXException, IOException {

    String resolvedId = null;
    if (publicId != null && systemId != null) {
        resolvedId = resolvePublic(publicId, systemId);
    }
    else if (systemId != null) {
        resolvedId = resolveSystem(systemId);
    }

    if (resolvedId != null) {
        InputSource source = new InputSource(resolvedId);
        source.setPublicId(publicId);
        return source;
    }
    return null;
}
 
Example 4
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 5
Project: incubator-netbeans   File: AbstractCatalog.java   Source Code and License Vote up 5 votes
protected InputSource resolvePublicId(String publicId) {

        // public id -> system id
        if (publicId != null) {
            String value = getPublicMapping(publicId);
            if (value != null) {                
                InputSource input = new InputSource(value);
                input.setPublicId(publicId);
                return input;
            }
        }
        
        return null;
    }
 
Example 6
Project: incubator-netbeans   File: FileEntityResolver.java   Source Code and License Vote up 5 votes
/** Tries to find the entity on system file system.
 */
public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
    if (publicID == null) {
        return null;
    }


    String id = convertPublicId (publicID);
    
    StringBuffer sb = new StringBuffer (200);
    sb.append (ENTITY_PREFIX);
    sb.append (id);
    
    FileObject fo = FileUtil.getConfigFile (sb.toString ());
    if (fo != null) {
        
        // fill in InputSource instance
        
        InputSource in = new InputSource (fo.getInputStream ());
        try {
            Object myPublicID = fo.getAttribute("hint.originalPublicID");  //NOI18N
            if (myPublicID instanceof String) {
                in.setPublicId((String)myPublicID);
            }                
            URL url = fo.getURL();
            in.setSystemId(url.toString());  // we get nasty nbfs: instead nbres: but it is enough                
        } catch (IOException ex) {
            // do no care just no system id
        }
        return in;
    } else {
        return null;
    }
}
 
Example 7
Project: lams   File: PluggableSchemaResolver.java   Source Code and License Vote up 5 votes
@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
	if (logger.isTraceEnabled()) {
		logger.trace("Trying to resolve XML entity with public id [" + publicId +
				"] and system id [" + systemId + "]");
	}

	if (systemId != null) {
		String resourceLocation = getSchemaMappings().get(systemId);
		if (resourceLocation != null) {
			Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
			try {
				InputSource source = new InputSource(resource.getInputStream());
				source.setPublicId(publicId);
				source.setSystemId(systemId);
				if (logger.isDebugEnabled()) {
					logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
				}
				return source;
			}
			catch (FileNotFoundException ex) {
				if (logger.isDebugEnabled()) {
					logger.debug("Couldn't find XML schema [" + systemId + "]: " + resource, ex);
				}
			}
		}
	}
	return null;
}
 
Example 8
Project: lams   File: BeansDtdResolver.java   Source Code and License Vote up 5 votes
@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
	if (logger.isTraceEnabled()) {
		logger.trace("Trying to resolve XML entity with public ID [" + publicId +
				"] and system ID [" + systemId + "]");
	}
	if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
		int lastPathSeparator = systemId.lastIndexOf("/");
		for (String DTD_NAME : DTD_NAMES) {
			int dtdNameStart = systemId.indexOf(DTD_NAME);
			if (dtdNameStart > lastPathSeparator) {
				String dtdFile = systemId.substring(dtdNameStart);
				if (logger.isTraceEnabled()) {
					logger.trace("Trying to locate [" + dtdFile + "] in Spring jar");
				}
				try {
					Resource resource = new ClassPathResource(dtdFile, getClass());
					InputSource source = new InputSource(resource.getInputStream());
					source.setPublicId(publicId);
					source.setSystemId(systemId);
					if (logger.isDebugEnabled()) {
						logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
					}
					return source;
				}
				catch (IOException ex) {
					if (logger.isDebugEnabled()) {
						logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path", ex);
					}
				}

			}
		}
	}

	// Use the default behavior -> download from website or wherever.
	return null;
}
 
Example 9
Project: lams   File: EJB3DTDEntityResolver.java   Source Code and License Vote up 5 votes
private InputSource buildInputSource(String publicId, String systemId, InputStream dtdStream, boolean resolved) {
	if ( dtdStream == null ) {
		LOG.tracev( "Unable to locate [{0}] on classpath", systemId );
		return null;
	}
	LOG.tracev( "Located [{0}] in classpath", systemId );
	InputSource source = new InputSource( dtdStream );
	source.setPublicId( publicId );
	source.setSystemId( systemId );
	this.resolved = resolved;
	return source;
}
 
Example 10
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 11
Project: OpenJSharp   File: XMLCatalogResolver.java   Source Code and License Vote up 5 votes
/**
  * <p>Resolves an external entity. If the entity cannot be
  * resolved, this method should return <code>null</code>. This
  * method returns an input source if an entry was found in the
  * catalog for the given external identifier. It should be
  * overrided if other behaviour is required.</p>
  *
  * @param name the identifier of the external entity
  * @param publicId the public identifier, or <code>null</code> if none was supplied
  * @param baseURI the URI with respect to which relative systemIDs are interpreted.
  * @param systemId the system identifier
  *
  * @throws SAXException any SAX exception, possibly wrapping another exception
  * @throws IOException thrown if some i/o error occurs
  */
 public InputSource resolveEntity(String name, String publicId,
     String baseURI, String systemId) throws SAXException, IOException {

     String resolvedId = null;

     if (!getUseLiteralSystemId() && baseURI != null) {
         // Attempt to resolve the system identifier against the base URI.
         try {
             URI uri = new URI(new URI(baseURI), systemId);
             systemId = uri.toString();
         }
         // Ignore the exception. Fallback to the literal system identifier.
         catch (URI.MalformedURIException ex) {}
     }

     if (publicId != null && systemId != null) {
         resolvedId = resolvePublic(publicId, systemId);
     }
     else if (systemId != null) {
         resolvedId = resolveSystem(systemId);
     }

     if (resolvedId != null) {
         InputSource source = new InputSource(resolvedId);
         source.setPublicId(publicId);
         return source;
     }
     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: OpenJSharp   File: ResolvingParser.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 16
Project: monarch   File: DefaultEntityResolver2.java   Source Code and License Vote up 5 votes
/**
 * Get {@link InputSource} for path in class path.
 * 
 * @param path to resource to get {@link InputSource} for.
 * @return InputSource if resource found, otherwise null.
 * @since GemFire 8.1
 */
protected final InputSource getClassPathInputSource(final String publicId, final String systemId,
    final String path) {
  final InputStream stream = ClassPathLoader.getLatest().getResourceAsStream(getClass(), path);
  if (null == stream) {
    return null;
  }

  final InputSource inputSource = new InputSource(stream);
  inputSource.setPublicId(publicId);
  inputSource.setSystemId(systemId);

  return inputSource;
}
 
Example 17
Project: parabuild-ci   File: EmptyEntityResolver.java   Source Code and License Vote up 5 votes
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
{
    log.debug("resolveEntity(publicId=" + publicId + ", systemId=" + systemId + ") returning null");

    InputSource is = new InputSource(new StringReader(""));
    is.setPublicId(publicId);
    is.setSystemId(systemId);

    return is;
}
 
Example 18
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 19
Project: openjdk-jdk10   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(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;
}
 
Example 20
Project: myfaces-trinidad   File: SkinUtils.java   Source Code and License Vote up 4 votes
/**
 * @param provider      an XMLProvider implementation.
 * @param resolver      A NameResolver that can be used to locate resources, such as source images
 *                      for colorized icons.
 * @param inputStream   the inputStream. Must be non-null
 * @param parserManager the ParserManager to use for parsing Must  be non-null.
 * @param configFile    The name of the config file we are parsing.
 * @return A SkinsNode object (contains a List of SkinMetadata and a List of SkinAddition)
 */
static private SkinsNode _getSkinsNodeFromInputStream(
  XMLProvider        provider,
  NameResolver       resolver,
  InputStream        inputStream,
  ParserManager      parserManager,
  String             configFile,
  boolean            isMetaInf
  )
{

  if (inputStream == null)
    throw new NullPointerException(_LOG.getMessage("NO_INPUTSTREAM"));
  if (parserManager == null)
    throw new NullPointerException(_LOG.getMessage("NULL_PARSEMANAGER"));
  SkinsNode skinsNode = null;
  try
  {
    InputSource input = new InputSource();
    input.setByteStream(inputStream);
    input.setPublicId(configFile);

    ParseContextImpl context = new ParseContextImpl();

    // set a property on the parse context so that we can modify the stylesheet name
    // while creating SkinMetadata or SkinAddition
    if (isMetaInf)
      context.setProperty(XMLConstants.SKIN_NAMESPACE, XMLConstants.META_INF, isMetaInf);

    // Set up the NameResolver if we have one
    if (resolver != null)
      XMLUtils.setResolver(context, resolver);

    // Create the TreeBuilder
    TreeBuilder builder = new TreeBuilder(parserManager,
                                          SkinsNode.class);
    skinsNode = ((SkinsNode) builder.parse(provider, input, context));

    if (isMetaInf)
      context.setProperty(XMLConstants.SKIN_NAMESPACE, XMLConstants.META_INF, null);
  }
  catch (Exception e)
  {
    _LOG.warning("SKIN_CONFIG_PROCESS_FAILURE", configFile);
    _LOG.warning(e);
  }
  finally
  {
    try
    {
      inputStream.close();
    }
    catch (IOException ioe)
    {
      // Ignore
      ;
    }
  }
  return skinsNode;
}