Package com.simoncat

Source Code of com.simoncat.XMLHandler

package com.simoncat;

// Servlet's part
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// DOM/XSLT part
import java.io.*;
import java.util.*;
import org.apache.xerces.dom.*;
import org.w3c.dom.*;
import org.apache.xml.serialize.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

// Reflection, etc.
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
import java.beans.Introspector;
import java.beans.BeanInfo;
import java.lang.reflect.Method;
import java.beans.PropertyDescriptor;


import com.simoncat.time.TaskProgrammer;

import com.simoncat.vo.Server;
import com.simoncat.vo.Event;


public class XMLHandler extends HttpServlet {

    private static HashMap cache;
    private ServletConfig sConfig;
   
    //static{
    private static TaskProgrammer taskProgrammer;// = new TaskProgrammer();
    //}

    public void init(ServletConfig config) throws ServletException {
        this.sConfig = config;
        taskProgrammer = new TaskProgrammer();
    }

  public static boolean addEvent(Server server,Event event){
    return taskProgrammer.scheduleEvent(server,event);
  }
 
  public static Event[] getEventsProgrammed(){
    return taskProgrammer.getEventsProgrammed();
  }
 
  public static Event[] getEventsFinished(){
    return taskProgrammer.getEventsFinished();
  }
 

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        Object form      =  request.getAttribute("form");
        String style     =  (String)request.getAttribute("style");
        String content   =  (String)request.getAttribute("content");

        if ((style == null) || (content == null)) {
            style = Constants.xmlContentType;   
        }

        if (form == null) {
            System.out.println ("[ERROR] No form bean found in request scope!");
        } else {
            Document doc = new DocumentImpl();
            try {
                Element root = doc.createElement("root")
                Element elem = processBean(form, root, doc);
                root.appendChild(elem);
                doc.appendChild(root);
            } catch ( Exception ex ) {
                System.out.println(ex.toString());
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
  
            try {
                response.setContentType(style);
                PrintWriter out  =  response.getWriter();
                if (style.equals(Constants.xmlContentType)) {
                    OutputFormat formatter = new OutputFormat();
                    formatter.setPreserveSpace(true);
                    XMLSerializer serializer = new XMLSerializer(out, formatter);
                    serializer.serialize(doc);
                } else {
                    if (cache == null) cache = new HashMap();
                    Transformer t = null;
                    String path = sConfig.getServletContext().getRealPath("/");
                    String xsl = style;
                    Source xmlSource = new DOMSource(doc);
                    // XSL processing can be time consuming, but this is mainly due to the overhead
                    // of compiling the XSL sheet.
                    // By caching the compiled sheets, the process is speeded up dramatically.
                    // Here we are introducing the simplest caching mechanism. In a real-life
                    // enterprise application it will be smart to check if XSLT template was changed
                    // after previos use.
                    if (cache.containsKey(xsl)) {
                        t = (Transformer)cache.get(xsl);
                    } else {
                        TransformerFactory tFactory = TransformerFactory.newInstance();
                        Source xslSource = new StreamSource(new File(path, xsl));
                        t = tFactory.newTransformer(xslSource);
                        cache.put(xsl, t);
                    }
                    t.transform(xmlSource, new StreamResult(out));
                }
            } catch ( Exception ex ) {
                System.out.println(ex.toString());
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }
    }

    private Element processBean(Object o, Element e, Document d) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(o.getClass());
        String beanName = info.getBeanDescriptor().getName();

        Element elem = d.createElement(beanName)
        e.appendChild(elem);

        PropertyDescriptor[] properties = info.getPropertyDescriptors();
        for (int i = 0; i < properties.length; i++) {
            Class type = properties[i].getPropertyType();
            String name = properties[i].getName();
            Method m = properties[i].getReadMethod();
            if ((m != null) && (!name.equals("class"))) {
                Object value = m.invoke(o, null);
                if (value == null) value = new String("");
                Element subelem = processProperty(name, value, elem, d);
                elem.appendChild(subelem);
            }
        }
        return elem;
    }

    private Element processProperty(String n, Object v, Element e, Document d) throws Exception {
        Class c = v.getClass();
        Package p = c.getPackage();
        Element elem = e;
        if (n != null) {
            elem = d.createElement(n);
            e.appendChild(elem);
        }

        if (v instanceof Collection) {
            Collection col = (Collection) v;
            Iterator i = col.iterator();
            while ( i.hasNext() ) {
                Element subelem = processProperty(null, i.next(), elem, d);
                if (subelem != null) {
                    e.appendChild(elem);
                }
            }
        } else {
            if ((p != null) && (p.getName().equals("java.lang"))) {
                elem.appendChild(d.createTextNode(v.toString()));
            } else {                   
                Element subelem = processBean(v, elem, d);
                elem.appendChild (subelem);
            }
        }
        return elem;
    }

}
TOP

Related Classes of com.simoncat.XMLHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.