Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

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.

  org.xml.sax.EntityResolver

  java.io.Reader

  javax.swing.text.html.parser.Entity

  javax.naming.spi.Resolver

  java.util.Dictionary

  org.xml.sax.InputSource

  org.xml.sax.SAXException

  java.io.IOException

  java.net.URL

  java.sql.Date

  java.net.URI

  java.io.ByteArrayInputStream




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy