KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > ie > filters > MagnoliaV2Filter


1 package info.magnolia.cms.core.ie.filters;
2
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.SAXException JavaDoc;
5 import org.xml.sax.XMLReader JavaDoc;
6 import org.xml.sax.helpers.AttributesImpl JavaDoc;
7 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
8
9
10 /**
11  * Sax filter, strips version information from a jcr xml (system view).
12  */

13 public class MagnoliaV2Filter extends XMLFilterImpl JavaDoc {
14
15     /**
16      * if != 0 we are in the middle of a filtered element.
17      */

18     private int inMetadataElement;
19
20     private boolean skipNode;
21
22     private boolean skipProperty;
23
24     /**
25      * Instantiates a new version filter.
26      * @param parent wrapped XMLReader
27      */

28     public MagnoliaV2Filter(XMLReader JavaDoc parent) {
29         super(parent);
30     }
31
32     /**
33      * @see org.xml.sax.helpers.XMLFilterImpl#endElement(String, String, String)
34      */

35     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
36
37         if (inMetadataElement > 0) {
38             inMetadataElement--;
39         }
40
41         if (skipNode && "sv:node".equals(qName)) {
42             skipNode = false;
43             return;
44         }
45
46         if (skipProperty) {
47             if ("sv:property".equals(qName)) {
48                 skipProperty = false;
49             }
50             return;
51         }
52
53         super.endElement(uri, localName, qName);
54     }
55
56     /**
57      * @see org.xml.sax.helpers.XMLFilterImpl#characters(char[], int, int)
58      */

59     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
60         // filter content
61
// if (inMetadataElement == 0) {
62
super.characters(ch, start, length);
63         // }
64
}
65
66     /**
67      * @see org.xml.sax.helpers.XMLFilterImpl#startElement(String, String, String, Attributes)
68      */

69     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
70
71         if (inMetadataElement > 0) {
72             inMetadataElement++;
73         }
74
75         String JavaDoc svname = atts.getValue("sv:name");
76
77         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
78             inMetadataElement++;
79         }
80
81         if (inMetadataElement > 0) {
82             // remove
83
// <sv:node sv:name="jcr:content">
84
if ("sv:node".equals(qName) && "jcr:content".equals(svname)) {
85                 skipNode = true;
86                 return;
87             }
88             if ("sv:property".equals(qName)
89                 && ("sequenceposition".equals(svname) || "jcr:primaryType".equals(svname) || "jcr:isCheckedOut"
90                     .equals(svname))) {
91                 skipProperty = true;
92                 return;
93             }
94             if ("sv:property".equals(qName)
95                 && ("Data".equals(svname) || "template".equals(svname) || "authorid".equals(svname) || "title"
96                     .equals(svname))) {
97                 atts = new AttributesImpl JavaDoc();
98                 ((AttributesImpl JavaDoc) atts).addAttribute(uri, "name", "sv:name", uri, "mgnl:" + svname);
99                 ((AttributesImpl JavaDoc) atts).addAttribute(uri, "type", "sv:type", uri, "String");
100             }
101
102             else if ("sv:property".equals(qName) && ("creationdate".equals(svname) || "lastmodified".equals(svname))) {
103                 atts = new AttributesImpl JavaDoc();
104                 ((AttributesImpl JavaDoc) atts).addAttribute(uri, "name", "sv:name", uri, "mgnl:" + svname);
105                 ((AttributesImpl JavaDoc) atts).addAttribute(uri, "type", "sv:type", uri, "Date");
106             }
107
108         }
109
110         super.startElement(uri, localName, qName, atts);
111
112         if ("sv:node".equals(qName) && "MetaData".equals(svname)) {
113
114             // add:
115
// <sv:property sv:name="jcr:primaryType" sv:type="Name">
116
// <sv:value>nt:unstructured</sv:value>
117
// </sv:property>
118

119             String JavaDoc atturi = atts.getURI(0);
120             AttributesImpl JavaDoc atts2 = new AttributesImpl JavaDoc();
121             atts2.addAttribute(uri, "name", "sv:name", atturi, "jcr:primaryType");
122             atts2.addAttribute(uri, "type", "sv:type", atturi, "Name");
123
124             super.startElement(uri, "property", "sv:property", atts2);
125             super.startElement(uri, "value", "sv:value", new AttributesImpl JavaDoc());
126             super.characters(new char[]{'m', 'g', 'n', 'l', ':', 'm', 'e', 't', 'a', 'D', 'a', 't', 'a'}, 0, 13);
127             super.endElement(uri, "value", "sv:value");
128             super.endElement(uri, "property", "sv:property");
129         }
130     }
131 }
Popular Tags