KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portlet > admin > services > PortletNameFinder


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 JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.xml.sax.ErrorHandler JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
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 JavaDoc CLASS_NAME = "PortletNameFinder";
47
48     PortletNameFinder() {
49     }
50
51
52     public static ArrayList JavaDoc getPortletNames(InputStream JavaDoc ios) throws Exception JavaDoc {
53           final String JavaDoc METHOD_NAME = "getPortletNames(InputStream)";
54         ArrayList JavaDoc nameList = new ArrayList JavaDoc();
55         boolean validation = false;
56
57         boolean ignoreWhitespace = false;
58         boolean ignoreComments = false;
59         boolean putCDATAIntoText = false;
60         boolean createEntityRefs = false;
61
62         DocumentBuilderFactory JavaDoc 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 JavaDoc db = null;
73         try {
74             db = dbf.newDocumentBuilder();
75         } catch (ParserConfigurationException JavaDoc e) {
76             PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
77             throw new PlutoAdminException(e);
78         }
79
80         OutputStreamWriter JavaDoc errorWriter =
81                 new OutputStreamWriter JavaDoc(System.err, PlutoAdminConstants.ENCODING);
82         db.setErrorHandler(
83                 new MyErrorHandler(new PrintWriter JavaDoc(errorWriter, true)));
84
85         Document JavaDoc doc = null;
86         try {
87             doc = db.parse(ios);
88         } catch (SAXException JavaDoc e) {
89             PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
90             throw new PlutoAdminException(e);
91         } catch (IOException JavaDoc e) {
92             PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
93             throw new PlutoAdminException(e);
94         }
95
96         NodeList JavaDoc 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 JavaDoc 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 JavaDoc {
121         private static final String JavaDoc INNER_CLASS_NAME = "MyErrorHandler";
122         PrintWriter JavaDoc out = null;
123         MyErrorHandler(PrintWriter JavaDoc p_out) {
124             out = p_out;
125         }
126
127
128        private String JavaDoc getParseExceptionInfo(SAXParseException JavaDoc e) {
129             String JavaDoc systemId = e.getSystemId();
130             if (systemId == null) {
131                 systemId = "null";
132             }
133             String JavaDoc info = "URI=" + systemId +
134                 " Line=" + e.getLineNumber() +
135                 ": " + e.getMessage();
136             return info;
137         }
138
139
140         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
141                 String JavaDoc METHOD_NAME = "warning(e)";
142             String JavaDoc msg = "Warning: " + getParseExceptionInfo(e);
143             PlutoAdminLogger.logWarn(INNER_CLASS_NAME, METHOD_NAME, msg);
144         }
145
146         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
147             String JavaDoc METHOD_NAME = "error(e)";
148             String JavaDoc message = "Error: " + getParseExceptionInfo(e);
149             PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
150             throw new SAXException JavaDoc(message);
151         }
152
153         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
154             String JavaDoc METHOD_NAME = "fatalError(e)";
155             String JavaDoc message = "Fatal Error: " + getParseExceptionInfo(e);
156             PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
157             throw new SAXException JavaDoc(message);
158         }
159     }
160
161
162
163 }
164
Popular Tags