Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/lilacsoftware/util/ServletConfigurationInserter.java


1   /*____________________________________________________________________________
2     
3     Orca - Audio System
4   
5     Copyright (C) 2001 Tom Wadzinski <orca_twadzins@yahoo.com>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21    
22  ____________________________________________________________________________*/
23  
24  package com.lilacsoftware.util;
25  
26  import java.io.*;
27  
28  /**
29   * <code>ServletConfigurationInserter</code> is a quick and dirty tool to insert a 
30   * <servlet>/<servlet mapping> section into an existing
31   * web.xml file. Used with JspC, so that jsp precompiling can be done during a build
32   * without manual file modification.
33   */
34  public class ServletConfigurationInserter
35  {
36      private static final String USAGE = "java com.lilacsoftware.util.ServletConfigurationInserter snippetFile webXMLFile outputFile";
37  
38      public static void main(String args[]) { 
39    if(args.length != 3){
40        System.err.println(USAGE);
41        System.exit(1);
42    }
43    String snippetFile = args[0];
44    String webXMLFile = args[1];
45    String outputFile = args[2];
46    try {
47        ServletConfigurationInserter sci = new ServletConfigurationInserter();
48        sci.insertSnippet(snippetFile,  webXMLFile,  outputFile);
49        System.out.println("ServletConfigurationInserter: updated " + outputFile);
50    }
51    catch (Exception e) {
52        e.printStackTrace();
53        System.exit(2);
54    }
55      } 
56  
57  
58      /*
59       * Read in webXMLFile, writing to outputFile, looking for presence
60       * of first <servlet-mapping> tag.  When found, write snippet file to outputFile.
61       * Then finish writing webXMLFile to outputFile, blissfully ignorant of comments...
62       */
63      public void insertSnippet(String snippetFile, String webXMLFile, String outputFile)
64    throws IOException
65      {
66    boolean foundFirstMappingTag = false;
67    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
68  
69    //Read in web.xml file and begin writing to outputFile
70    BufferedReader in = new BufferedReader(new FileReader(webXMLFile));
71    String line;
72    while ((line = in.readLine()) != null) {
73        //Look for first servlet mapping line
74        if ( (!foundFirstMappingTag) && (line.indexOf("<servlet-mapping>") >= 0) ) {
75      out.println("<!-- com.lilacsoftware.orca.ServletConfigurationInserter: Inserting servlet section -->");
76      foundFirstMappingTag = true;
77      writeInSnippet(snippetFile, out);
78      out.println("<!-- com.lilacsoftware.orca.ServletConfigurationInserter: End Inserting servlet section -->");
79        }
80       out.println(line); 
81    }
82    in.close();
83    out.close();
84    if (!foundFirstMappingTag) {
85        throw new IOException("Error:webxmlFile didn't contain a <servlet-mapping> tag!");
86    }
87      }
88  
89      private void writeInSnippet(String snippetFile, PrintWriter out)
90    throws IOException
91      {
92    //Read in snippet and write to outputFile
93    BufferedReader in = new BufferedReader(new FileReader(snippetFile));
94    String line;
95    while ((line = in.readLine()) != null) {
96        if (line.startsWith("JASPER")) {
97      //Skip non-xml "JASPER" lines 
98      continue;
99        }
100       
101       if (line.indexOf("\\") >= 0) {
102     //Replace backslash with forward slash...
103     line = line.replace('\\', '/');
104       }
105       out.println(line);
106   }
107   in.close();
108     }
109 
110 }