001: /*
002: * XML 2 Java Binding (X2JB) - the excellent Java tool.
003: * Copyright 2007, by Richard Opalka.
004: *
005: * This is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as
007: * published by the Free Software Foundation; either version 2.1 of
008: * the License, or (at your option) any later version.
009: *
010: * This software is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this software; if not see the FSF site:
017: * http://www.fsf.org/ and search for the LGPL License document there.
018: */
019: package webapp;
020:
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.ParserConfigurationException;
024: import org.w3c.dom.Document;
025: import org.x2jb.bind.XML2Java;
026: import webapp.ifaces.*;
027:
028: /**
029: * Web app sample
030: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
031: * @version 1.0
032: */
033: public final class Main {
034:
035: private static final int PADDING_SIZE = 4;
036:
037: /**
038: * Lookups specified resource on the classpath and returns parsed document instance
039: * @param classpath resource to return
040: */
041: private static Document getDocument(String resource)
042: throws Exception {
043: Document retVal = null;
044: try {
045: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
046: .newInstance();
047: builderFactory.setIgnoringComments(true);
048: DocumentBuilder builder = builderFactory
049: .newDocumentBuilder();
050: retVal = builder.parse(Main.class
051: .getResourceAsStream(resource));
052: } catch (ParserConfigurationException e) {
053: e.printStackTrace(System.err);
054: System.exit(1);
055: }
056: return retVal;
057: }
058:
059: public static void main(String[] args) throws Exception {
060: Document parsedDocument = getDocument("/web.xml");
061: WebApp webApp = (WebApp) XML2Java.bind(parsedDocument,
062: WebApp.class);
063:
064: System.out.println();
065: System.out.println("Web application configuration: ");
066: System.out.println();
067: printDescription(webApp.getDescription(), 0);
068: System.out.println();
069: printFilters(webApp.getFilters(), 0);
070: printFilterMappings(webApp.getFilterMappings(), 0);
071: printListeners(webApp.getListeners(), 0);
072: printServlets(webApp.getServlets(), 0);
073: printServletMappings(webApp.getServletMappings(), 0);
074: printEnvEntries(webApp.getEnvEntries(), 0);
075: printSessionConfig(webApp.getSessionConfig(), 0);
076: printWelcomeFileList(webApp.getWelcomeFileList(), 0);
077: }
078:
079: private static void printWelcomeFileList(WelcomeFileList list,
080: int level) {
081: if (list != null) {
082: String[] welcomeFiles = list.getFiles();
083: if (welcomeFiles.length != 0) {
084: printPadding(level);
085: System.out.println("Welcome file list: ");
086: System.out.println();
087: for (int i = 0; i < welcomeFiles.length; i++) {
088: printPadding(level + 1);
089: System.out.println("Welcome file: "
090: + welcomeFiles[i]);
091: }
092: System.out.println();
093: }
094: }
095: }
096:
097: private static void printSessionConfig(SessionConfig config,
098: int level) {
099: if (config != null) {
100: printPadding(level);
101: System.out.println("Session timeout: "
102: + config.getSessionTimeout());
103: System.out.println();
104: }
105: }
106:
107: private static void printEnvEntries(EnvEntry[] entries, int level) {
108: if (entries.length != 0) {
109: printPadding(level);
110: System.out.println("Environment Entries: ");
111: System.out.println();
112: for (int i = 0; i < entries.length; i++) {
113: EnvEntry e = entries[i];
114: printDescription(e.getDescription(), level + 1);
115: printPadding(level + 1);
116: System.out.println("Entry name: " + e.getName());
117: printPadding(level + 1);
118: System.out.println("Entry type: " + e.getType());
119: if (e.getValue() != null) {
120: printPadding(level + 1);
121: System.out.println("Entry value: " + e.getValue());
122: }
123: System.out.println();
124: }
125: }
126: }
127:
128: private static void printServletMappings(
129: ServletMapping[] servletMappings, int level) {
130: if (servletMappings.length != 0) {
131: printPadding(level);
132: System.out.println("Servlet mappings: ");
133: System.out.println();
134: for (int i = 0; i < servletMappings.length; i++) {
135: ServletMapping s = servletMappings[i];
136: printPadding(level + 1);
137: System.out.println("Servlet name: "
138: + s.getServletName());
139: printPadding(level + 1);
140: System.out.println("URL pattern: " + s.getUrlPattern());
141: System.out.println();
142: }
143: }
144: }
145:
146: private static void printListeners(Listener[] listeners, int level) {
147: if (listeners.length != 0) {
148: printPadding(level);
149: System.out.println("Listeners: ");
150: System.out.println();
151: for (int i = 0; i < listeners.length; i++) {
152: Listener l = listeners[i];
153: printPadding(level + 1);
154: System.out.println("Listener class: "
155: + l.getListenerClass());
156: System.out.println();
157: }
158: }
159: }
160:
161: private static void printPadding(int level) {
162: for (int i = 0; i < level * PADDING_SIZE; i++) {
163: System.out.print(' ');
164: }
165: }
166:
167: private static void printDescription(Description desc, int level) {
168: if (desc.getDescription() != null) {
169: printPadding(level);
170: System.out.println("Description: " + desc.getDescription());
171: }
172: if (desc.getDisplayName() != null) {
173: printPadding(level);
174: System.out
175: .println("Display name: " + desc.getDisplayName());
176: }
177: if (desc.getIcon() != null) {
178: printPadding(level);
179: System.out.println("Icon: " + desc.getIcon());
180: }
181: }
182:
183: private static void printFilters(Filter[] filters, int level) {
184: if (filters.length != 0) {
185: printPadding(level);
186: System.out.println("Filters: ");
187: System.out.println();
188: for (int i = 0; i < filters.length; i++) {
189: Filter f = filters[i];
190: printDescription(f.getDescription(), level + 1);
191: printPadding(level + 1);
192: System.out.println("Filter name: " + f.getName());
193: printPadding(level + 1);
194: System.out.println("Filter class: " + f.getClassName());
195: printParams(f.getInitParams(), level + 2);
196: System.out.println();
197: }
198: }
199: }
200:
201: private static void printServlets(Servlet[] servlets, int level) {
202: if (servlets.length != 0) {
203: printPadding(level);
204: System.out.println("Servlets: ");
205: System.out.println();
206: for (int i = 0; i < servlets.length; i++) {
207: Servlet s = servlets[i];
208: printPadding(level + 1);
209: System.out.println("Servlet name: " + s.getName());
210: printPadding(level + 1);
211: if (s.getClassName() != null) {
212: System.out.println("Servlet class: "
213: + s.getClassName());
214: } else {
215: System.out.println("Servlet JSP file: "
216: + s.getJspFile());
217: }
218: if (s.getLoadOnStartup() != 0) {
219: printPadding(level + 1);
220: System.out.println("Load on startup: "
221: + s.getLoadOnStartup());
222: }
223: printParams(s.getInitParams(), level + 2);
224: System.out.println();
225: }
226: }
227: }
228:
229: private static void printFilterMappings(
230: FilterMapping[] filterMappings, int level) {
231: if (filterMappings.length != 0) {
232: printPadding(level);
233: System.out.println("Filter mappings: ");
234: System.out.println();
235: for (int i = 0; i < filterMappings.length; i++) {
236: FilterMapping f = filterMappings[i];
237: printPadding(level + 1);
238: System.out.println("Filter name: " + f.getFilterName());
239: printPadding(level + 1);
240: if (f.getServletName() != null) {
241: System.out.println("Applies to servlet: "
242: + f.getServletName());
243: } else {
244: System.out.println("Applies to URL pattern: "
245: + f.getURLPattern());
246: }
247: Dispatcher[] dispatchers = f.getDispatchers();
248: if (dispatchers.length != 0) {
249: printPadding(level + 1);
250: System.out.print("Dispatchers: ");
251: for (int j = 0; j < dispatchers.length; j++) {
252: System.out.print(dispatchers[j] + " ");
253: }
254: System.out.println();
255: }
256: System.out.println();
257: }
258: }
259: }
260:
261: private static void printParams(Parameter[] params, int level) {
262: for (int i = 0; i < params.length; i++) {
263: System.out.println();
264: Parameter p = params[i];
265: if (p.getDescription() != null) {
266: printPadding(level);
267: System.out.println("Parameter description: "
268: + p.getDescription());
269: }
270: printPadding(level);
271: System.out.println("Param name: " + p.getName());
272: printPadding(level);
273: System.out.println("Param value: " + p.getValue());
274: }
275: }
276:
277: }
|