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.lang.reflect.Method;
046: import java.lang.reflect.InvocationTargetException;
047:
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.servlet.jsp.tagext.PageData;
051:
052: import org.apache.jasper.JasperException;
053:
054: /** This class is similar to org.apache.jasper.compiler.Validator, it only
055: * allows getting access to the XML view of the page.
056: *
057: * @author Petr Jiricka
058: */
059: public class NbValidator {
060:
061: private static final Logger LOGGER = Logger
062: .getLogger(NbValidator.class.getName());
063:
064: private static Method validateXmlViewM;
065: private static Field bufF;
066:
067: static {
068: initReflection();
069: }
070:
071: private static void initReflection() {
072: try {
073: validateXmlViewM = Validator.class.getDeclaredMethod(
074: "validateXmlView", new Class[] { PageData.class,
075: Compiler.class }); // NOI18N
076: validateXmlViewM.setAccessible(true);
077: bufF = PageDataImpl.class.getDeclaredField("buf"); // NOI18N
078: bufF.setAccessible(true);
079: } catch (NoSuchMethodException e) {
080: LOGGER.log(Level.INFO, null, e);
081: } catch (NoSuchFieldException e) {
082: LOGGER.log(Level.INFO, null, e);
083: }
084: }
085:
086: /** Returns the XML view of the page.
087: */
088: public static String validate(Compiler compiler, Node.Nodes page)
089: throws JasperException {
090:
091: /*
092: * Visit the page/tag directives first, as they are global to the page
093: * and are position independent.
094: */
095: page.visit(new Validator.DirectiveVisitor(compiler));
096:
097: // Determine the default output content type
098: PageInfo pageInfo = compiler.getPageInfo();
099: String contentType = pageInfo.getContentType();
100:
101: if (contentType == null || contentType.indexOf("charset=") < 0) { // NOI18N
102: boolean isXml = page.getRoot().isXmlSyntax();
103: String defaultType;
104: if (contentType == null) {
105: defaultType = isXml ? "text/xml" : "text/html"; // NOI18N
106: } else {
107: defaultType = contentType;
108: }
109:
110: String charset = null;
111: if (isXml) {
112: charset = "UTF-8"; // NOI18N
113: } else {
114: if (!page.getRoot().isDefaultPageEncoding()) {
115: charset = page.getRoot().getPageEncoding();
116: }
117: }
118:
119: if (charset != null) {
120: pageInfo.setContentType(defaultType + ";charset="
121: + charset); // NOI18N
122: } else {
123: pageInfo.setContentType(defaultType);
124: }
125: }
126:
127: /*
128: * Validate all other nodes.
129: * This validation step includes checking a custom tag's mandatory and
130: * optional attributes against information in the TLD (first validation
131: * step for custom tags according to JSP.10.5).
132: */
133: page.visit(new Validator.ValidateVisitor(compiler));
134:
135: /*
136: * Invoke TagLibraryValidator classes of all imported tags
137: * (second validation step for custom tags according to JSP.10.5).
138: */
139: // validateXmlView(new PageDataImpl(page, compiler), compiler);
140: try {
141: PageDataImpl pdi = new PageDataImpl(page, compiler);
142:
143: validateXmlViewM.invoke(null,
144: new Object[] { pdi, compiler });
145:
146: /*
147: * Invoke TagExtraInfo method isValid() for all imported tags
148: * (third validation step for custom tags according to JSP.10.5).
149: */
150: page.visit(new Validator.TagExtraInfoVisitor(compiler));
151:
152: StringBuffer buf = (StringBuffer) bufF.get(pdi);
153: return buf.toString();
154: } catch (IllegalAccessException e) {
155: LOGGER.log(Level.INFO, null, e);
156: throw new JasperException(e.getMessage());
157: } catch (InvocationTargetException e) {
158: Throwable target = e.getTargetException();
159: if (target instanceof JasperException) {
160: throw (JasperException) target;
161: } else {
162: LOGGER.log(Level.INFO, null, e);
163: throw new JasperException(e.getMessage());
164: }
165: }
166: }
167: }
|