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-2006 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.io.FileNotFoundException;
045: import java.io.IOException;
046: import java.lang.reflect.Field;
047: import java.lang.reflect.InvocationTargetException;
048: import java.lang.reflect.Method;
049: import java.net.URL;
050: import java.util.jar.JarFile;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import org.apache.jasper.JasperException;
054: import org.apache.jasper.JspCompilationContext;
055:
056: /**
057: *
058: * @author Petr Jiricka
059: */
060: public class ParserControllerProxy {
061:
062: private static final Logger LOGGER = Logger
063: .getLogger(ParserControllerProxy.class.getName());
064:
065: private final JspCompilationContext ctxt;
066: private final Compiler compiler;
067: private final ParserController pc;
068:
069: boolean isXml;
070: String sourceEnc;
071:
072: private static Method getJarFileM;
073: private static Method resolveFileNameM;
074: private static Method getJspConfigPageEncodingM;
075: private static Method determineSyntaxAndEncodingM;
076: private static Field isXmlF;
077: private static Field sourceEncF;
078:
079: static {
080: initMethodsAndFields();
081: }
082:
083: /** Creates a new instance of ParserControllerProxy */
084: public ParserControllerProxy(JspCompilationContext ctxt,
085: Compiler compiler) {
086: this .ctxt = ctxt;
087: this .compiler = compiler;
088: pc = new ParserController(ctxt, compiler);
089: }
090:
091: public static void initMethodsAndFields() {
092: try {
093: // getJarFile method
094: getJarFileM = ParserController.class.getDeclaredMethod(
095: "getJarFile", new Class[] { URL.class }); // NOI18N
096: getJarFileM.setAccessible(true);
097: // resolveFileName method
098: resolveFileNameM = ParserController.class
099: .getDeclaredMethod("resolveFileName",
100: new Class[] { String.class }); // NOI18N
101: resolveFileNameM.setAccessible(true);
102: // getJspConfigPageEncoding method
103: getJspConfigPageEncodingM = ParserController.class
104: .getDeclaredMethod("getJspConfigPageEncoding",
105: new Class[] { String.class }); // NOI18N
106: getJspConfigPageEncodingM.setAccessible(true);
107: // determineSyntaxAndEncoding method
108: determineSyntaxAndEncodingM = ParserController.class
109: .getDeclaredMethod("determineSyntaxAndEncoding",
110: new Class[] // NOI18N
111: { String.class, JarFile.class, String.class });
112: determineSyntaxAndEncodingM.setAccessible(true);
113: // isXML field
114: isXmlF = ParserController.class.getDeclaredField("isXml"); // NOI18N
115: isXmlF.setAccessible(true);
116: // sourceEnc field
117: sourceEncF = ParserController.class
118: .getDeclaredField("sourceEnc"); // NOI18N
119: sourceEncF.setAccessible(true);
120: } catch (NoSuchMethodException e) {
121: LOGGER.log(Level.INFO, null, e);
122: } catch (NoSuchFieldException e) {
123: LOGGER.log(Level.INFO, null, e);
124: }
125: }
126:
127: public void extractSyntaxAndEncoding(String inFileName)
128: throws FileNotFoundException, JasperException, IOException {
129: // If we're parsing a packaged tag file or a resource included by it
130: // (using an include directive), ctxt.getTagFileJar() returns the
131: // JAR file from which to read the tag file or included resource,
132: // respectively.
133: extractSyntaxAndEncoding(inFileName, ctxt.getTagFileJarUrl());
134: }
135:
136: private void extractSyntaxAndEncoding(String inFileName,
137: URL jarFileUrl) throws FileNotFoundException,
138: JasperException, IOException {
139: try {
140: JarFile jarFile = (JarFile) getJarFileM.invoke(pc,
141: new Object[] { jarFileUrl });
142: String absFileName = (String) resolveFileNameM.invoke(pc,
143: new Object[] { inFileName });
144: String jspConfigPageEnc = (String) getJspConfigPageEncodingM
145: .invoke(pc, new Object[] { absFileName });
146:
147: // Figure out what type of JSP document and encoding type we are
148: // dealing with
149: determineSyntaxAndEncodingM.invoke(pc, new Object[] {
150: absFileName, jarFile, jspConfigPageEnc });
151:
152: // now the isXml and sourceEnc variables of ParserController have values
153: isXml = ((Boolean) isXmlF.get(pc)).booleanValue();
154: sourceEnc = (String) sourceEncF.get(pc);
155: } catch (IllegalAccessException e) {
156: LOGGER.log(Level.INFO, null, e);
157: throw new JasperException(e);
158: } catch (InvocationTargetException e) {
159: Throwable r = e.getTargetException();
160: if (r instanceof RuntimeException) {
161: throw (RuntimeException) r;
162: }
163: if (r instanceof FileNotFoundException) {
164: throw (FileNotFoundException) r;
165: }
166: if (r instanceof JasperException) {
167: throw (JasperException) r;
168: }
169: if (r instanceof IOException) {
170: throw (IOException) r;
171: }
172: LOGGER.log(Level.INFO, null, e);
173: throw new JasperException(e);
174: }
175: }
176: }
|