Source for gnu.xml.stream.XMLInputFactoryImpl

   1: /* XMLInputFactoryImpl.java -- 
   2:    Copyright (C) 2005  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: 
  81:   public XMLInputFactoryImpl()
  82:   {
  83:     allocator = new XMLEventAllocatorImpl();
  84:   }
  85: 
  86:   public XMLStreamReader createXMLStreamReader(Reader reader)
  87:     throws XMLStreamException
  88:   {
  89:     return new XMLStreamReaderImpl(reader, null, null,
  90:                                    resolver, reporter,
  91:                                    validating, namespaceAware,
  92:                                    coalescing, replacingEntityReferences,
  93:                                    externalEntities, supportDTD);
  94:   }
  95:   
  96:   public XMLStreamReader createXMLStreamReader(Source source)
  97:     throws XMLStreamException
  98:   {
  99:     String systemId = source.getSystemId();
 100:     InputStream in = getInputStream(source);
 101:     return new XMLStreamReaderImpl(in, null, systemId,
 102:                                    resolver, reporter,
 103:                                    validating, namespaceAware,
 104:                                    coalescing, replacingEntityReferences,
 105:                                    externalEntities, supportDTD);
 106:   }
 107:   
 108:   public XMLStreamReader createXMLStreamReader(InputStream in)
 109:     throws XMLStreamException
 110:   {
 111:     return new XMLStreamReaderImpl(in, null, null,
 112:                                    resolver, reporter,
 113:                                    validating, namespaceAware,
 114:                                    coalescing, replacingEntityReferences,
 115:                                    externalEntities, supportDTD);
 116:   }
 117:   
 118:   public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
 119:     throws XMLStreamException
 120:   {
 121:     return createXMLStreamReader(in);
 122:   }
 123: 
 124:   public XMLEventReader createXMLEventReader(Reader reader)
 125:     throws XMLStreamException
 126:   {
 127:     XMLStreamReader sr = createXMLStreamReader(reader);
 128:     return new XMLEventReaderImpl(sr, allocator, null);
 129:   }
 130:   
 131:   public XMLEventReader createXMLEventReader(XMLStreamReader reader)
 132:     throws XMLStreamException
 133:   {
 134:     return new XMLEventReaderImpl(reader, allocator, null);
 135:   }
 136:   
 137:   public XMLEventReader createXMLEventReader(Source source)
 138:     throws XMLStreamException
 139:   {
 140:     XMLStreamReader sr = createXMLStreamReader(source);
 141:     return new XMLEventReaderImpl(sr, allocator, null);
 142:   }
 143:   
 144:   public XMLEventReader createXMLEventReader(InputStream in)
 145:     throws XMLStreamException
 146:   {
 147:     XMLStreamReader sr = createXMLStreamReader(in);
 148:     return new XMLEventReaderImpl(sr, allocator, null);
 149:   }
 150:   
 151:   public XMLEventReader createXMLEventReader(InputStream in, String encoding)
 152:     throws XMLStreamException
 153:   {
 154:     XMLStreamReader sr = createXMLStreamReader(in, encoding);
 155:     return new XMLEventReaderImpl(sr, allocator, null);
 156:   }
 157: 
 158:   public XMLStreamReader createFilteredReader(XMLStreamReader reader,
 159:                                               StreamFilter filter)
 160:     throws XMLStreamException
 161:   {
 162:     return new FilteredStreamReader(reader, filter);
 163:   }
 164: 
 165:   public XMLEventReader createFilteredReader(XMLEventReader reader,
 166:                                              EventFilter filter)
 167:     throws XMLStreamException
 168:   {
 169:     return new FilteredEventReader(reader, filter);
 170:   }
 171: 
 172:   public XMLResolver getXMLResolver()
 173:   {
 174:     return resolver;
 175:   }
 176: 
 177:   public void setXMLResolver(XMLResolver resolver)
 178:   {
 179:     this.resolver = resolver;
 180:   }
 181:   
 182:   public XMLReporter getXMLReporter()
 183:   {
 184:     return reporter;
 185:   }
 186: 
 187:   public void setXMLReporter(XMLReporter reporter)
 188:   {
 189:     this.reporter = reporter;
 190:   }
 191: 
 192:   public void setProperty(String name, Object value)
 193:     throws IllegalArgumentException
 194:   {
 195:     if (name.equals(IS_NAMESPACE_AWARE))
 196:       namespaceAware = ((Boolean) value).booleanValue();
 197:     else if (name.equals(IS_VALIDATING))
 198:       validating = ((Boolean) value).booleanValue();
 199:     else if (name.equals(IS_COALESCING))
 200:       coalescing = ((Boolean) value).booleanValue();
 201:     else if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
 202:       replacingEntityReferences = ((Boolean) value).booleanValue();
 203:     else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
 204:       externalEntities = ((Boolean) value).booleanValue();
 205:     else if (name.equals(SUPPORT_DTD))
 206:       supportDTD = ((Boolean) value).booleanValue();
 207:     else if (name.equals(REPORTER))
 208:       reporter = (XMLReporter) value;
 209:     else if (name.equals(RESOLVER))
 210:       resolver = (XMLResolver) value;
 211:     else if (name.equals(ALLOCATOR))
 212:       allocator = (XMLEventAllocator) value;
 213:     else
 214:       throw new IllegalArgumentException(name);
 215:   }
 216: 
 217:   public Object getProperty(String name)
 218:     throws IllegalArgumentException
 219:   {
 220:     if (name.equals(IS_NAMESPACE_AWARE))
 221:       return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
 222:     if (name.equals(IS_VALIDATING))
 223:       return validating ? Boolean.TRUE : Boolean.FALSE;
 224:     if (name.equals(IS_COALESCING))
 225:       return coalescing ? Boolean.TRUE : Boolean.FALSE;
 226:     if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
 227:       return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE;
 228:     if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
 229:       return externalEntities ? Boolean.TRUE : Boolean.FALSE;
 230:     if (name.equals(SUPPORT_DTD))
 231:       return supportDTD ? Boolean.TRUE : Boolean.FALSE;
 232:     if (name.equals(REPORTER))
 233:       return reporter;
 234:     if (name.equals(RESOLVER))
 235:       return resolver;
 236:     if (name.equals(ALLOCATOR))
 237:       return allocator;
 238:     throw new IllegalArgumentException(name);
 239:   }
 240: 
 241:   public boolean isPropertySupported(String name)
 242:   {
 243:     return name.equals(IS_NAMESPACE_AWARE) ||
 244:       name.equals(IS_VALIDATING) ||
 245:       name.equals(IS_COALESCING) ||
 246:       name.equals(IS_REPLACING_ENTITY_REFERENCES) ||
 247:       name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) ||
 248:       name.equals(SUPPORT_DTD) ||
 249:       name.equals(REPORTER) ||
 250:       name.equals(RESOLVER) ||
 251:       name.equals(ALLOCATOR);
 252:   }
 253:   
 254:   public void setEventAllocator(XMLEventAllocator allocator)
 255:   {
 256:     this.allocator = allocator;
 257:   }
 258: 
 259:   public XMLEventAllocator getEventAllocator()
 260:   {
 261:     return allocator;
 262:   }
 263: 
 264:   public void setCoalescing(boolean coalescing)
 265:   {
 266:     this.coalescing = coalescing;
 267:   }
 268: 
 269:   public boolean isCoalescing()
 270:   {
 271:     return coalescing;
 272:   }
 273: 
 274:   protected InputStream getInputStream(Source source)
 275:     throws XMLStreamException
 276:   {
 277:     InputStream in = null;
 278:     if (source instanceof StreamSource)
 279:       {
 280:         StreamSource streamSource = (StreamSource) source;
 281:         in = streamSource.getInputStream();
 282:       }
 283:     if (in == null)
 284:       {
 285:         String systemId = source.getSystemId();
 286:         try
 287:           {
 288:             URL url = new URL(systemId);
 289:             try
 290:               {
 291:                 in = url.openStream();
 292:               }
 293:             catch (IOException e2)
 294:               {
 295:                 XMLStreamException e3 = new XMLStreamException(e2);
 296:                 e3.initCause(e2);
 297:                 throw e3;
 298:               }
 299:           }
 300:         catch (MalformedURLException e)
 301:           {
 302:             // Fall back to relative file
 303:             if (File.separatorChar != '/')
 304:               systemId = systemId.replace('/', File.separatorChar);
 305:             try
 306:               {
 307:                 in = new FileInputStream(systemId);
 308:               }
 309:             catch (FileNotFoundException e2)
 310:               {
 311:                 XMLStreamException e3 = new XMLStreamException(e2);
 312:                 e3.initCause(e2);
 313:                 throw e3;
 314:               }
 315:           }
 316:       }
 317:     return in;
 318:   }
 319:   
 320: }