KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Stack JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.Locator JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
32 /**
33    This class provides the basic mechanism for constructing Frames in
34    which variable definitions can be recorded, and then used to
35    dereference variable references.
36
37    The idea is that it manages a collection of frames, held in a
38    frameholder. (The frameholder can either be provided by clients, or
39    a default one is provided). As SAX events come in frames are added
40    to this frameholder, in the appropriate place.
41
42 */

43 // This class only knows that frames begin and end on certain start
44
// and end elements, and that system property elements contain a name
45
// value pair that should be added to the current frame.
46
public class Framer extends DefaultHandler JavaDoc
47 {
48     
49     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
50         if (isSystemPropertyElement(localName)){
51             handleSystemPropertyEvent(atts);
52         } else if (isConfigEvent(localName)) {
53             handleStartConfigEvent(atts);
54         } else if (isServerEvent(localName)) {
55             handleStartServerEvent(atts);
56         } else if (isClusterEvent(localName)) {
57             handleStartClusterEvent(atts);
58         } else if (isServerRefEvent(localName)) {
59             handleStartServerRefEvent(atts);
60         }
61         super.startElement(namespaceURI, localName, qName, atts);
62     }
63
64     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
65         if (isConfigEvent(localName) || isServerEvent(localName) || isClusterEvent(localName)){
66             frameStack.pop();
67         }
68         super.endElement(namespaceURI, localName, qName);
69     }
70
71
72     private boolean isClusterEvent(String JavaDoc n){
73         return n.equals(CLUSTER);
74     }
75
76     private boolean isConfigEvent(String JavaDoc n){
77         return n.equals(CONFIG);
78     }
79     private boolean isServerEvent(String JavaDoc n){
80         return n.equals(SERVER);
81     }
82     private boolean isServerRefEvent(String JavaDoc n){
83         return n.equals(SERVER_REF);
84     }
85     private boolean isSystemPropertyElement(String JavaDoc n){
86         return n.equals(SYSTEM_PROPERTY);
87     }
88
89     private void handleStartClusterEvent(Attributes JavaDoc atts){
90         frameStack.push(getClusterFrame(atts));
91     }
92
93     private void handleStartConfigEvent(Attributes JavaDoc atts){
94         frameStack.push(getConfigFrame(atts));
95     }
96
97     private void handleStartServerEvent(Attributes JavaDoc atts){
98         frameStack.push(getServerFrame(atts));
99     }
100     protected void handleStartServerRefEvent(Attributes JavaDoc atts){}
101
102     private void handleSystemPropertyEvent(Attributes JavaDoc atts){
103         currentFrame().put(atts.getValue(NAMESPACE, NAME), atts.getValue(NAMESPACE, VALUE));
104     }
105
106     protected Frame getClusterFrame(Attributes JavaDoc atts){
107         return frameHolder.getClusterFrame(getFrameName(atts));
108     }
109     
110     protected Frame getConfigFrame(Attributes JavaDoc atts){
111         return frameHolder.getConfigFrame(getFrameName(atts));
112     }
113     
114     protected Frame getServerFrame(Attributes JavaDoc atts){
115         return frameHolder.getServerFrame(getFrameName(atts));
116     }
117
118
119     private String JavaDoc getFrameName(Attributes JavaDoc atts){
120         return atts.getValue(NAMESPACE, NAME);
121     }
122     
123     Framer(){
124         this(new FrameHolder());
125     }
126
127     Framer(FrameHolder fh){
128         frameHolder = fh;
129         frameStack.push(fh.getDomainFrame());
130     }
131     
132
133     FrameHolder getFrameHolder(){
134         return frameHolder;
135     }
136
137     Frame currentFrame(){
138         return (Frame) frameStack.peek();
139     }
140
141
142     private Stack JavaDoc frameStack = new Stack JavaDoc();
143     protected FrameHolder frameHolder = new FrameHolder();
144
145     public static final String JavaDoc CLUSTER = "cluster";
146     public static final String JavaDoc CONFIG = "config";
147     public static final String JavaDoc CONFIG_REF = "config-ref";
148     public static final String JavaDoc NAME = "name";
149     public static final String JavaDoc NAMESPACE = "";
150     public static final String JavaDoc SERVER = "server";
151     public static final String JavaDoc SERVER_REF = "server-ref";
152     public static final String JavaDoc SYSTEM_PROPERTY = "system-property";
153     public static final String JavaDoc VALUE = "value";
154
155 }
156
Popular Tags