View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.webapp.filter;
20  
21  import java.util.Vector;
22  
23  import org.xml.sax.Attributes;
24  import org.xml.sax.SAXException;
25  import org.xml.sax.helpers.DefaultHandler;
26  
27  
28  /**
29   * DOCUMENT ME!
30   *
31   * @author Robert J. Lebowitz (latest modification by $Author: grantsmith $)
32   * @version $Revision: 472644 $ $Date: 2006-11-08 16:04:52 -0500 (Wed, 08 Nov 2006) $
33   */
34  public class WelcomeFileHandler
35  extends DefaultHandler
36  {
37      //~ Instance fields --------------------------------------------------------
38  
39      private StringBuffer sb           = new StringBuffer();
40      private Vector       welcomeFiles;
41      private String[]     files;
42      private boolean      fileFlag = false;
43  
44      //~ Constructors -----------------------------------------------------------
45  
46      /**
47       * Creates a new WelcomeFileHandler object.
48       */
49      public WelcomeFileHandler()
50      {
51          super();
52      }
53  
54      //~ Methods ----------------------------------------------------------------
55  
56      /**
57       * Accessor method used to get the array of welcome files.
58       * @return The string array of welcome files.
59       *
60       */
61      public String[] getWelcomeFiles()
62      {
63          return files;
64      }
65  
66      /**
67       *
68       * Method used to examine, modify or extract text in body of an element
69       * @param ch character array containing the tag's body information.
70       * @param start starting index of the body text in the character array.
71       * @param length length of the text found in the body.
72       * @throws SAXException
73       *
74       */
75      public void characters(char[] ch, int start, int length)
76      throws SAXException
77      {
78          if (fileFlag)
79          {
80              sb.append(ch, start, length);
81          }
82  
83          super.characters(ch, start, length);
84      }
85  
86      /**
87       * Method called with each end element in an XML Document
88       * @param ns The namespace associated with this element
89       * @param local The local name of this element
90       * @param qName The qualified name of this element
91       * @throws SAXException
92       *
93       */
94      public void endElement(String ns, String local, String qName)
95      throws SAXException
96      {
97          if (qName.equals("welcome-file-list"))
98          {
99              files = new String[welcomeFiles.size()];
100             welcomeFiles.toArray(files);
101             welcomeFiles = null;
102         }
103 
104         if (qName.equals("welcome-file"))
105         {
106             welcomeFiles.add(sb.toString());
107             sb.setLength(0);
108             fileFlag = false;
109         }
110 
111         super.endElement(ns, local, qName);
112     }
113 
114     /**
115      *
116      *  Method called with each start element in an XML document
117      * @param ns The namespace associated with this element
118      * @param local The local name of this element
119      * @param qName The qualified name of this element
120      * @param atts Attributes associated with this element
121      * @throws SAXException
122      *
123      */
124     public void startElement(
125         String ns, String local, String qName, Attributes atts)
126     throws SAXException
127     {
128         if (qName.equals("welcome-file-list"))
129         {
130             welcomeFiles = new Vector();
131         }
132 
133         if (qName.equals("welcome-file"))
134         {
135             sb.setLength(0);
136             fileFlag = true;
137         }
138 
139         super.startElement(ns, local, qName, atts);
140     }
141 }