1   
2   
3   
4   
5   
6   
7   
8   package org.dom4j.tree;
9   
10  import java.io.IOException;
11  import java.io.Writer;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.dom4j.DocumentType;
16  import org.dom4j.Element;
17  import org.dom4j.Visitor;
18  
19  /***
20   * <p>
21   * <code>AbstractDocumentType</code> is an abstract base class for tree
22   * implementors to use for implementation inheritence.
23   * </p>
24   * 
25   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
26   * @version $Revision: 1.17 $
27   */
28  public abstract class AbstractDocumentType extends AbstractNode implements
29          DocumentType {
30      public AbstractDocumentType() {
31      }
32  
33      public short getNodeType() {
34          return DOCUMENT_TYPE_NODE;
35      }
36  
37      public String getName() {
38          return getElementName();
39      }
40  
41      public void setName(String name) {
42          setElementName(name);
43      }
44  
45      public String getPath(Element context) {
46          
47          return "";
48      }
49  
50      public String getUniquePath(Element context) {
51          
52          return "";
53      }
54  
55      /***
56       * Returns the text format of the declarations if applicable, or the empty
57       * String
58       * 
59       * @return DOCUMENT ME!
60       */
61      public String getText() {
62          List list = getInternalDeclarations();
63  
64          if ((list != null) && (list.size() > 0)) {
65              StringBuffer buffer = new StringBuffer();
66              Iterator iter = list.iterator();
67  
68              if (iter.hasNext()) {
69                  Object decl = iter.next();
70                  buffer.append(decl.toString());
71  
72                  while (iter.hasNext()) {
73                      decl = iter.next();
74                      buffer.append("\n");
75                      buffer.append(decl.toString());
76                  }
77              }
78  
79              return buffer.toString();
80          }
81  
82          return "";
83      }
84  
85      public String toString() {
86          return super.toString() + " [DocumentType: " + asXML() + "]";
87      }
88  
89      public String asXML() {
90          StringBuffer buffer = new StringBuffer("<!DOCTYPE ");
91          buffer.append(getElementName());
92  
93          boolean hasPublicID = false;
94          String publicID = getPublicID();
95  
96          if ((publicID != null) && (publicID.length() > 0)) {
97              buffer.append(" PUBLIC \"");
98              buffer.append(publicID);
99              buffer.append("\"");
100             hasPublicID = true;
101         }
102 
103         String systemID = getSystemID();
104 
105         if ((systemID != null) && (systemID.length() > 0)) {
106             if (!hasPublicID) {
107                 buffer.append(" SYSTEM");
108             }
109 
110             buffer.append(" \"");
111             buffer.append(systemID);
112             buffer.append("\"");
113         }
114 
115         buffer.append(">");
116 
117         return buffer.toString();
118     }
119 
120     public void write(Writer writer) throws IOException {
121         writer.write("<!DOCTYPE ");
122         writer.write(getElementName());
123 
124         boolean hasPublicID = false;
125         String publicID = getPublicID();
126 
127         if ((publicID != null) && (publicID.length() > 0)) {
128             writer.write(" PUBLIC \"");
129             writer.write(publicID);
130             writer.write("\"");
131             hasPublicID = true;
132         }
133 
134         String systemID = getSystemID();
135 
136         if ((systemID != null) && (systemID.length() > 0)) {
137             if (!hasPublicID) {
138                 writer.write(" SYSTEM");
139             }
140 
141             writer.write(" \"");
142             writer.write(systemID);
143             writer.write("\"");
144         }
145 
146         List list = getInternalDeclarations();
147 
148         if ((list != null) && (list.size() > 0)) {
149             writer.write(" [");
150 
151             for (Iterator iter = list.iterator(); iter.hasNext();) {
152                 Object decl = iter.next();
153                 writer.write("\n  ");
154                 writer.write(decl.toString());
155             }
156 
157             writer.write("\n]");
158         }
159 
160         writer.write(">");
161     }
162 
163     public void accept(Visitor visitor) {
164         visitor.visit(this);
165     }
166 }
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203