KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import org.xml.sax.ContentHandler JavaDoc;
4 import org.xml.sax.Attributes JavaDoc;
5 import org.xml.sax.SAXException JavaDoc;
6 import org.xml.sax.Locator JavaDoc;
7
8 public class XTASMLContentHandler implements ContentHandler JavaDoc
9 {
10   private static final int ROOT = 0;
11   private static final int QUERY = 1;
12   private static final int INSTRUCTION = 2;
13
14   private int state = ROOT;
15
16   private boolean hasPrefixes = false;
17
18   private String JavaDoc sourceId;
19   private String JavaDoc destinationId;
20
21   private String JavaDoc match;
22   private String JavaDoc type;
23   private String JavaDoc newValue = "";
24
25
26   public XTASMLContentHandler ()
27   {
28 // this.statement = statement;
29
}
30
31   public void setDocumentLocator (Locator JavaDoc locator)
32   {
33   }
34
35   public void startDocument ()
36     throws SAXException JavaDoc
37   {
38   }
39
40   public void endDocument()
41     throws SAXException JavaDoc
42   {
43   }
44
45   public void startPrefixMapping (String JavaDoc prefix, String JavaDoc uri)
46     throws SAXException JavaDoc
47   {
48 // if (!prefix.equals("")
49
this.hasPrefixes = true;
50       System.out.println("Prefix: "+prefix+" "+uri);
51   }
52
53
54   public void endPrefixMapping (String JavaDoc prefix)
55     throws SAXException JavaDoc
56   {
57   }
58
59   public void startElement (String JavaDoc namespaceURI, String JavaDoc localName,
60                             String JavaDoc qName, Attributes JavaDoc atts)
61     throws SAXException JavaDoc
62   {
63 // if (!namespaceURI.equals(""))
64
// System.out.println("Element: "+namespaceURI+" "+qName+" "+localName);
65

66     if( state == ROOT ) {
67         if( localName.equals("query") ) {
68
69             int n = atts.getLength();
70             // source and destination
71
for(int i = 0; i < n; i++) {
72                if( atts.getLocalName(i).equals("source") )
73                   sourceId = atts.getValue(i);
74                else if( atts.getLocalName(i).equals("destination") )
75                   destinationId = atts.getValue(i);
76                else
77                if(!hasPrefixes)
78                   throw new SAXException JavaDoc("Unknown query attribute '"+atts.getQName(i)+"'!");
79             }
80             state = QUERY;
81
82         } else
83             throw new SAXException JavaDoc("The XTASML's root element must be 'query'!");
84
85     } else if( state == QUERY ) {
86         type = localName;
87         int n = atts.getLength();
88         // match or xpath
89
for(int i = 0; i < n; i++) {
90            if( atts.getLocalName(i).equals("match") || atts.getLocalName(i).equals("xpath") || atts.getLocalName(i).equals("resource"))
91                match=atts.getValue(i);
92             else
93                throw new SAXException JavaDoc("Unknown query attribute '"+atts.getQName(i)+"'!");
94             }
95          state = INSTRUCTION;
96     } else if( state == INSTRUCTION ) {
97         newValue+="<"+localName;
98         int n = atts.getLength();
99         for(int i = 0; i < n; i++) {
100           newValue+=" "+atts.getLocalName(i)+"=\""+atts.getValue(i)+"\"";
101         }
102         newValue+=">";
103
104     }
105
106
107   }
108
109
110   public void endElement (String JavaDoc namespaceURI, String JavaDoc localName,
111                           String JavaDoc qName)
112     throws SAXException JavaDoc
113   {
114      if( localName == type )
115          state = QUERY;
116
117      if( state == INSTRUCTION ) {
118         newValue+="</"+localName+">";
119       }
120
121   }
122
123
124   public void characters (char[] ch, int start, int length)
125     throws SAXException JavaDoc
126   {
127 // String s = new String(ch, start, (length > 30) ? 30 : length);
128
String JavaDoc s = new String JavaDoc(ch, start, length);
129     newValue+=s.trim();
130
131 // System.out.println("characters: \""+s+"\"");
132

133   }
134
135
136   public void ignorableWhitespace (char[] ch, int start, int length)
137     throws SAXException JavaDoc
138   {
139     String JavaDoc s = new String JavaDoc(ch, start, length);
140     newValue+=s;
141 // System.out.println("characters: \""+s+"\"");
142

143   }
144
145   public void processingInstruction (String JavaDoc target, String JavaDoc data)
146     throws SAXException JavaDoc
147   {
148   }
149
150   public void skippedEntity (String JavaDoc name)
151     throws SAXException JavaDoc
152   {
153   }
154
155   public String JavaDoc getSourceId()
156   {
157     return sourceId;
158   }
159
160   public String JavaDoc getDestinationId()
161   {
162     return destinationId;
163   }
164
165   public String JavaDoc getType()
166   {
167     return type;
168   }
169
170   public String JavaDoc getMatch()
171   {
172     return match;
173   }
174
175   public String JavaDoc getNewValue()
176   {
177     return newValue;
178   }
179 }
180
Popular Tags