1 /*
2  * Copyright (c) 2005, 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 javax.xml.stream.events.EntityDeclaration;
29 import javax.xml.stream.events.XMLEvent;
30 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
31 
32 /**
33  *
34  * This class store all the information for a particular EntityDeclaration. EntityDeclaration interface
35  * has various get* functiosn to retirve information about a particular EntityDeclaration.
36  *
37  * @author  Neeraj Bajaj, Sun Microsystems.
38  */
39 public class EntityDeclarationImpl extends DummyEvent implements EntityDeclaration {
40 
41     private XMLResourceIdentifier fXMLResourceIdentifier ;
42     private String fEntityName;
43     private String fReplacementText;
44     private String fNotationName;
45 
46     /** Creates a new instance of EntityDeclarationImpl */
EntityDeclarationImpl()47     public EntityDeclarationImpl() {
48         init();
49     }
50 
EntityDeclarationImpl(String entityName , String replacementText)51     public EntityDeclarationImpl(String entityName , String replacementText){
52         this(entityName,replacementText,null);
53 
54     }
55 
EntityDeclarationImpl(String entityName, String replacementText, XMLResourceIdentifier resourceIdentifier)56     public EntityDeclarationImpl(String entityName, String replacementText, XMLResourceIdentifier resourceIdentifier){
57         init();
58         fEntityName = entityName;
59         fReplacementText = replacementText;
60         fXMLResourceIdentifier = resourceIdentifier;
61     }
62 
setEntityName(String entityName)63     public void setEntityName(String entityName){
64         fEntityName = entityName;
65     }
66 
getEntityName()67     public String getEntityName(){
68         return fEntityName;
69     }
70 
setEntityReplacementText(String replacementText)71     public void setEntityReplacementText(String replacementText){
72         fReplacementText = replacementText;
73     }
74 
setXMLResourceIdentifier(XMLResourceIdentifier resourceIdentifier)75     public void setXMLResourceIdentifier(XMLResourceIdentifier resourceIdentifier){
76         fXMLResourceIdentifier = resourceIdentifier ;
77     }
78 
getXMLResourceIdentifier()79     public XMLResourceIdentifier getXMLResourceIdentifier(){
80         return fXMLResourceIdentifier;
81     }
82 
getSystemId()83     public String getSystemId(){
84         if(fXMLResourceIdentifier != null)
85             return fXMLResourceIdentifier.getLiteralSystemId();
86         return null;
87     }
88 
getPublicId()89     public String getPublicId(){
90         if(fXMLResourceIdentifier != null)
91             return fXMLResourceIdentifier.getPublicId();
92 
93         return null;
94     }
95 
getBaseURI()96     public String getBaseURI() {
97         if(fXMLResourceIdentifier != null)
98             return fXMLResourceIdentifier.getBaseSystemId();
99         return null;
100     }
101 
getName()102     public String getName(){
103         return fEntityName;
104     }
105 
getNotationName()106     public String getNotationName() {
107         return fNotationName;
108     }
109 
setNotationName(String notationName)110     public void setNotationName(String notationName){
111         fNotationName = notationName;
112     }
113 
getReplacementText()114     public String getReplacementText() {
115         return fReplacementText;
116     }
117 
init()118     protected void init(){
119         setEventType(XMLEvent.ENTITY_DECLARATION);
120     }
121 
writeAsEncodedUnicodeEx(java.io.Writer writer)122     protected void writeAsEncodedUnicodeEx(java.io.Writer writer)
123     throws java.io.IOException
124     {
125         writer.write("<!ENTITY ");
126         writer.write(fEntityName);
127         if (fReplacementText != null) {
128             //internal entity
129             //escape quotes, lt and amps
130             writer.write(" \"");
131             charEncode(writer, fReplacementText);
132         } else {
133             //external entity
134             String pubId = getPublicId();
135             if (pubId != null) {
136                 writer.write(" PUBLIC \"");
137                 writer.write(pubId);
138             } else {
139                 writer.write(" SYSTEM \"");
140                 writer.write(getSystemId());
141             }
142         }
143         writer.write("\"");
144         if (fNotationName != null) {
145             writer.write(" NDATA ");
146             writer.write(fNotationName);
147         }
148         writer.write(">");
149     }
150 }
151