KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Field JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.apache.jasper.JasperException;
26 import org.apache.jasper.JspCompilationContext;
27 import org.openide.ErrorManager;
28
29 /** Reflection stuff for org.apache.jasper.compiler.Compiler.
30  *
31  * @author Petr Jiricka
32  */

33 public class CompilerHacks {
34
35     private Compiler JavaDoc comp;
36     protected JspCompilationContext ctxt;
37
38     private static Field JavaDoc pageInfoF;
39     private static Field JavaDoc errDispatcherF;
40
41     static {
42         initMethodsAndFields();
43     }
44     
45     /** Creates a new instance of CompilerHacks */
46     public CompilerHacks(JspCompilationContext ctxt) {
47         this.ctxt = ctxt;
48     }
49
50     static void initMethodsAndFields() {
51         try {
52             // pageInfo field
53
pageInfoF = Compiler JavaDoc.class.getDeclaredField("pageInfo");
54             pageInfoF.setAccessible(true);
55             // errDispatcher field
56
errDispatcherF = Compiler JavaDoc.class.getDeclaredField("errDispatcher");
57             errDispatcherF.setAccessible(true);
58         }
59         catch (NoSuchFieldException JavaDoc e) {
60             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
61         }
62     }
63     
64     private void setupCompiler() throws JasperException {
65         if (comp == null) {
66             comp = ctxt.createCompiler();
67             setErrDispatcherInCompiler(comp, new ErrorDispatcher(false));
68             setPageInfoInCompiler(comp, new HackPageInfo(new BeanRepository(
69                 ctxt.getClassLoader(), comp.getErrorDispatcher()), ""));
70         }
71     }
72     
73     Compiler JavaDoc getCompiler() throws JasperException {
74         setupCompiler();
75         return comp;
76     }
77     
78     private static void setPageInfoInCompiler(Compiler JavaDoc c, PageInfo pageInfo) throws JasperException {
79         try {
80             pageInfoF.set(c, pageInfo);
81         }
82         catch (IllegalAccessException JavaDoc e) {
83             throw new JasperException(e);
84         }
85     }
86     
87     private static void setErrDispatcherInCompiler(Compiler JavaDoc c, ErrorDispatcher errDispatcher) throws JasperException {
88         try {
89             errDispatcherF.set(c, errDispatcher);
90         }
91         catch (IllegalAccessException JavaDoc e) {
92             throw new JasperException(e);
93         }
94     }
95     
96     /** Hacked PageInfo to get better XML directive data
97      */

98     class HackPageInfo extends PageInfo {
99
100         /** Map of prefix -> uri. */
101         private Map JavaDoc approxXmlPrefixMapper;
102         
103         HackPageInfo(BeanRepository beanRepository, String JavaDoc jspFile) {
104             super(beanRepository, jspFile);
105             approxXmlPrefixMapper = new HashMap JavaDoc();
106         }
107         
108         public void pushPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
109             super.pushPrefixMapping(prefix, uri);
110             if (uri != null) {
111                 approxXmlPrefixMapper.put(prefix, uri);
112             }
113         }
114         
115         Map JavaDoc getApproxXmlPrefixMapper() {
116             return approxXmlPrefixMapper;
117         }
118     }
119     
120 }
121
Popular Tags