All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.facelets.util.DevTools Maven / Gradle / Ivy

Go to download

Facelets is an open source alternative view handler technology for JavaServer Faces (JSF).

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.sun.facelets.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;

import javax.el.Expression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;

public final class DevTools {
    
    private final static String TS = "<";
    
    private static final String ERROR_TEMPLATE = "META-INF/rsc/facelet-dev-error.xml";
    
    private static String[] ERROR_PARTS;
    
    private static final String DEBUG_TEMPLATE = "META-INF/rsc/facelet-dev-debug.xml";
    
    private static String[] DEBUG_PARTS;

    public DevTools() {
        super();
    }
    
    public static void main(String[] argv) throws Exception {
        DevTools.init();
    }
    
    private static void init() throws IOException {
        if (ERROR_PARTS == null) {
            ERROR_PARTS = splitTemplate(ERROR_TEMPLATE);
        }
        
        if (DEBUG_PARTS == null) {
            DEBUG_PARTS = splitTemplate(DEBUG_TEMPLATE);
        }
    }
    
    private static String[] splitTemplate(String rsc) throws IOException {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(rsc);
        if (is == null) {
            throw new FileNotFoundException(rsc);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buff = new byte[512];
        int read;
        while ((read = is.read(buff)) != -1) {
            baos.write(buff, 0, read);
        }
        String str = baos.toString();
        return str.split("@@");
    }
    
    public static void debugHtml(Writer writer, FacesContext faces, Exception e) throws IOException {
        init();
        Date now = new Date();
        for (int i = 0; i < ERROR_PARTS.length; i++) {
            if ("message".equals(ERROR_PARTS[i])) {
                String msg = e.getMessage();
                if (msg != null) {
                    writer.write(msg.replaceAll("<", TS));
                } else {
                    writer.write(e.getClass().getName());
                }
            } else if ("trace".equals(ERROR_PARTS[i])) {
                writeException(writer, e);
            } else if ("now".equals(ERROR_PARTS[i])) {
                writer.write(DateFormat.getDateTimeInstance().format(now));
            } else if ("tree".equals(ERROR_PARTS[i])) {
                writeComponent(writer, faces.getViewRoot());
            } else if ("vars".equals(ERROR_PARTS[i])) {
                writeVariables(writer, faces);
            } else {
                writer.write(ERROR_PARTS[i]);
            }
        }
    }
    
    private static void writeException(Writer writer, Exception e) throws IOException {
        StringWriter str = new StringWriter(256);
        PrintWriter pstr = new PrintWriter(str);
        e.printStackTrace(pstr);
        pstr.close();
        writer.write(str.toString().replaceAll("<", TS));
    }
    
    public static void debugHtml(Writer writer, FacesContext faces) throws IOException {
        init();
        Date now = new Date();
        for (int i = 0; i < DEBUG_PARTS.length; i++) {
            if ("message".equals(DEBUG_PARTS[i])) {
                writer.write(faces.getViewRoot().getViewId());
            } else if ("now".equals(DEBUG_PARTS[i])) {
                writer.write(DateFormat.getDateTimeInstance().format(now));
            } else if ("tree".equals(DEBUG_PARTS[i])) {            
                writeComponent(writer, faces.getViewRoot());
            } else if ("vars".equals(DEBUG_PARTS[i])) {
                writeVariables(writer, faces);
            } else {
                writer.write(DEBUG_PARTS[i]);
            }
        }
    }
    
    private static void writeVariables(Writer writer, FacesContext faces) throws IOException {
        ExternalContext ctx = faces.getExternalContext();
        writeVariables(writer, ctx.getRequestParameterMap(), "Request Parameters");
        writeVariables(writer, ctx.getRequestMap(), "Request Attributes");
        if (ctx.getSession(false) != null) {
            writeVariables(writer, ctx.getSessionMap(), "Session Attributes");
        }
        writeVariables(writer, ctx.getApplicationMap(), "Application Attributes");
    }
    
    private static void writeVariables(Writer writer, Map vars, String caption) throws IOException {
        writer.write("");
        boolean written = false;
        if (!vars.isEmpty()) {
            SortedMap map = new TreeMap(vars);
            Map.Entry entry = null;
            String key = null;
            for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) {
                entry = (Map.Entry) itr.next();
                key = entry.getKey().toString();
                if (key.indexOf('.') == -1) {
                    writer.write("");
                    written = true;
                }
            }
        }
        if (!written) {
            writer.write("");
        }
        writer.write("
"); writer.write(caption); writer.write("
NameValue
"); writer.write(key.replaceAll("<", TS)); writer.write(""); writer.write(entry.getValue() == null ? "null" : entry.getValue().toString() .replaceAll("<", TS)); writer.write("
None
"); } private static void writeComponent(Writer writer, UIComponent c) throws IOException { writer.write("
"); boolean hasChildren = c.getChildCount() > 0 || c.getFacets().size() > 0; writeStart(writer, c, hasChildren); writer.write(""); if (hasChildren) { if (c.getFacets().size() > 0) { Map.Entry entry; for (Iterator itr = c.getFacets().entrySet().iterator(); itr.hasNext(); ) { entry = (Map.Entry) itr.next(); writer.write("
"); writer.write(""); writer.write((String) entry.getKey()); writer.write(""); writeComponent(writer, (UIComponent) entry.getValue()); writer.write("
"); } } if (c.getChildCount() > 0) { for (Iterator itr = c.getChildren().iterator(); itr.hasNext(); ) { writer.write("
"); writeComponent(writer, (UIComponent) itr.next()); writer.write("
"); } } writer.write("
"); writeEnd(writer, c); writer.write("
"); } writer.write("
"); } private static void writeEnd(Writer writer, UIComponent c) throws IOException { if (!isText(c)) { writer.write(TS); writer.write('/'); writer.write(getName(c)); writer.write('>'); } } private final static String[] IGNORE = new String[] { "parent", "rendererType" }; private static void writeAttributes(Writer writer, UIComponent c) { try { BeanInfo info = Introspector.getBeanInfo(c.getClass()); PropertyDescriptor[] pd = info.getPropertyDescriptors(); Method m = null; Object v = null; String str = null; for (int i = 0; i < pd.length; i++) { if (pd[i].getWriteMethod() != null && Arrays.binarySearch(IGNORE, pd[i].getName()) < 0) { m = pd[i].getReadMethod(); try { v = m.invoke(c, null); if (v != null) { if (v instanceof Collection || v instanceof Map || v instanceof Iterator) { continue; } writer.write(" "); writer.write(pd[i].getName()); writer.write("=\""); if (v instanceof Expression) { str = ((Expression) v).getExpressionString(); } else if (v instanceof ValueBinding) { str = ((ValueBinding) v).getExpressionString(); } else if (v instanceof MethodBinding) { str = ((MethodBinding) v).getExpressionString(); } else { str = v.toString(); } writer.write(str.replaceAll("<", TS)); writer.write("\""); } } catch (Exception e) { // do nothing } } } ValueBinding binding = c.getValueBinding("binding"); if (binding != null) { writer.write(" binding=\""); writer.write(binding.getExpressionString().replaceAll("<", TS)); writer.write("\""); } } catch (Exception e) { // do nothing } } private static void writeStart(Writer writer, UIComponent c, boolean children) throws IOException { if (isText(c)) { String str = c.toString().trim(); writer.write(str.replaceAll("<", TS)); } else { writer.write(TS); writer.write(getName(c)); writeAttributes(writer, c); if (children) { writer.write('>'); } else { writer.write("/>"); } } } private static String getName(UIComponent c) { String nm = c.getClass().getName(); return nm.substring(nm.lastIndexOf('.') + 1); } private static boolean isText(UIComponent c) { return (c.getClass().getName().startsWith("com.sun.facelets.compiler")); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy