KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > xml > DtdTracer


1 package org.exoplatform.services.xml.querying.impl.xtas.xml;
2
3 import org.xml.sax.SAXException JavaDoc;
4 import org.xml.sax.ext.DeclHandler JavaDoc;
5 import org.xml.sax.ext.LexicalHandler JavaDoc;
6
7 /**
8  * Lexical handler for the SAXTransformer used in the UniFormTree
9  * @version $Id: DtdTracer.java 566 2005-01-25 12:50:49Z kravchuk $
10  */

11 public class DtdTracer
12       implements LexicalHandler JavaDoc,DeclHandler JavaDoc
13 {
14
15    private String JavaDoc name;
16    private String JavaDoc publicId;
17    private String JavaDoc systemId;
18
19    private String JavaDoc text="";
20    private boolean isInternal = false;
21
22     //
23
// LexicalHandler methods
24
//
25

26     public DtdTracer()
27     {
28        name=null;
29        publicId=null;
30        systemId=null;
31
32        text="";
33        isInternal = false;
34
35     }
36
37     /** Start DTD. */
38     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
39         throws SAXException JavaDoc {
40
41         //<!DOCTYPE [name] SYSTEM "[systemId]" PUBLIC "[publicId]">
42
//<!DOCTYPE employees [
43
this.name = name;
44         this.systemId = systemId;
45         this.publicId = publicId;
46
47
48         if (systemId == null)
49 // throw new SAXException("Only External DTD is Supported yet!");
50
isInternal = true;
51         else {
52           isInternal = false;
53           text = "<!DOCTYPE "+name+" SYSTEM \""+systemId+"\">";
54         }
55        
56
57     }
58
59     /** Start entity. */
60     public void startEntity(String JavaDoc name) throws SAXException JavaDoc
61     { }
62
63     /** Start CDATA section. */
64     public void startCDATA() throws SAXException JavaDoc
65     { }
66
67     /** End CDATA section. */
68     public void endCDATA() throws SAXException JavaDoc
69     { }
70
71     /** Comment. */
72     public void comment(char[] ch, int offset, int length)
73         throws SAXException JavaDoc
74     { /*System.out.println("Comment ->"+ch.length+" "+length+" "+offset);*/ }
75
76     /** End entity. */
77     public void endEntity(String JavaDoc name) throws SAXException JavaDoc
78     { }
79
80     /** End DTD. */
81     public void endDTD() throws SAXException JavaDoc {
82        if ( isInternal )
83          text+="]>";
84     }
85
86     //
87
// DeclHandler methods
88
//
89

90     /** Element declaration. */
91     public void elementDecl(String JavaDoc name, String JavaDoc contentModel)
92         throws SAXException JavaDoc {
93
94        //<!ELEMENT employee (firstname, lastname, telephone, address?)>
95
if( isInternal )
96          text+="<!ELEMENT "+name+"("+contentModel+")>\n";
97     }
98
99     /** Attribute declaration. */
100     public void attributeDecl(String JavaDoc elementName, String JavaDoc attributeName,
101                               String JavaDoc type, String JavaDoc valueDefault,
102                               String JavaDoc value) throws SAXException JavaDoc {
103
104         //<!ATTLIST employee id ID #REQUIRED>
105
if( isInternal )
106           text+="<!ATTLIST "+elementName+" "+attributeName+" "+
107                       type+" "+valueDefault+" "+value+">\n";
108
109
110     }
111
112     /** Internal entity declaration. */
113     public void internalEntityDecl(String JavaDoc name, String JavaDoc text)
114         throws SAXException JavaDoc {
115
116         // <!ENTITY PICK5 "0">
117
if( isInternal )
118           text+="<!ENTITY "+name+"\""+text+"\">\n";
119
120     }
121
122     /** External entity declaration. */
123     public void externalEntityDecl(String JavaDoc name,
124                                    String JavaDoc publicId, String JavaDoc systemId)
125         throws SAXException JavaDoc {
126
127         //<!ENTITY ttt SYSTEM "http://www.ttt/ttt.xml" PUBLIC "tttt">
128
String JavaDoc publicStr=(publicId==null)?"":" PUBLIC "+"\""+publicId+"\"";
129         String JavaDoc systemStr=(systemId==null)?"":" SYSTEM "+"\""+systemId+"\"";
130
131        if( isInternal )
132           text+="<!ENTITY "+name+"\""+name+"\">"+systemStr+publicStr+"\n";
133
134     }
135
136     public String JavaDoc getSystemId()
137     {
138        return systemId;
139     }
140
141     public String JavaDoc getPublicId()
142     {
143        return publicId;
144     }
145
146     public String JavaDoc getText()
147     {
148        return text;
149     }
150
151 }
Popular Tags