1 33 34 package com.icesoft.faces.metadata; 35 36 import java.io.File ; 37 import java.io.FileNotFoundException ; 38 import java.io.FileOutputStream ; 39 import java.io.IOException ; 40 import java.io.OutputStream ; 41 import java.net.URL ; 42 43 import javax.faces.render.RenderKitFactory; 44 import javax.xml.parsers.DocumentBuilder ; 45 import javax.xml.parsers.DocumentBuilderFactory ; 46 import javax.xml.parsers.ParserConfigurationException ; 47 import javax.xml.transform.Transformer ; 48 import javax.xml.transform.TransformerException ; 49 import javax.xml.transform.TransformerFactory ; 50 import javax.xml.transform.stream.StreamResult ; 51 import javax.xml.transform.stream.StreamSource ; 52 53 import org.w3c.dom.Document ; 54 import org.xml.sax.ErrorHandler ; 55 import org.xml.sax.SAXException ; 56 import org.xml.sax.SAXParseException ; 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 69 70 public class TestValidMetadataXML{ 71 72 private String INPUT_METADATA_XML = "extended-faces-config.xml"; 73 private String OUTPUT_METADATA_XML = "extended-faces-config-stream.xml"; 74 75 76 public static void main(String [] args) { 77 78 TestValidMetadataXML test = new TestValidMetadataXML(); 79 test.setUp(); 80 test.testMetadata(); 81 } 82 83 84 private File getConfDir() { 85 86 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 87 URL url = classLoader.getResource("."); 88 89 File buildDir = new File ( 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 confFile = new File (buildDir.getParent() + File.separatorChar 97 + "classes" + File.separator + "conf"); 98 99 return confFile; 100 } 101 102 protected void setUp(){ 103 104 File 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 xsltStreamSourceString = confFile.getPath() + File.separatorChar 113 + "xslt_conf" + File.separatorChar + "translate-conf.xsl"; 114 String outputStreamString = confFile.getPath() + File.separatorChar 115 + OUTPUT_METADATA_XML; 116 String streamSourceString = confFile.getPath() + File.separatorChar 117 + INPUT_METADATA_XML; 118 119 try { 120 121 TransformerFactory tFactory = TransformerFactory.newInstance(); 122 StreamSource xsltStreamSource = new StreamSource ( 123 xsltStreamSourceString); 124 Transformer transformer = tFactory.newTransformer(xsltStreamSource); 125 OutputStream outputStream = new FileOutputStream (outputStreamString); 126 StreamResult streamResult = new StreamResult (outputStream); 127 StreamSource streamSource = new StreamSource (streamSourceString); 128 transformer.transform(streamSource, streamResult); 129 130 } catch (TransformerException e) { 131 System.err.println("Please check the following file :\n"+streamSourceString); 132 e.printStackTrace(); 133 System.exit(1); 134 } catch (FileNotFoundException e) { 135 System.err.println("Please check the following file :\n"+streamSourceString); 136 e.printStackTrace(); 137 System.exit(1); 138 } catch (Exception 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 confDir = getConfDir(); 148 String outputStreamString = confDir.getPath() + File.separatorChar 149 + INPUT_METADATA_XML; 150 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory 151 .newInstance(); 152 documentBuilderFactory.setValidating(true); 153 documentBuilderFactory.setNamespaceAware(true); 154 155 DocumentBuilder documentBuilder = null; 156 try { 157 documentBuilder = documentBuilderFactory.newDocumentBuilder(); 158 } catch (ParserConfigurationException e) { 159 e.printStackTrace(); 160 System.exit(1); 161 } 162 163 documentBuilder.setErrorHandler(new ErrorHandler () { 164 public void error(SAXParseException 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 ex) throws SAXException { 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 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 document = documentBuilder.parse(new File (outputStreamString)); 191 } catch (SAXException se) { 192 se.printStackTrace(); 193 System.exit(1); 194 } catch (IOException se) { 195 se.printStackTrace(); 196 System.exit(1); 197 } 198 } 199 200 207 private static String convertFileUrlToPath(URL url) { 208 209 String path = url.getPath(); 210 if( url.toExternalForm().startsWith("file:") ) { 211 StringBuffer sb = new StringBuffer ( 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 nfe) { 228 } 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 |