001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.jasper.compiler;
043:
044: import java.lang.reflect.Field;
045: import java.util.HashMap;
046: import java.util.Map;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import org.apache.jasper.JasperException;
050: import org.apache.jasper.JspCompilationContext;
051:
052: /** Reflection stuff for org.apache.jasper.compiler.Compiler.
053: *
054: * @author Petr Jiricka
055: */
056: public class CompilerHacks {
057:
058: private static final Logger LOGGER = Logger
059: .getLogger(CompilerHacks.class.getName());
060:
061: // @GuardedBy(this)
062: private Compiler comp;
063: private final JspCompilationContext ctxt;
064:
065: private static Field pageInfoF;
066: private static Field errDispatcherF;
067:
068: static {
069: initMethodsAndFields();
070: }
071:
072: /** Creates a new instance of CompilerHacks */
073: public CompilerHacks(JspCompilationContext ctxt) {
074: this .ctxt = ctxt;
075: }
076:
077: static void initMethodsAndFields() {
078: try {
079: // pageInfo field
080: pageInfoF = Compiler.class.getDeclaredField("pageInfo"); // NOI18N
081: pageInfoF.setAccessible(true);
082: // errDispatcher field
083: errDispatcherF = Compiler.class
084: .getDeclaredField("errDispatcher"); // NOI18N
085: errDispatcherF.setAccessible(true);
086: } catch (NoSuchFieldException e) {
087: LOGGER.log(Level.INFO, null, e);
088: }
089: }
090:
091: private synchronized void setupCompiler() throws JasperException {
092: if (comp == null) {
093: comp = ctxt.createParser();
094: setErrDispatcherInCompiler(comp, new ErrorDispatcher(false));
095: setPageInfoInCompiler(comp, new HackPageInfo(
096: new BeanRepository(ctxt.getClassLoader(), comp
097: .getErrorDispatcher()), ctxt.getJspFile()));
098: }
099: }
100:
101: Compiler getCompiler() throws JasperException {
102: setupCompiler();
103: return comp;
104: }
105:
106: private static void setPageInfoInCompiler(Compiler c,
107: PageInfo pageInfo) throws JasperException {
108: try {
109: pageInfoF.set(c, pageInfo);
110: } catch (IllegalAccessException e) {
111: throw new JasperException(e);
112: }
113: }
114:
115: private static void setErrDispatcherInCompiler(Compiler c,
116: ErrorDispatcher errDispatcher) throws JasperException {
117: try {
118: errDispatcherF.set(c, errDispatcher);
119: } catch (IllegalAccessException e) {
120: throw new JasperException(e);
121: }
122: }
123:
124: /** Hacked PageInfo to get better XML directive data
125: */
126: static final class HackPageInfo extends PageInfo {
127:
128: /** Map of prefix -> uri. */
129: private final Map<String, String> approxXmlPrefixMapper = new HashMap<String, String>();
130:
131: HackPageInfo(BeanRepository beanRepository, String jspFile) {
132: super (beanRepository, jspFile);
133: }
134:
135: @Override
136: public void pushPrefixMapping(String prefix, String uri) {
137: super .pushPrefixMapping(prefix, uri);
138: if (uri != null) {
139: synchronized (approxXmlPrefixMapper) {
140: approxXmlPrefixMapper.put(prefix, uri);
141: }
142: }
143: }
144:
145: Map<String, String> getApproxXmlPrefixMapper() {
146: synchronized (approxXmlPrefixMapper) {
147: return approxXmlPrefixMapper;
148: }
149: }
150: }
151: }
|