KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > ParserControllerProxy


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.jasper.compiler;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29 import org.apache.jasper.JasperException;
30 import org.apache.jasper.JspCompilationContext;
31 import org.apache.jasper.compiler.Compiler;
32 import org.openide.ErrorManager;
33
34 /**
35  *
36  * @author Petr Jiricka
37  */

38 public class ParserControllerProxy {
39
40     private JspCompilationContext ctxt;
41     private Compiler JavaDoc compiler;
42     private ParserController pc;
43     
44     boolean isXml;
45     String JavaDoc sourceEnc;
46     
47     private static Method JavaDoc getJarFileM;
48     private static Method JavaDoc resolveFileNameM;
49     private static Method JavaDoc getJspConfigPageEncodingM;
50     private static Method JavaDoc determineSyntaxAndEncodingM;
51     private static Field JavaDoc isXmlF;
52     private static Field JavaDoc sourceEncF;
53     
54     static {
55         initMethodsAndFields();
56     }
57     
58     /** Creates a new instance of ParserControllerProxy */
59     public ParserControllerProxy(JspCompilationContext ctxt, Compiler JavaDoc compiler) {
60         this.ctxt = ctxt;
61     this.compiler = compiler;
62         pc = new ParserController(ctxt, compiler);
63     }
64     
65     public static void initMethodsAndFields() {
66         try {
67             // getJarFile method
68
getJarFileM = ParserController.class.getDeclaredMethod("getJarFile", new Class JavaDoc[] {URL JavaDoc.class});
69             getJarFileM.setAccessible(true);
70             // resolveFileName method
71
resolveFileNameM = ParserController.class.getDeclaredMethod("resolveFileName", new Class JavaDoc[] {String JavaDoc.class});
72             resolveFileNameM.setAccessible(true);
73             // getJspConfigPageEncoding method
74
getJspConfigPageEncodingM = ParserController.class.getDeclaredMethod("getJspConfigPageEncoding", new Class JavaDoc[] {String JavaDoc.class});
75             getJspConfigPageEncodingM.setAccessible(true);
76             // determineSyntaxAndEncoding method
77
determineSyntaxAndEncodingM = ParserController.class.getDeclaredMethod("determineSyntaxAndEncoding", new Class JavaDoc[]
78                 {String JavaDoc.class, JarFile JavaDoc.class, String JavaDoc.class});
79             determineSyntaxAndEncodingM.setAccessible(true);
80             // isXML field
81
isXmlF = ParserController.class.getDeclaredField("isXml");
82             isXmlF.setAccessible(true);
83             // sourceEnc field
84
sourceEncF = ParserController.class.getDeclaredField("sourceEnc");
85             sourceEncF.setAccessible(true);
86         }
87         catch (NoSuchMethodException JavaDoc e) {
88             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
89         }
90         catch (NoSuchFieldException JavaDoc e) {
91             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
92         }
93     }
94     
95     public void extractSyntaxAndEncoding(String JavaDoc inFileName)
96     throws FileNotFoundException JavaDoc, JasperException, IOException JavaDoc {
97     // If we're parsing a packaged tag file or a resource included by it
98
// (using an include directive), ctxt.getTagFileJar() returns the
99
// JAR file from which to read the tag file or included resource,
100
// respectively.
101
extractSyntaxAndEncoding(inFileName, ctxt.getTagFileJarUrl());
102     }
103     
104     private void extractSyntaxAndEncoding(String JavaDoc inFileName, URL JavaDoc jarFileUrl)
105         throws FileNotFoundException JavaDoc, JasperException, IOException JavaDoc {
106         try {
107             JarFile JavaDoc jarFile = (JarFile JavaDoc)getJarFileM.invoke(pc, new Object JavaDoc[] {jarFileUrl});
108             String JavaDoc absFileName = (String JavaDoc)resolveFileNameM.invoke(pc, new Object JavaDoc[] {inFileName});
109             String JavaDoc jspConfigPageEnc = (String JavaDoc)getJspConfigPageEncodingM.invoke(pc, new Object JavaDoc[] {absFileName});
110
111             // Figure out what type of JSP document and encoding type we are
112
// dealing with
113
determineSyntaxAndEncodingM.invoke(pc, new Object JavaDoc[] {absFileName, jarFile, jspConfigPageEnc});
114             
115             // now the isXml and sourceEnc variables of ParserController have values
116
isXml = ((Boolean JavaDoc)isXmlF.get(pc)).booleanValue();
117             sourceEnc = (String JavaDoc)sourceEncF.get(pc);
118         }
119         catch (IllegalAccessException JavaDoc e) {
120             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
121             throw new JasperException(e);
122         }
123         catch (InvocationTargetException JavaDoc e) {
124             Throwable JavaDoc r = e.getTargetException();
125             if (r instanceof RuntimeException JavaDoc) {
126                 throw (RuntimeException JavaDoc)r;
127             }
128             if (r instanceof FileNotFoundException JavaDoc) {
129                 throw (FileNotFoundException JavaDoc)r;
130             }
131             if (r instanceof JasperException) {
132                 throw (JasperException)r;
133             }
134             if (r instanceof IOException JavaDoc) {
135                 throw (IOException JavaDoc)r;
136             }
137             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
138             throw new JasperException(e);
139         }
140     }
141     
142 }
143
Popular Tags