KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  */

4 package info.magnolia.cms.core.ie.filters;
5
6 import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.SAXException JavaDoc;
8 import org.xml.sax.XMLReader JavaDoc;
9 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
10
11
12 public class ImportXmlRootFilter extends XMLFilterImpl JavaDoc {
13
14     // this is true if it is an import of a file containing jcr:root node
15
public boolean rootNodeFound = false;
16
17     private boolean isRootNode = false;
18
19     private boolean isPrimaryTypeProperty = false;
20
21     private boolean isPrimaryTypeValue = false;
22
23     /**
24      * if != 0 we are in the middle of a filtered element.
25      */

26     private int inFilterElement;
27
28     public ImportXmlRootFilter(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 (inFilterElement > 0) {
38             inFilterElement--;
39             return;
40         }
41
42         super.endElement(uri, localName, qName);
43     }
44
45     /**
46      * Root node was found should be called after parsing to check if a root node was indeed found
47      */

48     public boolean rootNodeWasFound() {
49         return rootNodeFound;
50     }
51
52     /**
53      * @see org.xml.sax.helpers.XMLFilterImpl#characters(char[], int, int)
54      */

55     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
56         // filter content
57
if (inFilterElement == 0) {
58             // change primary type of root node
59
if (this.isPrimaryTypeValue) {
60                 this.isRootNode = false;
61                 this.isPrimaryTypeProperty = false;
62                 this.isPrimaryTypeValue = false;
63
64                 super.characters("mgnl:content".toCharArray(), 0, "mgnl:content".length());
65             }
66             else {
67                 super.characters(ch, start, length);
68             }
69         }
70     }
71
72     /**
73      * @see org.xml.sax.helpers.XMLFilterImpl#startElement(String, String, String, Attributes)
74      */

75     /*
76      * (non-Javadoc)
77      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
78      * org.xml.sax.Attributes)
79      */

80     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
81         // filter if already in a version element
82
if (inFilterElement > 0) {
83             inFilterElement++;
84             return;
85         }
86
87         // filter if it is the version store
88
if ("sv:node".equals(qName)) { //$NON-NLS-1$
89
String JavaDoc attName = atts.getValue("sv:name"); //$NON-NLS-1$
90
// remember if there was a root node presend
91
if ("jcr:root".equals(attName)) {
92                 this.rootNodeFound = true;
93                 this.isRootNode = true;
94             }
95
96             if ("jcr:system".equals(attName)) { //$NON-NLS-1$
97
inFilterElement++;
98                 return;
99             }
100         }
101
102         // change the nodetype of the jcr:root node
103
if (this.isRootNode && "sv:property".equals(qName) && "jcr:primaryType".equals(atts.getValue("sv:name"))) {
104             this.isPrimaryTypeProperty = true;
105         }
106
107         if (this.isPrimaryTypeProperty && "sv:value".equals(qName)) {
108             this.isPrimaryTypeValue = true;
109         }
110
111         super.startElement(uri, localName, qName, atts);
112     }
113 }
Popular Tags