1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.stream.events;
  27 
  28 import java.util.Iterator;
  29 import javax.xml.namespace.NamespaceContext;
  30 import javax.xml.namespace.QName;
  31 import javax.xml.stream.XMLEventFactory;
  32 import javax.xml.stream.Location;
  33 import javax.xml.stream.events.Attribute;
  34 import javax.xml.stream.events.Characters;
  35 import javax.xml.stream.events.ProcessingInstruction;
  36 import javax.xml.stream.events.Namespace;
  37 import javax.xml.stream.events.EntityDeclaration;
  38 import javax.xml.stream.events.EntityReference;
  39 import javax.xml.stream.events.StartDocument;
  40 import javax.xml.stream.events.StartElement;
  41 
  42 
  43 /**
  44  *
  45  * @author  Neeraj Bajaj, k.venugopal@sun.com
  46  */
  47 public class XMLEventFactoryImpl extends XMLEventFactory {
  48 
  49     Location location = null;
  50     /** Creates a new instance of XMLEventFactory */
  51     public XMLEventFactoryImpl() {
  52     }
  53 
  54     @Override
  55     public Attribute createAttribute(String localName, String value) {
  56         AttributeImpl attr =  new AttributeImpl(localName, value);
  57         if(location != null)attr.setLocation(location);
  58         return attr;
  59     }
  60 
  61     @Override
  62     public Attribute createAttribute(QName name, String value) {
  63         return createAttribute(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart(), value);
  64     }
  65 
  66     @Override
  67     public Attribute createAttribute(String prefix, String namespaceURI, String localName, String value) {
  68         AttributeImpl attr =  new AttributeImpl(prefix, namespaceURI, localName, value, null);
  69         if(location != null)attr.setLocation(location);
  70         return attr;
  71     }
  72 
  73     @Override
  74     public Characters createCData(String content) {
  75         //stax doesn't have separate CDATA event. This is taken care by
  76         //CHRACTERS event setting the cdata flag to true.
  77         CharacterEvent charEvent =  new CharacterEvent(content, true);
  78         if(location != null)charEvent.setLocation(location);
  79         return charEvent;
  80     }
  81 
  82     @Override
  83     public Characters createCharacters(String content) {
  84         CharacterEvent charEvent =  new CharacterEvent(content);
  85         if(location != null)charEvent.setLocation(location);
  86         return charEvent;
  87     }
  88 
  89     @Override
  90     public javax.xml.stream.events.Comment createComment(String text) {
  91         CommentEvent charEvent =  new CommentEvent(text);
  92         if(location != null)charEvent.setLocation(location);
  93         return charEvent;
  94     }
  95 
  96     @Override
  97     public javax.xml.stream.events.DTD createDTD(String dtd) {
  98         DTDEvent dtdEvent = new DTDEvent(dtd);
  99         if(location != null)dtdEvent.setLocation(location);
 100         return dtdEvent;
 101     }
 102 
 103     @Override
 104     public javax.xml.stream.events.EndDocument createEndDocument() {
 105         EndDocumentEvent event =new EndDocumentEvent();
 106         if(location != null)event.setLocation(location);
 107         return event;
 108     }
 109 
 110     @Override
 111     public javax.xml.stream.events.EndElement createEndElement(QName name,
 112             Iterator<? extends Namespace> namespaces) {
 113         return createEndElement(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart());
 114     }
 115 
 116     @Override
 117     public javax.xml.stream.events.EndElement createEndElement(
 118             String prefix, String namespaceUri, String localName) {
 119         EndElementEvent event =  new EndElementEvent(prefix, namespaceUri, localName);
 120         if(location != null)event.setLocation(location);
 121         return event;
 122     }
 123 
 124     @Override
 125     public javax.xml.stream.events.EndElement createEndElement(String prefix, String namespaceUri,
 126             String localName, Iterator<? extends Namespace> namespaces) {
 127 
 128         EndElementEvent event =  new EndElementEvent(prefix, namespaceUri, localName);
 129         if(namespaces!=null){
 130             while(namespaces.hasNext())
 131                 event.addNamespace(namespaces.next());
 132         }
 133         if(location != null)event.setLocation(location);
 134         return event;
 135     }
 136 
 137     @Override
 138     public EntityReference createEntityReference(String name, EntityDeclaration entityDeclaration) {
 139         EntityReferenceEvent event =  new EntityReferenceEvent(name, entityDeclaration);
 140         if(location != null)event.setLocation(location);
 141         return event;
 142     }
 143 
 144     @Override
 145     public Characters createIgnorableSpace(String content) {
 146         CharacterEvent event =  new CharacterEvent(content, false, true);
 147         if(location != null)event.setLocation(location);
 148         return event;
 149     }
 150 
 151     @Override
 152     public Namespace createNamespace(String namespaceURI) {
 153         NamespaceImpl event =  new NamespaceImpl(namespaceURI);
 154         if(location != null)event.setLocation(location);
 155         return event;
 156     }
 157 
 158     @Override
 159     public Namespace createNamespace(String prefix, String namespaceURI) {
 160         NamespaceImpl event =  new NamespaceImpl(prefix, namespaceURI);
 161         if(location != null)event.setLocation(location);
 162         return event;
 163     }
 164 
 165     @Override
 166     public ProcessingInstruction createProcessingInstruction(String target, String data) {
 167         ProcessingInstructionEvent event =  new ProcessingInstructionEvent(target, data);
 168         if(location != null)event.setLocation(location);
 169         return event;
 170     }
 171 
 172     @Override
 173     public Characters createSpace(String content) {
 174         CharacterEvent event =  new CharacterEvent(content);
 175         if(location != null)event.setLocation(location);
 176         return event;
 177     }
 178 
 179     @Override
 180     public StartDocument createStartDocument() {
 181         StartDocumentEvent event = new StartDocumentEvent();
 182         if(location != null)event.setLocation(location);
 183         return event;
 184     }
 185 
 186     @Override
 187     public StartDocument createStartDocument(String encoding) {
 188         StartDocumentEvent event =  new StartDocumentEvent(encoding);
 189         if(location != null)event.setLocation(location);
 190         return event;
 191     }
 192 
 193     @Override
 194     public StartDocument createStartDocument(String encoding, String version) {
 195         StartDocumentEvent event =  new StartDocumentEvent(encoding, version);
 196         if(location != null)event.setLocation(location);
 197         return event;
 198     }
 199 
 200     @Override
 201     public StartDocument createStartDocument(String encoding, String version, boolean standalone) {
 202         StartDocumentEvent event =  new StartDocumentEvent(encoding, version, standalone);
 203         if(location != null)event.setLocation(location);
 204         return event;
 205     }
 206 
 207     @Override
 208     public StartElement createStartElement(QName name, Iterator<? extends Attribute> attributes,
 209             Iterator<? extends Namespace> namespaces) {
 210         return createStartElement(name.getPrefix(), name.getNamespaceURI(),
 211                 name.getLocalPart(), attributes, namespaces);
 212     }
 213 
 214     @Override
 215     public StartElement createStartElement(String prefix, String namespaceUri, String localName) {
 216         StartElementEvent event =  new StartElementEvent(prefix, namespaceUri, localName);
 217         if(location != null)event.setLocation(location);
 218         return event;
 219     }
 220 
 221     @Override
 222     public StartElement createStartElement(String prefix, String namespaceUri,
 223             String localName, Iterator<? extends Attribute> attributes,
 224             Iterator<? extends Namespace> namespaces) {
 225         return createStartElement(prefix, namespaceUri, localName, attributes, namespaces, null);
 226     }
 227 
 228     @Override
 229     public StartElement createStartElement(String prefix, String namespaceUri,
 230             String localName, Iterator<? extends Attribute> attributes,
 231             Iterator<? extends Namespace> namespaces, NamespaceContext context) {
 232         StartElementEvent elem =  new StartElementEvent(prefix, namespaceUri, localName);
 233         elem.addAttributes(attributes);
 234         elem.addNamespaceAttributes(namespaces);
 235         elem.setNamespaceContext(context);
 236         if(location != null)elem.setLocation(location);
 237         return elem;
 238     }
 239 
 240     @Override
 241     public void setLocation(javax.xml.stream.Location location) {
 242         this.location = location;
 243     }
 244 
 245 }