📄 xincludefilter.java
字号:
URL baseURI = (URL) uris.peek (); String base; base = atts.getValue ("http://www.w3.org/XML/1998/namespace", "base"); if (base == null) uris.push (baseURI); else { URL url; if (base.indexOf ('#') != -1) fatal (new SAXParseException ( "xml:base with fragment: " + base, locator)); try { baseURI = new URL (baseURI, base); uris.push (baseURI); } catch (Exception e) { fatal (new SAXParseException ( "xml:base with illegal uri: " + base, locator, e)); } } if (!"http://www.w3.org/2001/XInclude".equals (uri)) { super.startElement (uri, localName, qName, atts); return; } if ("include".equals (localName)) { String href = atts.getValue ("href"); String parse = atts.getValue ("parse"); String encoding = atts.getValue ("encoding"); URL url = (URL) uris.peek (); SAXParseException x = null; if (href == null) fatal (new SAXParseException ( "XInclude missing href", locator)); if (href.indexOf ('#') != -1) fatal (new SAXParseException ( "XInclude with fragment: " + href, locator)); if (parse == null || "xml".equals (parse)) x = xinclude (url, href); else if ("text".equals (parse)) x = readText (url, href, encoding); else fatal (new SAXParseException ( "unknown XInclude parsing mode: " + parse, locator)); if (x == null) { // strip out all child content ignoreCount++; return; } // FIXME the 17-Sept-2002 CR of XInclude says we "must" // use xi:fallback elements to handle resource errors, // if they exist. fatal (x); } else if ("fallback".equals (localName)) { fatal (new SAXParseException ( "illegal top level XInclude 'fallback' element", locator)); } else { ErrorHandler eh = getErrorHandler (); // CR doesn't say this is an error if (eh != null) eh.warning (new SAXParseException ( "unrecognized toplevel XInclude element: " + localName, locator)); super.startElement (uri, localName, qName, atts); } } public void endElement (String uri, String localName, String qName) throws SAXException { if (ignoreCount != 0) { if (--ignoreCount != 0) return; } uris.pop (); if (!("http://www.w3.org/2001/XInclude".equals (uri) && "include".equals (localName))) super.endElement (uri, localName, qName); } // // ignore all content within non-empty xi:include elements // public void characters (char ch [], int start, int length) throws SAXException { if (ignoreCount == 0) super.characters (ch, start, length); } public void processingInstruction (String target, String value) throws SAXException { if (ignoreCount == 0) super.processingInstruction (target, value); } public void ignorableWhitespace (char ch [], int start, int length) throws SAXException { if (ignoreCount == 0) super.ignorableWhitespace (ch, start, length); } public void comment (char ch [], int start, int length) throws SAXException { if (ignoreCount == 0) super.comment (ch, start, length); } public void startCDATA () throws SAXException { if (ignoreCount == 0) super.startCDATA (); } public void endCDATA () throws SAXException { if (ignoreCount == 0) super.endCDATA (); } public void startPrefixMapping (String prefix, String uri) throws SAXException { if (ignoreCount == 0) super.startPrefixMapping (prefix, uri); } public void endPrefixMapping (String prefix) throws SAXException { if (ignoreCount == 0) super.endPrefixMapping (prefix); } public void skippedEntity (String name) throws SAXException { if (ignoreCount == 0) super.skippedEntity (name); } // JDK 1.1 seems to need it to be done this way, sigh void setLocator (Locator l) { locator = l; } Locator getLocator () { return locator; } // // for XIncluded entities, manage the current locator and // filter out events that would be incorrect to report // private class Scrubber extends EventFilter { Scrubber (EventFilter f) throws SAXException { // delegation passes to next in chain super (f); // process all content events super.setContentHandler (this); super.setProperty (LEXICAL_HANDLER, this); // drop all DTD events super.setDTDHandler (null); super.setProperty (DECL_HANDLER, null); } // maintain proxy locator // only one startDocument()/endDocument() pair per event stream public void setDocumentLocator (Locator l) { setLocator (l); } public void startDocument () { } public void endDocument () { } private void reject (String message) throws SAXException { fatal (new SAXParseException (message, getLocator ())); } // only the DTD from the "base document" gets reported public void startDTD (String root, String publicId, String systemId) throws SAXException { reject ("XIncluded DTD: " + systemId); } public void endDTD () throws SAXException { reject ("XIncluded DTD"); } // ... so this should never happen public void skippedEntity (String name) throws SAXException { reject ("XInclude skipped entity: " + name); } // since we rejected DTDs, only builtin entities can be reported } // <xi:include parse='xml' ...> // relative to the base URI passed private SAXParseException xinclude (URL url, String href) throws SAXException { XMLReader helper; Scrubber scrubber; Locator savedLocator = locator; // start with a parser acting just like our input // modulo DTD-ish stuff (validation flag, entity resolver) helper = XMLReaderFactory.createXMLReader (); helper.setErrorHandler (getErrorHandler ()); helper.setFeature (FEATURE_URI + "namespace-prefixes", true); // Set up the proxy locator and event filter. scrubber = new Scrubber (this); locator = null; bind (helper, scrubber); // Merge the included document, except its DTD try { url = new URL (url, href); href = url.toString (); if (inclusions.contains (href)) fatal (new SAXParseException ( "XInclude, circular inclusion", locator)); inclusions.addElement (href); uris.push (url); helper.parse (new InputSource (href)); return null; } catch (java.io.IOException e) { return new SAXParseException (href, locator, e); } finally { pop (href); locator = savedLocator; } } // <xi:include parse='text' ...> // relative to the base URI passed private SAXParseException readText (URL url, String href, String encoding) throws SAXException { InputStream in = null; try { URLConnection conn; InputStreamReader reader; char buf [] = new char [4096]; int count; url = new URL (url, href); conn = url.openConnection (); in = conn.getInputStream (); if (encoding == null) encoding = Resolver.getEncoding (conn.getContentType ()); if (encoding == null) { ErrorHandler eh = getErrorHandler (); if (eh != null) eh.warning (new SAXParseException ( "guessing text encoding for URL: " + url, locator)); reader = new InputStreamReader (in); } else reader = new InputStreamReader (in, encoding); while ((count = reader.read (buf, 0, buf.length)) != -1) super.characters (buf, 0, count); in.close (); return null; } catch (IOException e) { return new SAXParseException ( "can't XInclude text", locator, e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -