KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > metadata > TestValidMetadataXML


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.metadata;
35
36 import java.io.File JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.net.URL JavaDoc;
42
43 import javax.faces.render.RenderKitFactory;
44 import javax.xml.parsers.DocumentBuilder JavaDoc;
45 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
46 import javax.xml.parsers.ParserConfigurationException JavaDoc;
47 import javax.xml.transform.Transformer JavaDoc;
48 import javax.xml.transform.TransformerException JavaDoc;
49 import javax.xml.transform.TransformerFactory JavaDoc;
50 import javax.xml.transform.stream.StreamResult JavaDoc;
51 import javax.xml.transform.stream.StreamSource JavaDoc;
52
53 import org.w3c.dom.Document JavaDoc;
54 import org.xml.sax.ErrorHandler JavaDoc;
55 import org.xml.sax.SAXException JavaDoc;
56 import org.xml.sax.SAXParseException JavaDoc;
57
58 import com.icesoft.jsfmeta.MetadataXmlParser;
59 import com.sun.rave.jsfmeta.beans.ComponentBean;
60 import com.sun.rave.jsfmeta.beans.FacesConfigBean;
61 import com.sun.rave.jsfmeta.beans.PropertyBean;
62 import com.sun.rave.jsfmeta.beans.RenderKitBean;
63 import com.sun.rave.jsfmeta.beans.RendererBean;
64
65 /*
66  * TestValidMetadataXml validates metadata.
67  *
68  */

