View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.services;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStreamWriter;
21  import java.io.PrintWriter;
22  import java.util.ArrayList;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.ParserConfigurationException;
27  
28  import org.apache.pluto.portlet.admin.PlutoAdminConstants;
29  import org.apache.pluto.portlet.admin.PlutoAdminException;
30  import org.apache.pluto.portlet.admin.PlutoAdminLogger;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.NodeList;
33  import org.xml.sax.ErrorHandler;
34  import org.xml.sax.SAXException;
35  import org.xml.sax.SAXParseException;
36  
37  /***
38   *
39   * Gets the portlet names from portlet.xml using SAX
40   *
41   * @author Ken Atherton
42   *
43   */
44  public class PortletNameFinder {
45  
46   	public static final String CLASS_NAME = "PortletNameFinder";
47  
48      PortletNameFinder() {
49      }
50  
51  
52      public static ArrayList  getPortletNames(InputStream ios) throws Exception {
53      	  final String METHOD_NAME = "getPortletNames(InputStream)";
54          ArrayList nameList = new ArrayList();
55          boolean validation = false;
56  
57          boolean ignoreWhitespace = false;
58          boolean ignoreComments = false;
59          boolean putCDATAIntoText = false;
60          boolean createEntityRefs = false;
61  
62          DocumentBuilderFactory dbf =
63                  DocumentBuilderFactory.newInstance();
64  
65          dbf.setValidating(validation);
66          dbf.setIgnoringComments(ignoreComments);
67          dbf.setIgnoringElementContentWhitespace(ignoreWhitespace);
68          dbf.setCoalescing(putCDATAIntoText);
69          dbf.setExpandEntityReferences(!createEntityRefs);
70  
71  
72          DocumentBuilder db = null;
73          try {
74              db = dbf.newDocumentBuilder();
75          } catch (ParserConfigurationException e) {
76          	PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
77          	throw new PlutoAdminException(e);
78          }
79  
80          OutputStreamWriter errorWriter =
81                  new OutputStreamWriter(System.err, PlutoAdminConstants.ENCODING);
82          db.setErrorHandler(
83                  new MyErrorHandler(new PrintWriter(errorWriter, true)));
84  
85          Document doc = null;
86          try {
87              doc = db.parse(ios);
88          } catch (SAXException e) {
89          	PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
90          	throw new PlutoAdminException(e);
91          } catch (IOException e) {
92          	PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
93          	throw new PlutoAdminException(e);
94          }
95  
96          NodeList portletNames = doc.getElementsByTagName("portlet-name");
97          if (null !=  portletNames)
98          {
99              for (int i = 0; i < portletNames.getLength(); i++) {
100                 if (null != portletNames.item(i).getChildNodes().item(0))
101                 {
102                     String portletName  =  portletNames.item(i).getChildNodes().item(0).getNodeValue();
103                     nameList.add(portletName);
104                 }
105             }
106         }
107         return nameList;
108     }
109 
110 
111 
112 
113     /***
114      *
115      * Inner class that handles errors
116      *
117      * @author Ken Atherton
118      *
119      */
120     private static class MyErrorHandler implements ErrorHandler {
121         private static final String INNER_CLASS_NAME = "MyErrorHandler";
122         PrintWriter out = null;
123         MyErrorHandler(PrintWriter p_out) {
124         	out = p_out;
125         }
126 
127 
128        private String getParseExceptionInfo(SAXParseException e) {
129             String systemId = e.getSystemId();
130             if (systemId == null) {
131                 systemId = "null";
132             }
133             String info = "URI=" + systemId +
134                 " Line=" + e.getLineNumber() +
135                 ": " + e.getMessage();
136             return info;
137         }
138 
139 
140         public void warning(SAXParseException e) throws SAXException {
141         		String METHOD_NAME = "warning(e)";
142             String msg = "Warning: " + getParseExceptionInfo(e);
143           	PlutoAdminLogger.logWarn(INNER_CLASS_NAME, METHOD_NAME, msg);
144         }
145 
146         public void error(SAXParseException e) throws SAXException {
147       		String METHOD_NAME = "error(e)";
148             String message = "Error: " + getParseExceptionInfo(e);
149           	PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
150             throw new SAXException(message);
151         }
152 
153         public void fatalError(SAXParseException e) throws SAXException {
154       		String METHOD_NAME = "fatalError(e)";
155             String message = "Fatal Error: " + getParseExceptionInfo(e);
156           	PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
157             throw new SAXException(message);
158         }
159     }
160 
161 
162 
163 }