resolver.java
来自「java jdk 1.4的源码」· Java 代码 · 共 507 行 · 第 1/2 页
JAVA
507 行
// ignore the system "file.encoding" property since // autodetection is more correct. // if (charset == null) { contentType = contentType.trim (); if (contentType.startsWith ("text/")) { if (!"file".equalsIgnoreCase (scheme)) charset = "US-ASCII"; } // "application/*" has no default } } retval = new InputSource (XmlReader.createReader (stream, charset)); retval.setByteStream (stream); retval.setEncoding (charset); return retval; } /** * Creates an input source from a given URI. * * @param uri the URI (system ID) for the entity * @param checkType if true, the MIME content type for the entity * is checked for document type and character set encoding. */ static public InputSource createInputSource (URL uri, boolean checkType) throws IOException { URLConnection conn = uri.openConnection (); if (conn instanceof HttpURLConnection) { int status = ((HttpURLConnection)conn).getResponseCode (); if ((status >= 400 && status <= 417) || (status >=500 && status <=505)) { throw new IOException ("Error in opening uri " + uri + "status code=" + status); } } InputSource retval; if (checkType) { String contentType = conn.getContentType (); retval = createInputSource (contentType, conn.getInputStream (), false, uri.getProtocol ()); } else { retval = new InputSource ( XmlReader.createReader (conn.getInputStream ())); } retval.setSystemId (conn.getURL ().toString ()); return retval; } /** * Creates an input source from a given file, autodetecting * the character encoding. * * @param uri the URI (system ID) for the entity */ static public InputSource createInputSource (File file) throws IOException { InputSource retval; String path; retval = new InputSource ( XmlReader.createReader (new FileInputStream (file))); // On JDK 1.2 and later, simplify this: // "path = file.toURL ().toString ()". path = file.getAbsolutePath (); if (File.separatorChar != '/') path = path.replace (File.separatorChar, '/'); if (!path.startsWith ("/")) path = "/" + path; if (!path.endsWith ("/") && file.isDirectory ()) path = path + "/"; retval.setSystemId ("file:" + path); return retval; } /** * <b>SAX:</b> * Resolve the given entity into an input source. If the name can't * be mapped to a preferred form of the entity, the URI is used. To * resolve the entity, first a local catalog mapping names to URIs is * consulted. If no mapping is found there, a catalog mapping names * to java resources is consulted. Finally, if neither mapping found * a copy of the entity, the specified URI is used. * * <P> When a URI is used, <a href="#createInputSource"> * createInputSource</a> is used to correctly deduce the character * encoding used by this entity. No MIME type checking is done. * * @param name Used to find alternate copies of the entity, when * this value is non-null; this is the XML "public ID". * @param uri Used when no alternate copy of the entity is found; * this is the XML "system ID", normally a URI. */ public InputSource resolveEntity (String name, String uri) throws IOException, SAXException { InputSource retval; String mappedURI = name2uri (name); InputStream stream; // prefer explicit URI mappings, then bundled resources... if (mappedURI == null && (stream = mapResource (name)) != null) { uri = "java:resource:" + (String) id2resource.get (name); retval = new InputSource (XmlReader.createReader (stream)); // ...and treat all URIs the same (as URLs for now). } else { URL url; URLConnection conn; if (mappedURI != null) uri = mappedURI; else if (uri == null) return null; url = new URL (uri); conn = url.openConnection (); uri = conn.getURL ().toString (); // System.out.println ("++ URI: " + url); if (ignoringMIME) retval = new InputSource ( XmlReader.createReader (conn.getInputStream ())); else { String contentType = conn.getContentType (); retval = createInputSource (contentType, conn.getInputStream (), false, url.getProtocol ()); } } retval.setSystemId (uri); retval.setPublicId (name); return retval; } /** * Returns true if this resolver is ignoring MIME types in the documents * it returns, to work around bugs in how servers have reported the * documents' MIME types. */ public boolean isIgnoringMIME () { return ignoringMIME; } /** * Tells the resolver whether to ignore MIME types in the documents it * retrieves. Many web servers incorrectly assign text documents a * default character encoding, even when that is incorrect. For example, * all HTTP text documents default to use ISO-8859-1 (used for Western * European languages), and other MIME sources default text documents * to use US-ASCII (a seven bit encoding). For XML documents which * include text encoding declarations (as most should do), these server * bugs can be worked around by ignoring the MIME type entirely. */ public void setIgnoringMIME (boolean value) { ignoringMIME = value; } // maps the public ID to an alternate URI, if one is registered private String name2uri (String publicId) { if (publicId == null || id2uri == null) return null; return (String) id2uri.get (publicId); } /** * Registers the given public ID as corresponding to a particular * URI, typically a local copy. This URI will be used in preference * to ones provided as system IDs in XML entity declarations. This * mechanism would most typically be used for Document Type Definitions * (DTDs), where the public IDs are formally managed and versioned. * * @param publicId The managed public ID being mapped * @param uri The URI of the preferred copy of that entity */ public void registerCatalogEntry ( String publicId, String uri ) { if (id2uri == null) id2uri = new Hashtable (17); id2uri.put (publicId, uri); } // return the resource as a stream private InputStream mapResource (String publicId) { // System.out.println ("++ PUBLIC: " + publicId); if (publicId == null || id2resource == null) return null; String resourceName = (String) id2resource.get (publicId); ClassLoader loader = null; if (resourceName == null) return null; // System.out.println ("++ Resource: " + resourceName); if (id2loader != null) loader = (ClassLoader) id2loader.get (publicId); // System.out.println ("++ Loader: " + loader); if (loader == null) return ClassLoader.getSystemResourceAsStream (resourceName); return loader.getResourceAsStream (resourceName); } /** * Registers a given public ID as corresponding to a particular Java * resource in a given class loader, typically distributed with a * software package. This resource will be preferred over system IDs * included in XML documents. This mechanism should most typically be * used for Document Type Definitions (DTDs), where the public IDs are * formally managed and versioned. * * <P> If a mapping to a URI has been provided, that mapping takes * precedence over this one. * * @param publicId The managed public ID being mapped * @param resourceName The name of the Java resource * @param loader The class loader holding the resource, or null if * it is a system resource. */ public void registerCatalogEntry ( String publicId, String resourceName, ClassLoader loader ) { if (id2resource == null) id2resource = new Hashtable (17); id2resource.put (publicId, resourceName); if (loader != null) { if (id2loader == null) id2loader = new Hashtable (17); id2loader.put (publicId, loader); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?