svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / persistence / Handler.java @ 1956
History | View | Annotate | Download (3.09 KB)
1 |
package com.hardcode.gdbms.engine.data.persistence; |
---|---|
2 |
|
3 |
import java.io.FileNotFoundException; |
4 |
import java.io.PrintWriter; |
5 |
import java.util.Stack; |
6 |
|
7 |
import org.xml.sax.Attributes; |
8 |
import org.xml.sax.ContentHandler; |
9 |
import org.xml.sax.Locator; |
10 |
import org.xml.sax.SAXException; |
11 |
|
12 |
|
13 |
public class Handler implements ContentHandler{ |
14 |
private PrintWriter out; |
15 |
private String sangrado = ""; |
16 |
private Stack empty = new Stack(); |
17 |
|
18 |
public Handler(){ |
19 |
empty.push(Boolean.FALSE);
|
20 |
} |
21 |
|
22 |
|
23 |
/**
|
24 |
* @see org.xml.sax.ContentHandler#endDocument()
|
25 |
*/
|
26 |
public void endDocument() throws SAXException { |
27 |
out.close(); |
28 |
} |
29 |
|
30 |
/**
|
31 |
* @see org.xml.sax.ContentHandler#startDocument()
|
32 |
*/
|
33 |
public void startDocument() throws SAXException { |
34 |
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
35 |
} |
36 |
|
37 |
/**
|
38 |
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
39 |
*/
|
40 |
public void characters(char[] ch, int start, int length) throws SAXException { |
41 |
} |
42 |
|
43 |
/**
|
44 |
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
|
45 |
*/
|
46 |
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { |
47 |
} |
48 |
|
49 |
/**
|
50 |
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
51 |
*/
|
52 |
public void endPrefixMapping(String prefix) throws SAXException { |
53 |
} |
54 |
|
55 |
/**
|
56 |
* @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
57 |
*/
|
58 |
public void skippedEntity(String name) throws SAXException { |
59 |
} |
60 |
|
61 |
/**
|
62 |
* @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
63 |
*/
|
64 |
public void setDocumentLocator(Locator locator) { |
65 |
} |
66 |
|
67 |
/**
|
68 |
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
|
69 |
*/
|
70 |
public void processingInstruction(String target, String data) throws SAXException { |
71 |
} |
72 |
|
73 |
/**
|
74 |
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
|
75 |
*/
|
76 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
77 |
} |
78 |
|
79 |
/**
|
80 |
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
|
81 |
*/
|
82 |
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { |
83 |
sangrado = sangrado.substring(0, sangrado.length()-2); |
84 |
boolean vacio = ((Boolean)empty.pop()).booleanValue(); |
85 |
if (vacio){
|
86 |
out.println("/>");
|
87 |
}else{
|
88 |
out.println(sangrado + "</" + qName + ">"); |
89 |
} |
90 |
} |
91 |
|
92 |
/**
|
93 |
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
94 |
*/
|
95 |
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { |
96 |
boolean vacio = ((Boolean)empty.peek()).booleanValue(); |
97 |
String cadena = ""; |
98 |
if (vacio){
|
99 |
cadena = ">\n";
|
100 |
empty.pop(); |
101 |
empty.push(Boolean.FALSE);
|
102 |
} |
103 |
|
104 |
String attsString = ""; |
105 |
for (int i = 0; i < atts.getLength(); i++){ |
106 |
attsString+=" " + atts.getQName(i) + "=\""+ atts.getValue(i)+"\""; |
107 |
} |
108 |
|
109 |
cadena += sangrado + "<" + qName + attsString;
|
110 |
out.print(cadena); |
111 |
sangrado += " ";
|
112 |
empty.push(Boolean.TRUE);
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* @param writer
|
117 |
*/
|
118 |
public void setOut(PrintWriter writer) throws FileNotFoundException { |
119 |
out = writer; |
120 |
} |
121 |
|
122 |
} |