001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.metadata;
035:
036: import java.io.File;
037: import java.io.FileNotFoundException;
038: import java.io.FileOutputStream;
039: import java.io.IOException;
040: import java.io.OutputStream;
041: import java.net.URL;
042:
043: import javax.faces.render.RenderKitFactory;
044: import javax.xml.parsers.DocumentBuilder;
045: import javax.xml.parsers.DocumentBuilderFactory;
046: import javax.xml.parsers.ParserConfigurationException;
047: import javax.xml.transform.Transformer;
048: import javax.xml.transform.TransformerException;
049: import javax.xml.transform.TransformerFactory;
050: import javax.xml.transform.stream.StreamResult;
051: import javax.xml.transform.stream.StreamSource;
052:
053: import org.w3c.dom.Document;
054: import org.xml.sax.ErrorHandler;
055: import org.xml.sax.SAXException;
056: import org.xml.sax.SAXParseException;
057:
058: import com.icesoft.jsfmeta.MetadataXmlParser;
059: import com.sun.rave.jsfmeta.beans.ComponentBean;
060: import com.sun.rave.jsfmeta.beans.FacesConfigBean;
061: import com.sun.rave.jsfmeta.beans.PropertyBean;
062: import com.sun.rave.jsfmeta.beans.RenderKitBean;
063: import com.sun.rave.jsfmeta.beans.RendererBean;
064:
065: /*
066: * TestValidMetadataXml validates metadata.
067: *
068: */
069:
070: public class TestValidMetadataXML {
071:
072: private String INPUT_METADATA_XML = "extended-faces-config.xml";
073: private String OUTPUT_METADATA_XML = "extended-faces-config-stream.xml";
074:
075: public static void main(String[] args) {
076:
077: TestValidMetadataXML test = new TestValidMetadataXML();
078: test.setUp();
079: test.testMetadata();
080: }
081:
082: private File getConfDir() {
083:
084: ClassLoader classLoader = Thread.currentThread()
085: .getContextClassLoader();
086: URL url = classLoader.getResource(".");
087:
088: File buildDir = new File(convertFileUrlToPath(url));
089:
090: if (!buildDir.isDirectory()) {
091: System.out.println("test build directory does not exist: "
092: + buildDir);
093: System.exit(1);
094: }
095:
096: File confFile = new File(buildDir.getParent()
097: + File.separatorChar + "classes" + File.separator
098: + "conf");
099:
100: return confFile;
101: }
102:
103: protected void setUp() {
104:
105: File confFile = getConfDir();
106: boolean isConfFile = confFile.isDirectory();
107: if (!isConfFile) {
108: System.out
109: .println("no conf directory in the build directory: "
110: + confFile);
111: if (!confFile.mkdirs())
112: System.out
113: .println("conf directory could not be created");
114: }
115:
116: String xsltStreamSourceString = confFile.getPath()
117: + File.separatorChar + "xslt_conf" + File.separatorChar
118: + "translate-conf.xsl";
119: String outputStreamString = confFile.getPath()
120: + File.separatorChar + OUTPUT_METADATA_XML;
121: String streamSourceString = confFile.getPath()
122: + File.separatorChar + INPUT_METADATA_XML;
123:
124: try {
125:
126: TransformerFactory tFactory = TransformerFactory
127: .newInstance();
128: StreamSource xsltStreamSource = new StreamSource(
129: xsltStreamSourceString);
130: Transformer transformer = tFactory
131: .newTransformer(xsltStreamSource);
132: OutputStream outputStream = new FileOutputStream(
133: outputStreamString);
134: StreamResult streamResult = new StreamResult(outputStream);
135: StreamSource streamSource = new StreamSource(
136: streamSourceString);
137: transformer.transform(streamSource, streamResult);
138:
139: } catch (TransformerException e) {
140: System.err.println("Please check the following file :\n"
141: + streamSourceString);
142: e.printStackTrace();
143: System.exit(1);
144: } catch (FileNotFoundException e) {
145: System.err.println("Please check the following file :\n"
146: + streamSourceString);
147: e.printStackTrace();
148: System.exit(1);
149: } catch (Exception e) {
150: System.err.println("Please check the following file :\n"
151: + streamSourceString);
152: e.printStackTrace();
153: System.exit(1);
154: }
155: }
156:
157: public void testMetadata() {
158:
159: File confDir = getConfDir();
160: String outputStreamString = confDir.getPath()
161: + File.separatorChar + INPUT_METADATA_XML;
162: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
163: .newInstance();
164: documentBuilderFactory.setValidating(true);
165: documentBuilderFactory.setNamespaceAware(true);
166:
167: DocumentBuilder documentBuilder = null;
168: try {
169: documentBuilder = documentBuilderFactory
170: .newDocumentBuilder();
171: } catch (ParserConfigurationException e) {
172: e.printStackTrace();
173: System.exit(1);
174: }
175:
176: documentBuilder.setErrorHandler(new ErrorHandler() {
177: public void error(SAXParseException ex) {
178: System.err.println("Please check the following \n"
179: + "line number=" + ex.getLineNumber()
180: + " column number= " + ex.getColumnNumber()
181: + "\n URL=" + ex.getSystemId());
182: ex.printStackTrace();
183: System.exit(1);
184: }
185:
186: public void fatalError(SAXParseException ex)
187: throws SAXException {
188: System.err.println("Please check the following \n"
189: + "line number=" + ex.getLineNumber()
190: + " column number=" + ex.getColumnNumber()
191: + "\n URL=" + ex.getSystemId());
192: ex.printStackTrace();
193: System.exit(1);
194: }
195:
196: public void warning(SAXParseException ex) {
197: System.err.println("Please check the following \n"
198: + "line number=" + ex.getLineNumber()
199: + " column number=" + ex.getColumnNumber()
200: + "\n URL=" + ex.getSystemId());
201: ex.printStackTrace();
202: System.exit(1);
203: }
204: });
205:
206: try {
207: Document document = documentBuilder.parse(new File(
208: outputStreamString));
209: } catch (SAXException se) {
210: se.printStackTrace();
211: System.exit(1);
212: } catch (IOException se) {
213: se.printStackTrace();
214: System.exit(1);
215: }
216: }
217:
218: /**
219: * Kind of hack-ish attempt at solving problem that if the directory,
220: * where we're building the component-metadata in, has special
221: * characters in its path, like spaces, then the URL to it will be
222: * escaped, which will be interpretted as a different directory,
223: * unless we unescape it.
224: */
225: private static String convertFileUrlToPath(URL url) {
226:
227: String path = url.getPath();
228: if (url.toExternalForm().startsWith("file:")) {
229: StringBuffer sb = new StringBuffer(path.length());
230: int pathLength = path.length();
231: for (int i = 0; i < pathLength;) {
232: char c = path.charAt(i);
233: if (c == '%') {
234: if ((i + 1) < pathLength
235: && isHexDigit(path.charAt(i + 1))) {
236: int increment = 2;
237: if ((i + 2) < pathLength
238: && isHexDigit(path.charAt(i + 2)))
239: increment++;
240: try {
241: char unescaped = (char) Integer.parseInt(
242: path
243: .substring(i + 1, i
244: + increment), 16);
245:
246: sb.append(unescaped);
247: i += increment;
248: continue;
249: } catch (NumberFormatException nfe) {
250: // Not a valid hex escape, so just fall through,
251: // and append it to the path
252: }
253: }
254: }
255: sb.append(c);
256: i++;
257: }
258: path = sb.toString();
259: }
260: return path;
261: }
262:
263: private static boolean isHexDigit(char c) {
264: return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
265: }
266: }
|