KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > VariableResolver


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.io.FilterReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.DTDHandler JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.SAXNotRecognizedException JavaDoc;
39 import org.xml.sax.SAXNotSupportedException JavaDoc;
40 import org.xml.sax.XMLFilter JavaDoc;
41 import org.xml.sax.XMLReader JavaDoc;
42 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
43 import java.io.InputStreamReader JavaDoc;
44 import com.sun.org.apache.xml.internal.serializer.ToXMLStream;
45 import java.io.StringReader JavaDoc;
46 import java.io.BufferedWriter JavaDoc;
47
48 /**
49    This class provides the mechanism to resolve variables present in
50    some given {@link InputSource}.
51 */

52
53 // This class works like a standard XMLFilter, except that it parses
54
// the input source twice. On the first pass through it constructs a
55
// frame set which holds all the variable definitions. On the second
56
// pass it uses those definitions to resolve all variable references.
57
// All that this class does is to wrap up the underlying classes that
58
// provide that functionality and coordinate their activities.
59
// The kind of bugs that will be present in this class will be those
60
// that are to do with XML handling and coordination, not variable
61
// dereferencing.
62

63 // I dont think that this really should be a filter - its been
64
// implemented as if its an XMLReader, and that's how its being used.
65

66 public class VariableResolver extends XMLFilterImpl JavaDoc
67 {
68     
69     private final FrameHolderBuilder frameHolderBuilder = new FrameHolderBuilder();
70     private final VariableExpander ve = new VariableExpander();
71     
72     
73     VariableResolver() throws SAXException JavaDoc{
74         super.setParent(getXMLReader());
75     }
76
77
78       /*
79        * Parse the input source, using the given input source. Note
80        * that the input source is used twice - this is probably an
81        * error, in general. The solution is to store the first set of
82        * events and then replay them.
83        */

84
85     public void parse(InputSource JavaDoc is) throws IOException JavaDoc, SAXException JavaDoc {
86         final InputSource JavaDoc is2 = copyInputSource(is);
87         super.setContentHandler(frameHolderBuilder);
88         super.parse(is2);
89         is2.getCharacterStream().reset();
90         ve.setFramer(new Framer(frameHolderBuilder.getFrameHolder()));
91         ve.setParent(getParent());
92         ve.parse(is2);
93     }
94
95     private InputSource JavaDoc copyInputSource(InputSource JavaDoc is) throws IOException JavaDoc, SAXException JavaDoc {
96         final StringWriter JavaDoc out = new StringWriter JavaDoc();
97         final ToXMLStream xml = new ToXMLStream();
98         xml.setWriter(out);
99         final XMLReader JavaDoc r = getXMLReader();
100         r.setContentHandler(xml);
101         if (super.getEntityResolver() != null){
102             r.setEntityResolver(super.getEntityResolver());
103         }
104         r.parse(is);
105         final InputSource JavaDoc result= new InputSource JavaDoc(new StringReader JavaDoc(out.toString()){ public void close(){} });
106         result.setPublicId(is.getPublicId());
107         result.setSystemId(is.getSystemId());
108         return result;
109     }
110     
111     private XMLReader JavaDoc getXMLReader() throws SAXException JavaDoc {
112         try{
113         final XMLReader JavaDoc xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
114         xr.setFeature("http://xml.org/sax/features/namespaces", true);
115         xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
116         return xr;
117         }
118         catch (ParserConfigurationException JavaDoc e){
119             throw new SAXException JavaDoc(e);
120         }
121     }
122
123     public void setContentHandler(ContentHandler JavaDoc contentHandler) {
124         ve.setContentHandler(contentHandler);
125     }
126
127 }
128
Popular Tags