Source for gnu.xml.stream.XMLInputFactoryImpl

   1: /* XMLInputFactoryImpl.java --
   2:    Copyright (C) 2005,2006  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.stream;
  39: 
  40: import java.io.File;
  41: import java.io.FileInputStream;
  42: import java.io.FileNotFoundException;
  43: import java.io.InputStream;
  44: import java.io.IOException;
  45: import java.io.Reader;
  46: import java.net.MalformedURLException;
  47: import java.net.URL;
  48: 
  49: import javax.xml.transform.Source;
  50: import javax.xml.transform.stream.StreamSource;
  51: import javax.xml.stream.EventFilter;
  52: import javax.xml.stream.StreamFilter;
  53: import javax.xml.stream.XMLEventReader;
  54: import javax.xml.stream.XMLReporter;
  55: import javax.xml.stream.XMLResolver;
  56: import javax.xml.stream.XMLStreamException;
  57: import javax.xml.stream.XMLStreamReader;
  58: import javax.xml.stream.XMLInputFactory;
  59: import javax.xml.stream.util.XMLEventAllocator;
  60: 
  61: /**
  62:  * Factory for creating parsers from various kinds of XML source.
  63:  *
  64:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  65:  */
  66: public class XMLInputFactoryImpl
  67:   extends XMLInputFactory
  68: {
  69: 
  70:   protected XMLResolver resolver;
  71:   protected XMLReporter reporter;
  72:   protected XMLEventAllocator allocator;
  73: 
  74:   protected boolean validating;
  75:   protected boolean namespaceAware = true;
  76:   protected boolean coalescing;
  77:   protected boolean replacingEntityReferences = true;
  78:   protected boolean externalEntities = true;
  79:   protected boolean supportDTD = true;
  80:   protected boolean xIncludeAware = false;
  81:   protected boolean baseAware = true;
  82:   protected boolean stringInterning = true;
  83: 
  84:   public XMLInputFactoryImpl()
  85:   {
  86:     allocator = new XMLEventAllocatorImpl();
  87:   }
  88: 
  89:   public XMLStreamReader createXMLStreamReader(Reader reader)
  90:     throws XMLStreamException
  91:   {
  92:     return createXMLStreamReader(null, reader);
  93:   }
  94: 
  95:   public XMLStreamReader createXMLStreamReader(Source source)
  96:     throws XMLStreamException
  97:   {
  98:     String systemId = source.getSystemId();
  99:     InputStream in = getInputStream(source);
 100:     XMLParser ret = new XMLParser(in, systemId,
 101:                                   validating,
 102:                                   namespaceAware,
 103:                                   coalescing,
 104:                                   replacingEntityReferences,
 105:                                   externalEntities,
 106:                                   supportDTD,
 107:                                   baseAware,
 108:                                   stringInterning,
 109:                                   false,
 110:                                   reporter,
 111:                                   resolver);
 112:     if (xIncludeAware)
 113:       return new XIncludeFilter(ret, systemId, namespaceAware, validating,
 114:                                 replacingEntityReferences);
 115:     return ret;
 116:   }
 117: 
 118:   public XMLStreamReader createXMLStreamReader(InputStream in)
 119:     throws XMLStreamException
 120:   {
 121:     return createXMLStreamReader(null, in);
 122:   }
 123: 
 124:   public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
 125:     throws XMLStreamException
 126:   {
 127:     return createXMLStreamReader(in);
 128:   }
 129: 
 130:   public XMLStreamReader createXMLStreamReader(String systemId, InputStream in)
 131:     throws XMLStreamException
 132:   {
 133:     XMLParser ret = new XMLParser(in, systemId,
 134:                                   validating,
 135:                                   namespaceAware,
 136:                                   coalescing,
 137:                                   replacingEntityReferences,
 138:                                   externalEntities,
 139:                                   supportDTD,
 140:                                   baseAware,
 141:                                   stringInterning,
 142:                                   false,
 143:                                   reporter,
 144:                                   resolver);
 145:     if (xIncludeAware)
 146:       return new XIncludeFilter(ret, null, namespaceAware, validating,
 147:                                 replacingEntityReferences);
 148:     return ret;
 149:   }
 150: 
 151:   public XMLStreamReader createXMLStreamReader(String systemId, Reader reader)
 152:     throws XMLStreamException
 153:   {
 154:     XMLParser ret = new XMLParser(reader, systemId,
 155:                                   validating,
 156:                                   namespaceAware,
 157:                                   coalescing,
 158:                                   replacingEntityReferences,
 159:                                   externalEntities,
 160:                                   supportDTD,
 161:                                   baseAware,
 162:                                   stringInterning,
 163:                                   false,
 164:                                   reporter,
 165:                                   resolver);
 166:     if (xIncludeAware)
 167:       return new XIncludeFilter(ret, null, namespaceAware, validating,
 168:                                 replacingEntityReferences);
 169:     return ret;
 170:   }
 171: 
 172:   public XMLEventReader createXMLEventReader(Reader reader)
 173:     throws XMLStreamException
 174:   {
 175:     XMLStreamReader sr = createXMLStreamReader(reader);
 176:     return new XMLEventReaderImpl(sr, allocator, null);
 177:   }
 178: 
 179:   public XMLEventReader createXMLEventReader(String systemId, Reader reader)
 180:     throws XMLStreamException
 181:   {
 182:     XMLStreamReader sr = createXMLStreamReader(systemId, reader);
 183:     return new XMLEventReaderImpl(sr, allocator, null);
 184:   }
 185: 
 186:   public XMLEventReader createXMLEventReader(XMLStreamReader reader)
 187:     throws XMLStreamException
 188:   {
 189:     return new XMLEventReaderImpl(reader, allocator, null);
 190:   }
 191: 
 192:   public XMLEventReader createXMLEventReader(Source source)
 193:     throws XMLStreamException
 194:   {
 195:     XMLStreamReader sr = createXMLStreamReader(source);
 196:     return new XMLEventReaderImpl(sr, allocator, null);
 197:   }
 198: 
 199:   public XMLEventReader createXMLEventReader(InputStream in)
 200:     throws XMLStreamException
 201:   {
 202:     XMLStreamReader sr = createXMLStreamReader(in);
 203:     return new XMLEventReaderImpl(sr, allocator, null);
 204:   }
 205: 
 206:   public XMLEventReader createXMLEventReader(InputStream in, String encoding)
 207:     throws XMLStreamException
 208:   {
 209:     XMLStreamReader sr = createXMLStreamReader(in, encoding);
 210:     return new XMLEventReaderImpl(sr, allocator, null);
 211:   }
 212: 
 213:   public XMLEventReader createXMLEventReader(String systemId, InputStream in)
 214:     throws XMLStreamException
 215:   {
 216:     XMLStreamReader sr = createXMLStreamReader(systemId, in);
 217:     return new XMLEventReaderImpl(sr, allocator, null);
 218:   }
 219: 
 220:   public XMLStreamReader createFilteredReader(XMLStreamReader reader,
 221:                                               StreamFilter filter)
 222:     throws XMLStreamException
 223:   {
 224:     return new FilteredStreamReader(reader, filter);
 225:   }
 226: 
 227:   public XMLEventReader createFilteredReader(XMLEventReader reader,
 228:                                              EventFilter filter)
 229:     throws XMLStreamException
 230:   {
 231:     return new FilteredEventReader(reader, filter);
 232:   }
 233: 
 234:   public XMLResolver getXMLResolver()
 235:   {
 236:     return resolver;
 237:   }
 238: 
 239:   public void setXMLResolver(XMLResolver resolver)
 240:   {
 241:     this.resolver = resolver;
 242:   }
 243: 
 244:   public XMLReporter getXMLReporter()
 245:   {
 246:     return reporter;
 247:   }
 248: 
 249:   public void setXMLReporter(XMLReporter reporter)
 250:   {
 251:     this.reporter = reporter;
 252:   }
 253: 
 254:   public void setProperty(String name, Object value)
 255:     throws IllegalArgumentException
 256:   {
 257:     if (name.equals(IS_NAMESPACE_AWARE))
 258:       namespaceAware = ((Boolean) value).booleanValue();
 259:     else if (name.equals(IS_VALIDATING))
 260:       validating = ((Boolean) value).booleanValue();
 261:     else if (name.equals(IS_COALESCING))
 262:       coalescing = ((Boolean) value).booleanValue();
 263:     else if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
 264:       replacingEntityReferences = ((Boolean) value).booleanValue();
 265:     else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
 266:       externalEntities = ((Boolean) value).booleanValue();
 267:     else if (name.equals(SUPPORT_DTD))
 268:       supportDTD = ((Boolean) value).booleanValue();
 269:     else if (name.equals(REPORTER))
 270:       reporter = (XMLReporter) value;
 271:     else if (name.equals(RESOLVER))
 272:       resolver = (XMLResolver) value;
 273:     else if (name.equals(ALLOCATOR))
 274:       allocator = (XMLEventAllocator) value;
 275:     else if (name.equals("gnu.xml.stream.stringInterning"))
 276:       stringInterning = ((Boolean) value).booleanValue();
 277:     else if (name.equals("gnu.xml.stream.baseAware"))
 278:       baseAware = ((Boolean) value).booleanValue();
 279:     else if (name.equals("gnu.xml.stream.xIncludeAware"))
 280:       xIncludeAware = ((Boolean) value).booleanValue();
 281:     else
 282:       throw new IllegalArgumentException(name);
 283:   }
 284: 
 285:   public Object getProperty(String name)
 286:     throws IllegalArgumentException
 287:   {
 288:     if (name.equals(IS_NAMESPACE_AWARE))
 289:       return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
 290:     if (name.equals(IS_VALIDATING))
 291:       return validating ? Boolean.TRUE : Boolean.FALSE;
 292:     if (name.equals(IS_COALESCING))
 293:       return coalescing ? Boolean.TRUE : Boolean.FALSE;
 294:     if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
 295:       return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE;
 296:     if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
 297:       return externalEntities ? Boolean.TRUE : Boolean.FALSE;
 298:     if (name.equals(SUPPORT_DTD))
 299:       return supportDTD ? Boolean.TRUE : Boolean.FALSE;
 300:     if (name.equals(REPORTER))
 301:       return reporter;
 302:     if (name.equals(RESOLVER))
 303:       return resolver;
 304:     if (name.equals(ALLOCATOR))
 305:       return allocator;
 306:     if (name.equals("gnu.xml.stream.stringInterning"))
 307:       return stringInterning ? Boolean.TRUE : Boolean.FALSE;
 308:     if (name.equals("gnu.xml.stream.baseAware"))
 309:       return baseAware ? Boolean.TRUE : Boolean.FALSE;
 310:     if (name.equals("gnu.xml.stream.xIncludeAware"))
 311:       return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
 312:     throw new IllegalArgumentException(name);
 313:   }
 314: 
 315:   public boolean isPropertySupported(String name)
 316:   {
 317:     return name.equals(IS_NAMESPACE_AWARE) ||
 318:       name.equals(IS_VALIDATING) ||
 319:       name.equals(IS_COALESCING) ||
 320:       name.equals(IS_REPLACING_ENTITY_REFERENCES) ||
 321:       name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) ||
 322:       name.equals(SUPPORT_DTD) ||
 323:       name.equals(REPORTER) ||
 324:       name.equals(RESOLVER) ||
 325:       name.equals(ALLOCATOR) ||
 326:       name.equals("gnu.xml.stream.stringInterning") ||
 327:       name.equals("gnu.xml.stream.baseAware") ||
 328:       name.equals("gnu.xml.stream.xIncludeAware");
 329:   }
 330: 
 331:   public void setEventAllocator(XMLEventAllocator allocator)
 332:   {
 333:     this.allocator = allocator;
 334:   }
 335: 
 336:   public XMLEventAllocator getEventAllocator()
 337:   {
 338:     return allocator;
 339:   }
 340: 
 341:   public void setCoalescing(boolean coalescing)
 342:   {
 343:     this.coalescing = coalescing;
 344:   }
 345: 
 346:   public boolean isCoalescing()
 347:   {
 348:     return coalescing;
 349:   }
 350: 
 351:   protected InputStream getInputStream(Source source)
 352:     throws XMLStreamException
 353:   {
 354:     InputStream in = null;
 355:     if (source instanceof StreamSource)
 356:       {
 357:         StreamSource streamSource = (StreamSource) source;
 358:         in = streamSource.getInputStream();
 359:       }
 360:     if (in == null)
 361:       {
 362:         String systemId = source.getSystemId();
 363:         try
 364:           {
 365:             URL url = new URL(systemId);
 366:             try
 367:               {
 368:                 in = url.openStream();
 369:               }
 370:             catch (IOException e2)
 371:               {
 372:                 XMLStreamException e3 = new XMLStreamException(e2);
 373:                 e3.initCause(e2);
 374:                 throw e3;
 375:               }
 376:           }
 377:         catch (MalformedURLException e)
 378:           {
 379:             // Fall back to relative file
 380:             if (File.separatorChar != '/')
 381:               systemId = systemId.replace('/', File.separatorChar);
 382:             try
 383:               {
 384:                 in = new FileInputStream(systemId);
 385:               }
 386:             catch (FileNotFoundException e2)
 387:               {
 388:                 XMLStreamException e3 = new XMLStreamException(e2);
 389:                 e3.initCause(e2);
 390:                 throw e3;
 391:               }
 392:           }
 393:       }
 394:     return in;
 395:   }
 396: 
 397: }