1 18 package org.apache.beehive.netui.compiler.grammar; 19 20 import org.apache.beehive.netui.compiler.AnnotationGrammar; 21 import org.apache.beehive.netui.compiler.FlowControllerInfo; 22 import org.apache.xmlbeans.SchemaType; 23 import org.apache.xmlbeans.XmlOptions; 24 import org.apache.xmlbeans.XmlObject; 25 import org.apache.xmlbeans.XmlError; 26 import org.apache.xmlbeans.XmlException; 27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue; 28 29 import java.util.Map ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.io.File ; 35 36 37 public class ValidXmlFileType 38 extends WebappPathType 39 { 40 private SchemaType _schema; 41 private static Map _parseResults = Collections.synchronizedMap( new HashMap () ); 42 43 public ValidXmlFileType( SchemaType schema, String requiredRuntimeVersion, AnnotationGrammar parentGrammar, 44 FlowControllerInfo fcInfo ) 45 { 46 super( false, requiredRuntimeVersion, parentGrammar, fcInfo ); 47 _schema = schema; 48 } 49 50 51 protected boolean checkAnyExtension() 52 { 53 return true; 54 } 55 56 57 protected boolean doFatalError() 58 { 59 return true; 60 } 61 62 63 protected boolean ignoreDirectories() 64 { 65 return false; 66 } 67 68 69 protected boolean allowFileInPageFlowSourceDir() 70 { 71 return true; 72 } 73 74 75 protected void runAdditionalChecks( File file, AnnotationValue value ) 76 { 77 try 78 { 79 ParseResults prevResults = ( ParseResults ) _parseResults.get( file.getPath() ); 84 85 if ( prevResults == null || file.lastModified() > prevResults.getFileModTime() ) 86 { 87 try 88 { 89 XmlOptions options = new XmlOptions(); 90 options.setDocumentType( _schema ); 91 XmlObject xml = XmlObject.Factory.parse( file, options ); 92 List errorListener = new ArrayList (); 93 options.setErrorListener( errorListener ); 94 95 if ( !xml.validate( options ) ) 96 { 97 assert !errorListener.isEmpty(); 98 99 XmlError err = ( XmlError ) errorListener.get( 0 ); 100 assert err != null; 101 102 throw new XmlException( err.getMessage(), null, err ); 103 } 104 } 105 catch ( Exception e ) 106 { 107 _parseResults.put( file.getPath(), new ParseResults( file.lastModified(), e ) ); 108 throw e; 109 } 110 111 _parseResults.put( file.getPath(), new ParseResults( file.lastModified(), null ) ); 112 } 113 else 114 { 115 Exception e = prevResults.getException(); 116 117 if ( e != null ) 118 { 119 throw e; 120 } 121 } 122 } 123 catch ( XmlException e ) 124 { 125 addErrorDiagnostic( e.getError(), value ); 126 } 127 catch ( Exception e ) 128 { 129 addError( value, "error.xml-read-error", new Object []{ file.getPath(), e.getMessage() } ); 130 } 131 } 132 133 private void addErrorDiagnostic( XmlError err, AnnotationValue value ) 134 { 135 if ( err.getColumn() != -1 && err.getLine() != -1 ) 136 { 137 Object [] args = 138 { 139 err.getSourceName(), 140 new Integer ( err.getLine() ), 141 new Integer ( err.getColumn() ), 142 err.getMessage() 143 }; 144 145 addError( value, "error.xml-parse-error", args ); 146 } 147 else if ( err.getLine() != -1 ) 148 { 149 Object [] args = 150 { 151 err.getSourceName(), 152 new Integer ( err.getLine() ), 153 err.getMessage() 154 }; 155 156 addError( value, "error.xml-parse-error-nocolumn", args ); 157 } 158 else 159 { 160 Object [] args = 161 { 162 err.getSourceName(), 163 err.getMessage() 164 }; 165 166 addError( value, "error.xml-parse-error-nolinecolumn", args ); 167 } 168 } 169 170 private static class ParseResults 171 { 172 private long _fileModTime; 173 private Exception _exception; 174 175 public ParseResults( long fileModTime, Exception exception ) 176 { 177 _fileModTime = fileModTime; 178 _exception = exception; 179 } 180 181 public long getFileModTime() 182 { 183 return _fileModTime; 184 } 185 186 public void setFileModTime( long fileModTime ) 187 { 188 _fileModTime = fileModTime; 189 } 190 191 public Exception getException() 192 { 193 return _exception; 194 } 195 196 public void setException( Exception exception ) 197 { 198 _exception = exception; 199 } 200 } 201 } 202 | Popular Tags |