Entity Resolver, with Chaining Java code example - Click here to copy ->>>
Can't find what you're looking for? Try our search:
|
|
Really working examples categorized by API, package, class. You can compile and run our examples right away!
Not from source code for Java projects - only working examples! Copy, compile and run!
|
------------------
| Code: |
/* http://www.oreilly.com/catalog/sax2/
* SAX2
* By David Brownell
* First Edition January 2002
* ISBN: 0-596-00237-8
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Date;
import java.util.Dictionary;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
// ASSUMES: this shares a package with a "Storage" class
// that at least maps keys to data.
/**
* Entity Resolver, with Chaining
*/
public class MyResolver implements EntityResolver
{
private EntityResolver next;
private Dictionary map;
// n -- optional resolver to consult on failure
// m -- mapping public ids to preferred URLs
public MyResolver (EntityResolver n, Dictionary m)
{ next = n; map = m; }
public InputSource resolveEntity (String publicId, String systemId)
throws SAXException, IOException
{
// magic URL?
if ("http://localhost/xml/date".equals (systemId)) {
InputSource retval = new InputSource (systemId);
Reader date;
date = new StringReader (new Date().toString ());
retval.setCharacterStream (date);
return retval;
}
// nonstandard URI scheme?
if (systemId.startsWith ("blob:")) {
InputSource retval = new InputSource (systemId);
String key = systemId.substring (5);
byte data [] = Storage.keyToBlob (key);
retval.setByteStream (new ByteArrayInputStream (data));
return retval;
}
// use table to map public id to local URL?
if (map != null && publicId != null) {
String url = (String) map.get (publicId);
if (url != null)
return new InputSource (url);
}
// chain to next resolver?
if (next != null)
return next.resolveEntity (publicId, systemId);
return null;
}
}
|
|
|
References.
The list of classes which were used on this page you can find below. The
links to Java API contain official SUN documentation about all used classes.
[ Go Back ]
|