KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
27 import org.xml.sax.helpers.DefaultHandler JavaDoc;
28 import com.sun.enterprise.util.LocalStringManager;
29 import java.util.ArrayList JavaDoc;
30 import java.io.IOException JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.Attributes JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 class Localiser extends DefaultHandler JavaDoc
36 {
37     private final LocalStringManager lsm;
38     private final Writer JavaDoc out;
39     private final Logger JavaDoc logger;
40     private final String JavaDoc prefix;
41     
42
43     Localiser(final LocalStringManager lsm, final Writer JavaDoc out, final String JavaDoc prefix){
44         this(lsm, out, null, prefix);
45     }
46
47     Localiser(final LocalStringManager lsm, final Writer JavaDoc out, final Logger JavaDoc logger){
48         this(lsm, out, logger, null);
49     }
50
51     Localiser(final LocalStringManager lsm, final Writer JavaDoc out){
52         this(lsm, out, null, null);
53     }
54     
55     Localiser(final LocalStringManager lsm, final Writer JavaDoc out, final Logger JavaDoc logger, final String JavaDoc prefix){
56         this.lsm = lsm;
57         this.out = out;
58         this.logger = logger;
59         this.prefix = (prefix == null ? "" : prefix +".");
60     }
61
62     public void startElement(final String JavaDoc namespaceURI,
63                          final String JavaDoc localName,
64                          final String JavaDoc qName,
65                          final Attributes JavaDoc atts)
66         throws SAXException JavaDoc {
67         if (localName.equals("location")){
68             startLocation();
69         } else if (localName.equals("message")){
70             startMessage(atts);
71         } else if (localName.equals("param")) {
72             startParam(atts);
73         }
74     }
75     public void characters(char[] ch,
76                        int start,
77                        int length)
78         throws SAXException JavaDoc{
79         if (null != textBuffer){
80             textBuffer.append(ch, start, length);
81         }
82     }
83
84     public void endElement(String JavaDoc namespaceURI,
85                            String JavaDoc localName,
86                            String JavaDoc qName)
87         throws SAXException JavaDoc{
88         if (localName.equals("location")){
89             endLocation();
90         } else if (localName.equals("message")){
91             endMessage();
92         } else if (localName.equals("param")) {
93             endParam();
94         }
95     }
96
97     private String JavaDoc message_id;
98     private ArrayList JavaDoc params = new ArrayList JavaDoc(5);
99     private StringBuffer JavaDoc textBuffer;
100
101     private void startLocation(){
102         textBuffer = new StringBuffer JavaDoc();
103     }
104
105     private void endLocation() throws SAXException JavaDoc {
106         if (null != textBuffer && textBuffer.length() > 0){
107             try {
108                 textBuffer.append(" ");
109                 out.write(textBuffer.toString());
110             }
111             catch (IOException JavaDoc e){
112                 throw new SAXException JavaDoc(e);
113             }
114         }
115         textBuffer = null;
116     }
117     
118     private void startMessage(final Attributes JavaDoc attr) throws SAXException JavaDoc {
119         params.clear();
120         final String JavaDoc id = attr.getValue("", "id");
121         if (null == id || id.length() == 0){
122             throw new SAXException JavaDoc("id attribute is either null or empty");
123         }
124         message_id = prefix + id;
125     }
126
127     private void startParam(final Attributes JavaDoc attr) {
128         textBuffer = new StringBuffer JavaDoc();
129     }
130     
131     private void endParam(){
132         params.add(textBuffer.toString());
133         textBuffer = null;
134     }
135
136     private void endMessage() throws SAXException JavaDoc {
137         final String JavaDoc msg = lsm.getLocalString(null, message_id,
138                            "Internal Error, message id \""+message_id+ "\" not present in localisation file",
139                            params.toArray());
140         message_id = null;
141         
142         if (null != logger && msg.startsWith("Internal Error")){
143             logger.severe(msg);
144             return;
145         }
146         
147         try {
148             out.write(msg+"\n");
149             out.flush();
150         }
151         catch (IOException JavaDoc e){
152             throw new SAXException JavaDoc(e);
153         }
154         
155             
156     }
157         
158 }
159
Popular Tags