1    package com.instantbank.collections.main.web;
2    
3    import java.io.ByteArrayInputStream;
4    import javax.servlet.jsp.PageContext;
5    import javax.servlet.jsp.JspException;
6    import javax.servlet.jsp.tagext.TagSupport;
7    import oracle.xml.parser.v2.DOMParser;
8    import oracle.xml.parser.v2.XMLDocument;
9    
10   public class DomParserTag extends TagSupport {
11     private String xml;
12     private boolean force = false;
13   
14   
15     public void setXml(String xml) {
16       this.xml = xml;
17     }
18   
19   
20     public void setId(String id) {
21       this.id = id;
22     }
23   
24   
25     public void setForce(boolean force) {
26       this.force = force;
27     }
28   
29   
30     public int doStartTag() throws JspException {
31       XMLDocument doc = (XMLDocument)pageContext.getServletContext().getAttribute(id);
32       if(doc == null || force) {
33         parse();
34       }
35       return SKIP_BODY;
36     }
37   
38   
39     private void parse() throws JspException {
40       XMLDocument doc;
41       DOMParser docParser = new DOMParser();
42       ByteArrayInputStream stream;
43   
44       try {
45         stream = new ByteArrayInputStream(xml.getBytes());
46         docParser.setValidationMode(false);
47         docParser.parse(stream);
48         doc = docParser.getDocument();
49         pageContext.setAttribute(id, doc, PageContext.PAGE_SCOPE);
50       }
51       catch(Exception e) {
52         throw new JspException(e.getMessage());
53       }
54     }
55   
56   
57     public void release() {
58       xml = null;
59       force = false;
60     }
61   }
62   
63