📄 xcat.java
字号:
if (!uri.startsWith (temp)) continue; if (prefix != null && (len = temp.length ()) < prefixLen) continue; prefix = temp; prefixLen = len; replace = (String) rewrites.get (temp); } if (prefix != null) { StringBuffer buf = new StringBuffer (replace); buf.append (uri.substring (prefixLen)); // IF the URI is accessible ... return new InputSource (buf.toString ()); } } // 7.1.2: 4. return 'delegateSystem' catalog match [complex] // 7.2.2: 4. return 'delegateURI' catalog match [complex] if (delegations != null) return checkDelegations (delegations, uri, null, uri); return null; } /** * Returns a URI for an external entity. */ public InputSource resolve ( boolean usingPublic, String publicId, String systemId ) throws SAXException, IOException { boolean preferSystem; InputSource retval; if (hasPreference) preferSystem = !this.usingPublic; else preferSystem = !usingPublic; if (publicId != null) publicId = normalizePublicId (false, publicId); // behavior here matches section 7.1.1 of the oasis spec if (systemId != null) { if (systemId.startsWith ("urn:publicid:")) { String temp = normalizePublicId (true, systemId); if (publicId == null) { publicId = temp; systemId = null; } else if (!publicId.equals (temp)) { // error; ok to recover by: systemId = null; } } else systemId = normalizeURI (systemId); } if (systemId == null && publicId == null) return null; if (systemId != null) { retval = mapURI (systemId, systemIds, systemRewrites, systemDelegations); if (retval != null) { retval.setPublicId (publicId); return retval; } } if (publicId != null && !(systemId != null && preferSystem)) { retval = locatePublicId (publicId); if (retval != null) { retval.setPublicId (publicId); return retval; } } // 7. apply nextCatalog entries if (next != null) { int length = next.size (); for (int i = 0; i < length; i++) { Catalog n = getNext (i); retval = n.resolve (usingPublic, publicId, systemId); if (retval != null) return retval; } } return null; } /** * Maps one URI into another, for resources that are not defined * using XML external entity or notation syntax. */ public InputSource resolveURI (String uri) throws SAXException, IOException { if (uri.startsWith ("urn:publicid:")) return resolve (true, normalizePublicId (true, uri), null); InputSource retval; uri = normalizeURI (uri); // 7.2.2 steps 2-4 retval = mapURI (uri, uris, uriRewrites, uriDelegations); if (retval != null) return retval; // 7.2.2 step 5. apply nextCatalog entries if (next != null) { int length = next.size (); for (int i = 0; i < length; i++) { Catalog n = getNext (i); retval = n.resolveURI (uri); if (retval != null) return retval; } } return null; } /** * Finds the external subset associated with a given root element. */ public InputSource getExternalSubset (String name) throws SAXException, IOException { if (doctypes != null) { String value = (String) doctypes.get (name); if (value != null) { // IF the URI is accessible ... return new InputSource (value); } } if (next != null) { int length = next.size (); for (int i = 0; i < length; i++) { Catalog n = getNext (i); if (n == null) continue; InputSource retval = n.getExternalSubset (name); if (retval != null) return retval; } } return null; } private synchronized Catalog getNext (int i) throws SAXException, IOException { Object obj; if (next == null || i < 0 || i >= next.size ()) return null; obj = next.elementAt (i); if (obj instanceof Catalog) return (Catalog) obj; // ok, we deferred reading that catalog till now. // load and cache it. Catalog cat = null; try { cat = loadCatalog (parserClass, eh, (String) obj, unified); next.setElementAt (cat, i); } catch (SAXException e) { // must fail quietly, says the OASIS spec } catch (IOException e) { // same applies here } return cat; } private InputSource checkDelegations ( Hashtable delegations, String id, String publicId, // only one of public/system String systemId // will be non-null... ) throws SAXException, IOException { Vector matches = null; int length = 0; // first, see if any prefixes match. for (Enumeration e = delegations.keys (); e.hasMoreElements (); /* NOP */) { String prefix = (String) e.nextElement (); if (!id.startsWith (prefix)) continue; if (matches == null) matches = new Vector (); // maintain in longer->shorter sorted order // NOTE: assumes not many matches will fire! int index; for (index = 0; index < length; index++) { String temp = (String) matches.elementAt (index); if (prefix.length () > temp.length ()) { matches.insertElementAt (prefix, index); break; } } if (index == length) matches.addElement (prefix); length++; } if (matches == null) return null; // now we know the list of catalogs to replace our "top level" // list ... we use it here, rather than somehow going back and // restarting, since this helps avoid reading most catalogs. // this assumes stackspace won't be a problem. for (int i = 0; i < length; i++) { Catalog catalog = null; InputSource result; // get this catalog. we may not have read it yet. synchronized (delegations) { Object prefix = matches.elementAt (i); Object cat = delegations.get (prefix); if (cat instanceof Catalog) catalog = (Catalog) cat; else { try { // load and cache that catalog catalog = loadCatalog (parserClass, eh, (String) cat, unified); delegations.put (prefix, catalog); } catch (SAXException e) { // must ignore, says the OASIS spec } catch (IOException e) { // same applies here } } } // ignore failed loads, and proceed if (catalog == null) continue; // we have a catalog ... resolve! // usingPublic value can't matter, there's no choice result = catalog.resolve (true, publicId, systemId); if (result != null) return result; } // if there were no successes, the entire // lookup failed (all the way to top level) throw new DoneDelegation (); } } /** This is the namespace URI used for OASIS XML Catalogs. */ private static final String catalogNamespace = "urn:oasis:names:tc:entity:xmlns:xml:catalog"; /** * Loads/unmarshals one catalog. */ private static class Loader extends DefaultHandler2 { private boolean preInterned; private ErrorHandler handler; private boolean unified; private int ignoreDepth; private Locator locator; private boolean started; private Hashtable externals; private Stack bases; Catalog cat = new Catalog (); /** * Constructor. * @param flag true iff the parser already interns strings. * @param eh Errors and warnings are delegated to this. * @param unified true keeps one table for URI mappings; * false matches OASIS spec, storing mappings * for URIs and SYSTEM ids in parallel tables. */ Loader (boolean flag, ErrorHandler eh, boolean unified) { preInterned = flag; handler = eh; this.unified = unified; cat.unified = unified; cat.eh = eh; } // strips out fragments private String nofrag (String uri) throws SAXException { if (uri.indexOf ('#') != -1) { warn ("URI with fragment: " + uri); uri = uri.substring (0, uri.indexOf ('#')); } return uri; } // absolutizes relative URIs private String absolutize (String uri) throws SAXException { // avoid creating URLs if they're already absolutized, // or if the URI is already using a known scheme if (uri.startsWith ("file:/") || uri.startsWith ("http:/") || uri.startsWith ("https:/") || uri.startsWith ("ftp:/") || uri.startsWith ("urn:") ) return uri; // otherwise, let's hope the JDK handles this URI scheme. try { URL base = (URL) bases.peek (); return new URL (base, uri).toString (); } catch (Exception e) { fatal ("can't absolutize URI: " + uri); return null; } } // recoverable error private void error (String message) throws SAXException { if (handler == null) return; handler.error (new SAXParseException (message, locator)); } // nonrecoverable error private void fatal (String message) throws SAXException { SAXParseException spe; spe = new SAXParseException (message, locator); if (handler != null) handler.fatalError (spe); throw spe; } // low severity problem private void warn (String message) throws SAXException { if (handler == null) return; handler.warning (new SAXParseException (message, locator)); } // callbacks: public void setDocumentLocator (Locator l) { locator = l; } public void startDocument () throws SAXException { if (locator == null) error ("no locator!"); bases = new Stack (); String uri = locator.getSystemId (); try { bases.push (new URL (uri)); } catch (IOException e) { fatal ("bad document base URI: " + uri); } } public void endDocument () throws SAXException { try { if (!started) error ("not a catalog!"); } finally { locator = null; handler = null; externals = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -