KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25
26 import javax.servlet.jsp.tagext.PageData JavaDoc;
27
28 import org.openide.ErrorManager;
29
30 import org.apache.jasper.JasperException;
31
32 /** This class is similar to org.apache.jasper.compiler.Validator, it only
33  * allows getting access to the XML view of the page.
34  *
35  * @author Petr Jiricka
36  */

37 public class NbValidator {
38
39     private static Method JavaDoc validateXmlViewM;
40     private static Field JavaDoc bufF;
41
42     static {
43         initReflection();
44     }
45     
46     private static void initReflection() {
47         try {
48             validateXmlViewM = Validator.class.getDeclaredMethod("validateXmlView", new Class JavaDoc[] {PageData JavaDoc.class, Compiler JavaDoc.class});
49             validateXmlViewM.setAccessible(true);
50             bufF = PageDataImpl.class.getDeclaredField("buf");
51             bufF.setAccessible(true);
52         }
53         catch (NoSuchMethodException JavaDoc e) {
54             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
55         }
56         catch (NoSuchFieldException JavaDoc e) {
57             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
58         }
59     }
60     
61     /** Returns the XML view of the page.
62      */

63     public static String JavaDoc validate(Compiler JavaDoc compiler,
64                 Node.Nodes page) throws JasperException {
65
66     /*
67      * Visit the page/tag directives first, as they are global to the page
68      * and are position independent.
69      */

70     page.visit(new Validator.DirectiveVisitor(compiler));
71
72     // Determine the default output content type
73
PageInfo pageInfo = compiler.getPageInfo();
74     String JavaDoc contentType = pageInfo.getContentType();
75
76     if (contentType == null || contentType.indexOf("charset=") < 0) {
77         boolean isXml = page.getRoot().isXmlSyntax();
78         String JavaDoc defaultType;
79         if (contentType == null) {
80         defaultType = isXml? "text/xml": "text/html";
81         } else {
82         defaultType = contentType;
83         }
84
85         String JavaDoc charset = null;
86         if (isXml) {
87         charset = "UTF-8";
88         } else {
89         if (!page.getRoot().isDefaultPageEncoding()) {
90             charset = page.getRoot().getPageEncoding();
91         }
92         }
93
94         if (charset != null) {
95         pageInfo.setContentType(defaultType + ";charset=" + charset);
96         } else {
97         pageInfo.setContentType(defaultType);
98         }
99     }
100
101     /*
102      * Validate all other nodes.
103      * This validation step includes checking a custom tag's mandatory and
104      * optional attributes against information in the TLD (first validation
105      * step for custom tags according to JSP.10.5).
106      */

107     page.visit(new Validator.ValidateVisitor(compiler));
108
109     /*
110      * Invoke TagLibraryValidator classes of all imported tags
111      * (second validation step for custom tags according to JSP.10.5).
112      */

113         // validateXmlView(new PageDataImpl(page, compiler), compiler);
114
try {
115             PageDataImpl pdi = new PageDataImpl(page, compiler);
116             
117             validateXmlViewM.invoke(null, new Object JavaDoc[] {pdi, compiler});
118             
119             /*
120              * Invoke TagExtraInfo method isValid() for all imported tags
121              * (third validation step for custom tags according to JSP.10.5).
122              */

123             page.visit(new Validator.TagExtraInfoVisitor(compiler));
124             
125             StringBuffer JavaDoc buf = (StringBuffer JavaDoc)bufF.get(pdi);
126             return buf.toString();
127         }
128         catch (IllegalAccessException JavaDoc e) {
129             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
130             throw new JasperException(e.getMessage());
131         }
132         catch (InvocationTargetException JavaDoc e) {
133             Throwable JavaDoc target = e.getTargetException();
134             if (target instanceof JasperException) {
135                 throw (JasperException)target;
136             }
137             else {
138                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
139                 throw new JasperException(e.getMessage());
140             }
141         }
142
143     }
144
145
146     
147 }
148
Popular Tags