KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > grammar > ValidXmlFileType


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.io.File JavaDoc;
35
36
37 public class ValidXmlFileType
38         extends WebappPathType
39 {
40     private SchemaType _schema;
41     private static Map JavaDoc _parseResults = Collections.synchronizedMap( new HashMap JavaDoc() );
42     
43     public ValidXmlFileType( SchemaType schema, String JavaDoc 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 JavaDoc file, AnnotationValue value )
76     {
77         try
78         {
79             //
80
// We cache the results of parsing the file until the file is actually modified,
81
// so we don't end up continually re-parsing it.
82
//
83
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 JavaDoc errorListener = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc e )
128         {
129             addError( value, "error.xml-read-error", new Object JavaDoc[]{ 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 JavaDoc[] args =
138                     {
139                         err.getSourceName(),
140                         new Integer JavaDoc( err.getLine() ),
141                         new Integer JavaDoc( 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 JavaDoc[] args =
150             {
151                 err.getSourceName(),
152                 new Integer JavaDoc( err.getLine() ),
153                 err.getMessage()
154             };
155                 
156             addError( value, "error.xml-parse-error-nocolumn", args );
157         }
158         else
159         {
160             Object JavaDoc[] 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 JavaDoc _exception;
174
175         public ParseResults( long fileModTime, Exception JavaDoc 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 JavaDoc getException()
192         {
193             return _exception;
194         }
195
196         public void setException( Exception JavaDoc exception )
197         {
198             _exception = exception;
199         }
200     }
201 }
202
Popular Tags