Source for gnu.xml.aelfred2.JAXPFactory

   1: /* JAXPFactory.java --
   2:    Copyright (C) 2001 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.xml.aelfred2;
  39: 
  40: import java.util.Enumeration;
  41: import java.util.Hashtable;
  42: 
  43: import org.xml.sax.Parser;
  44: import org.xml.sax.XMLReader;
  45: import org.xml.sax.SAXException;
  46: import org.xml.sax.SAXNotRecognizedException;
  47: import org.xml.sax.SAXNotSupportedException;
  48: import org.xml.sax.helpers.XMLReaderAdapter;
  49: 
  50: import javax.xml.parsers.ParserConfigurationException;
  51: import javax.xml.parsers.SAXParser;
  52: import javax.xml.parsers.SAXParserFactory;
  53: 
  54: 
  55: /**
  56:  * Configurable factory to create an Ælfred2 JAXP parser; required
  57:  * to bootstrap using JAXP.  You should use SAX2 directly where possible,
  58:  * rather than through JAXP, since that gives you better control.
  59:  * This class would normally be configured as a platform default factory.
  60:  *
  61:  * @author David Brownell
  62:  */
  63: public final class JAXPFactory
  64:   extends SAXParserFactory
  65: {
  66: 
  67:   private Hashtable flags = new Hashtable();
  68: 
  69:   /**
  70:    * Constructs a factory which normally returns a non-validating
  71:    * parser.
  72:    */
  73:   public JAXPFactory()
  74:   {
  75:   }
  76: 
  77:   public SAXParser newSAXParser()
  78:     throws ParserConfigurationException, SAXException
  79:   {
  80:     JaxpParser jaxp = new JaxpParser();
  81:     Enumeration e = flags.keys();
  82:     XMLReader parser = jaxp.getXMLReader();
  83: 
  84:     parser.setFeature(SAXDriver.FEATURE + "namespaces",
  85:                       isNamespaceAware());
  86:     parser.setFeature(SAXDriver.FEATURE + "validation",
  87:                       isValidating());
  88:     // that makes SAX2 feature flags trump JAXP
  89: 
  90:     while (e.hasMoreElements())
  91:       {
  92:         String uri = (String) e.nextElement();
  93:         Boolean value = (Boolean) flags.get(uri);
  94:         parser.setFeature(uri, value.booleanValue());
  95:       }
  96: 
  97:     return jaxp;
  98:   }
  99: 
 100:   // yes, this "feature transfer" mechanism doesn't play well
 101: 
 102:   public void setFeature(String name, boolean value)
 103:     throws ParserConfigurationException, SAXNotRecognizedException,
 104:            SAXNotSupportedException
 105:   {
 106:     try
 107:       {
 108:         // force "early" detection of errors where possible
 109:         // (flags can't necessarily be set before parsing)
 110:         new JaxpParser().getXMLReader().setFeature(name, value);
 111: 
 112:         flags.put(name, Boolean.valueOf(value));
 113:       }
 114:     catch (SAXNotRecognizedException e)
 115:       {
 116:         throw new SAXNotRecognizedException(name);
 117:       }
 118:     catch (SAXNotSupportedException e)
 119:       {
 120:         throw new SAXNotSupportedException(name);
 121:       }
 122:     catch (Exception e)
 123:       {
 124:         throw new ParserConfigurationException(e.getClass().getName()
 125:                                                + ": "
 126:                                                + e.getMessage());
 127:       }
 128:   }
 129: 
 130:   public boolean getFeature(String name)
 131:     throws ParserConfigurationException, SAXNotRecognizedException,
 132:            SAXNotSupportedException
 133:   {
 134:     Boolean value = (Boolean) flags.get(name);
 135: 
 136:     if (value != null)
 137:       {
 138:         return value.booleanValue();
 139:       }
 140:     else
 141:       {
 142:         try
 143:           {
 144:             return new JaxpParser().getXMLReader().getFeature(name);
 145:           }
 146:         catch (SAXNotRecognizedException e)
 147:           {
 148:             throw new SAXNotRecognizedException(name);
 149:           }
 150:         catch (SAXNotSupportedException e)
 151:           {
 152:             throw new SAXNotSupportedException(name);
 153:           }
 154:         catch (SAXException e)
 155:           {
 156:             throw new ParserConfigurationException(e.getClass().getName()
 157:                                                    + ": "
 158:                                                    + e.getMessage());
 159:           }
 160:       }
 161:   }
 162: 
 163:   private static class JaxpParser
 164:     extends SAXParser
 165:   {
 166: 
 167:     private XmlReader ae2 = new XmlReader();
 168:     private XMLReaderAdapter parser = null;
 169: 
 170:     JaxpParser()
 171:     {
 172:     }
 173: 
 174:     public void setProperty(String id, Object value)
 175:       throws SAXNotRecognizedException, SAXNotSupportedException
 176:     {
 177:       ae2.setProperty(id, value);
 178:     }
 179: 
 180:     public Object getProperty(String id)
 181:       throws SAXNotRecognizedException, SAXNotSupportedException
 182:     {
 183:       return ae2.getProperty(id);
 184:     }
 185: 
 186:     public Parser getParser()
 187:       throws SAXException
 188:     {
 189:       if (parser == null)
 190:         {
 191:           parser = new XMLReaderAdapter(ae2);
 192:         }
 193:       return parser;
 194:     }
 195: 
 196:     public XMLReader getXMLReader ()
 197:       throws SAXException
 198:     {
 199:       return ae2;
 200:     }
 201: 
 202:     public boolean isNamespaceAware()
 203:     {
 204:       try
 205:         {
 206:           return ae2.getFeature(SAXDriver.FEATURE + "namespaces");
 207:         }
 208:       catch (Exception e)
 209:         {
 210:           throw new Error();
 211:         }
 212:     }
 213: 
 214:     public boolean isValidating()
 215:     {
 216:       try
 217:         {
 218:           return ae2.getFeature(SAXDriver.FEATURE + "validation");
 219:         }
 220:       catch (Exception e)
 221:         {
 222:           throw new Error();
 223:         }
 224:     }
 225: 
 226:     // TODO isXIncludeAware()
 227: 
 228:   }
 229: 
 230: }