📄 butorconfigentityresolver.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.config.lowlevel;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URISyntaxException;import org.butor.log.Log;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * @author sawanai * */public class ButorConfigEntityResolver implements EntityResolver { /** * */ public ButorConfigEntityResolver() { super(); } /** * Allow the application to resolve external entities. * * <p>The Parser will call this method before opening any external * entity except the top-level document entity (including the * external DTD subset, external entities referenced within the * DTD, and external entities referenced within the document * element): the application may request that the parser resolve * the entity itself, that it use an alternative URI, or that it * use an entirely different input source.</p> * * <p>Application writers can use this method to redirect external * system identifiers to secure and/or local URIs, to look up * public identifiers in a catalogue, or to read an entity from a * database or other input source (including, for example, a dialog * box).</p> * * <p>If the system identifier is a URL, the SAX parser must * resolve it fully before reporting it to the application.</p> * * @param publicId The public identifier of the external entity * being referenced, or null if none was supplied. * @param systemId The system identifier of the external entity * being referenced. * @return An InputSource object describing the new input source, * or null to request that the parser open a regular * URI connection to the system identifier. * @exception org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @exception java.io.IOException A Java-specific IO exception, * possibly the result of creating a new InputStream * or Reader for the InputSource. * @see org.xml.sax.InputSource */ public InputSource resolveEntity (String publicId, String systemId) throws SAXException, IOException { try { // Wrap the systemId in a URI object to make it convenient // to extract the components of the systemId URI uri = new URI(systemId); // Check if external source is a file if ("file".equals(uri.getScheme())) { File file = new File(uri.getSchemeSpecificPart()); //TODO String filename = "/org/butor/config/lowlevel/" +file.getName(); Log.logStr(this, Log.LOG_TYPE_INFO, "resolveEntity()", "getting resource [" +filename +"] ..."); InputStream inputStream = this.getClass().getResourceAsStream(filename); if (inputStream == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "resolveEntity()", "Resource [" +filename +"] not found!"); } return new InputSource(inputStream); } } catch (URISyntaxException e) { //OK } // Returning null causes the caller to try accessing the systemid return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -