View Javadoc

1   /*
2    * $Id: XmlOutputServlet.java 126 2004-11-01 20:30:33Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.servlets;
24  
25  import java.io.PrintWriter;
26  import java.util.Collection;
27  import java.util.Enumeration;
28  import java.util.Iterator;
29  
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.talika.commons.util.FormatHelper;
36  
37  /**
38   * XML output servlet of Tarsis MVC Framework.<br>
39   * <br>
40   *
41   * @author  Jose M. Palomar
42   * @version $Revision: 126 $
43   */
44  public final class XmlOutputServlet extends AbstractServlet {
45  
46      // Constants
47      /**
48       * Content type.
49       */
50      private static final String CONTENT_TYPE = "text/xml";
51  
52      /**
53       * XML doc type.
54       */
55      private static final String DOC_TYPE =
56                  "<!DOCTYPE xml-output\n" +
57                      "\tPUBLIC \"-//Talika Open Source Group//Xml Output DTD 1.0//ES\"\n" +
58                      "\t\"http://www.talika.org/dtds/xml-output_1_0.dtd\">\n";
59  
60      // Fields
61      /**
62       * Format helper instance.
63       */
64      private FormatHelper formatHelper;
65  
66      // Methods
67      /**
68       * Called by the servlet container to indicate to a servlet that the servlet is
69       * being placed into service.<br>
70       * <br>
71       *
72       * @param config ServletConfig the <code>ServletConfig</code> object that contains
73       * configutation information for this servlet.
74       * @throws ServletException if an exception occurs that interrupts the servlet's
75       * normal operation.
76       * @see javax.servlet.Servlet#init(ServletConfig)
77       */
78      public void init(ServletConfig config) throws ServletException {
79          super.init(config);
80          this.formatHelper = FormatHelper.getInstance();
81      }
82  
83      /**
84       * Called by the servlet container to indicate to a servlet that the servlet is
85       * being taken out of service.
86       *
87       * @see javax.servlet.Servlet#destroy()
88       */
89      public void destroy() {
90          super.destroy();
91      }
92  
93      /**
94       * Processes requests for both HTTP GET and POST methods.<br>
95       * <br>
96       *
97       * @param request servlet object that contains the request the client has made of
98       * the servlet.
99       * @param response servlet object that contains the response the servlet sends to
100      * the client.
101      * @throws ServletException if the request could not be handled.
102      * @throws IOException if an input or output error is detected when the servlet
103      * handles request.
104      */
105     protected void process(HttpServletRequest request, HttpServletResponse response)
106     throws ServletException, java.io.IOException {
107 
108         try {
109 
110             // Set Content Type
111             response.setContentType(CONTENT_TYPE);
112 
113             PrintWriter out = response.getWriter();
114 
115             // Print Header
116             printHeader(out);
117 
118             Enumeration atts = request.getAttributeNames();
119             while (atts.hasMoreElements()) {
120 
121                 String attName = (String) atts.nextElement();
122                 Object attValue = request.getAttribute(attName);
123 
124                 if (attValue instanceof Collection) {
125                     printValueList(out, attName, (Collection) attValue);
126                 }
127                 else {
128                     printValue(out, attName, attValue);
129                 }
130 
131             }
132 
133             printFooter(out);
134 
135         }
136         catch (Throwable t) {
137             throw new ServletException(t);
138         }
139 
140     }
141 
142     /**
143      * Prints document header.
144      *
145      * @param out PrintWriter writer.
146      */
147     private void printHeader(PrintWriter out) {
148 
149         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
150         out.println();
151         out.println(DOC_TYPE);
152         out.println();
153 
154         out.println("<xml-output>");
155 
156     }
157 
158     /**
159      * Prints attribute value.
160      *
161      * @param out PrintWriter writer.
162      * @param name String attribute name.
163      * @param value Object attribute value.
164      */
165     private void printValue(PrintWriter out, String name, Object value) {
166 
167         out.println("\t<attribute>");
168         out.println("\t\t<name>" + name + "</name>");
169         out.println("\t\t<value>" + formatHelper.formatObject(value) + "</value>");
170         out.println("\t</attribute>");
171 
172     }
173 
174     /**
175      * Prints attribute values.
176      *
177      * @param out PrintWriter writer.
178      * @param name String attribute name.
179      * @param values Collection values collection.
180      */
181     private void printValueList(PrintWriter out, String name, Collection values) {
182 
183         if (values.isEmpty()) {
184             return;
185         }
186 
187         out.println("\t<attribute>");
188         out.println("\t\t<name>" + name + "</name>");
189         Iterator valuesIterator = values.iterator();
190         while (valuesIterator.hasNext()) {
191             Object value = valuesIterator.next();
192             out.println("\t\t<value>" + formatHelper.formatObject(value) + "</value>");
193         }
194         out.println("\t</attribute>");
195 
196     }
197 
198     /**
199      * Prints document footer.
200      *
201      * @param out PrintWriter writer.
202      */
203     private void printFooter(PrintWriter out) {
204 
205         out.println("</xml-output>");
206 
207     }
208 
209 }