69
70 public class TestValidMetadataXML{
71     
72     private String JavaDoc INPUT_METADATA_XML = "extended-faces-config.xml";
73     private String JavaDoc OUTPUT_METADATA_XML = "extended-faces-config-stream.xml";
74     
75     
76     public static void main(String JavaDoc[] args) {
77         
78         TestValidMetadataXML test = new TestValidMetadataXML();
79         test.setUp();
80         test.testMetadata();
81     }
82     
83     
84     private File JavaDoc getConfDir() {
85         
86         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
87         URL JavaDoc url = classLoader.getResource(".");
88         
89         File JavaDoc buildDir = new File JavaDoc( convertFileUrlToPath(url) );
90         
91         if(!buildDir.isDirectory()){
92             System.out.println("test build directory does not exist: "+buildDir);
93             System.exit(1);
94         }
95         
96         File JavaDoc confFile = new File JavaDoc(buildDir.getParent() + File.separatorChar
97                 + "classes" + File.separator + "conf");
98         
99         return confFile;
100     }
101     
102     protected void setUp(){
103         
104         File JavaDoc confFile = getConfDir();
105         boolean isConfFile = confFile.isDirectory();
106         if(!isConfFile){
107             System.out.println("no conf directory in the build directory: "+ confFile);
108             if( !confFile.mkdirs() )
109                 System.out.println("conf directory could not be created");
110         }
111         
112         String JavaDoc xsltStreamSourceString = confFile.getPath() + File.separatorChar
113                 + "xslt_conf" + File.separatorChar + "translate-conf.xsl";
114         String JavaDoc outputStreamString = confFile.getPath() + File.separatorChar
115                 + OUTPUT_METADATA_XML;
116         String JavaDoc streamSourceString = confFile.getPath() + File.separatorChar
117                 + INPUT_METADATA_XML;
118         
119         try {
120             
121             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
122             StreamSource JavaDoc xsltStreamSource = new StreamSource JavaDoc(
123                     xsltStreamSourceString);
124             Transformer JavaDoc transformer = tFactory.newTransformer(xsltStreamSource);
125             OutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(outputStreamString);
126             StreamResult JavaDoc streamResult = new StreamResult JavaDoc(outputStream);
127             StreamSource JavaDoc streamSource = new StreamSource JavaDoc(streamSourceString);
128             transformer.transform(streamSource, streamResult);
129             
130         } catch (TransformerException JavaDoc e) {
131             System.err.println("Please check the following file :\n"+streamSourceString);
132             e.printStackTrace();
133             System.exit(1);
134         } catch (FileNotFoundException JavaDoc e) {
135             System.err.println("Please check the following file :\n"+streamSourceString);
136             e.printStackTrace();
137             System.exit(1);
138         } catch (Exception JavaDoc e) {
139             System.err.println("Please check the following file :\n"+streamSourceString);
140             e.printStackTrace();
141             System.exit(1);
142         }
143     }
144     
145     public void testMetadata(){
146         
147         File JavaDoc confDir = getConfDir();
148         String JavaDoc outputStreamString = confDir.getPath() + File.separatorChar
149                 + INPUT_METADATA_XML;
150         DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory
151                 .newInstance();
152         documentBuilderFactory.setValidating(true);
153         documentBuilderFactory.setNamespaceAware(true);
154         
155         DocumentBuilder JavaDoc documentBuilder = null;
156         try {
157             documentBuilder = documentBuilderFactory.newDocumentBuilder();
158         } catch (ParserConfigurationException JavaDoc e) {
159             e.printStackTrace();
160             System.exit(1);
161         }
162         
163         documentBuilder.setErrorHandler(new ErrorHandler JavaDoc() {
164             public void error(SAXParseException JavaDoc ex) {
165                 System.err.println("Please check the following \n"+"line number="+ex.getLineNumber()+
166                         " column number= "+ ex.getColumnNumber()+
167                         "\n URL="+ex.getSystemId());
168                 ex.printStackTrace();
169                 System.exit(1);
170             }
171             
172             public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
173                 System.err.println("Please check the following \n"+"line number="+ex.getLineNumber()+
174                         " column number="+ ex.getColumnNumber()+
175                         "\n URL="+ex.getSystemId());
176                 ex.printStackTrace();
177                 System.exit(1);
178             }
179             
180             public void warning(SAXParseException JavaDoc ex) {
181                 System.err.println("Please check the following \n"+"line number="+ex.getLineNumber()+
182                         " column number="+ ex.getColumnNumber()+
183                         "\n URL="+ex.getSystemId());
184                 ex.printStackTrace();
185                 System.exit(1);
186             }
187         });
188         
189         try {
190             Document JavaDoc document = documentBuilder.parse(new File JavaDoc(outputStreamString));
191         } catch (SAXException JavaDoc se) {
192             se.printStackTrace();
193             System.exit(1);
194         } catch (IOException JavaDoc se) {
195             se.printStackTrace();
196             System.exit(1);
197         }
198     }
199     
200     /**
201      * Kind of hack-ish attempt at solving problem that if the directory,
202      * where we're building the component-metadata in, has special
203      * characters in its path, like spaces, then the URL to it will be
204      * escaped, which will be interpretted as a different directory,
205      * unless we unescape it.
206      */

207     private static String JavaDoc convertFileUrlToPath(URL JavaDoc url) {
208         
209         String JavaDoc path = url.getPath();
210         if( url.toExternalForm().startsWith("file:") ) {
211             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( path.length() );
212             int pathLength = path.length();
213             for(int i = 0; i < pathLength;) {
214                 char c = path.charAt(i);
215                 if( c == '%' ) {
216                     if( (i+1) < pathLength && isHexDigit(path.charAt(i+1)) ) {
217                         int increment = 2;
218                         if( (i+2) < pathLength && isHexDigit(path.charAt(i+2)) )
219                             increment++;
220                         try {
221                             char unescaped = (char) Integer.parseInt(
222                                     path.substring(i+1, i+increment), 16);
223                             
224                             sb.append( unescaped );
225                             i += increment;
226                             continue;
227                         } catch(NumberFormatException JavaDoc nfe) {
228                             // Not a valid hex escape, so just fall through,
229
// and append it to the path
230
}
231                     }
232                 }
233                 sb.append( c );
234                 i++;
235             }
236             path = sb.toString();
237         }
238         return path;
239     }
240     
241     private static boolean isHexDigit(char c) {
242         return ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
243     }
244 }
245
Popular Tags