Skip to content

Commit 927cf55

Browse files
committed
FacesConfigBeanNameResolver now uses a SAX-based parser to process faces-config.xml
1 parent 69cdf11 commit 927cf55

2 files changed

Lines changed: 223 additions & 28 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/el/resolver/FacesConfigBeanNameResolver.java‎

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@
3232
import javax.faces.webapp.FacesServlet;
3333
import javax.servlet.ServletContext;
3434

35-
import org.apache.commons.digester.Digester;
3635
import org.apache.commons.logging.Log;
3736
import org.apache.commons.logging.LogFactory;
38-
import org.xml.sax.SAXException;
3937

4038
import com.ocpsoft.pretty.faces.spi.ELBeanNameResolver;
41-
import com.ocpsoft.pretty.faces.util.EmptyEntityResolver;
39+
import com.ocpsoft.pretty.faces.util.SimpleXMLParserBase;
4240

4341
/**
4442
*
@@ -246,45 +244,25 @@ private void processFacesConfig(URL url, List<FacesConfigEntry> facesConfigEntri
246244
log.trace("Loading bean names from: " + url.toString());
247245
}
248246

249-
// setup digester
250-
Digester digester = new Digester();
247+
FacesConfigParser parser = new FacesConfigParser();
251248

252-
/*
253-
* We use the context class loader to resolve classes.
254-
* This fixes ClassNotFoundExceptions on Geronimo.
255-
*/
256-
digester.setUseContextClassLoader(true);
257-
258-
// prevent downloading of DTDs
259-
digester.setEntityResolver(new EmptyEntityResolver());
260-
261-
digester.setValidating(false);
262-
digester.push(facesConfigEntries);
263-
digester.addObjectCreate("faces-config/managed-bean", FacesConfigEntry.class);
264-
digester.addCallMethod("faces-config/managed-bean/managed-bean-name", "setName", 0);
265-
digester.addCallMethod("faces-config/managed-bean/managed-bean-class", "setBeanClass", 0);
266-
digester.addSetNext("faces-config/managed-bean", "add");
267-
268-
// stream used to read faces-config.xml file
269249
InputStream stream = null;
270250

271251
try
272252
{
273253
// open the file and let digester pares it
274254
stream = url.openStream();
275-
digester.parse(stream);
255+
parser.parse(stream);
256+
257+
// add new faces config entries
258+
facesConfigEntries.addAll( parser.getEntries() );
276259

277260
}
278261
catch (IOException e)
279262
{
280263
// may be thrown when reading the file
281264
log.error("Failed to parse: " + url.toString(), e);
282265
}
283-
catch (SAXException e)
284-
{
285-
// parsing errors
286-
log.error("Failed to parse: " + url.toString(), e);
287-
}
288266
finally
289267
{
290268
// close stream
@@ -350,5 +328,61 @@ public void setBeanClass(String beanClass)
350328
}
351329

352330
}
331+
332+
/**
333+
* Very simple parser to read the managed bean entries for a faces-config.xml
334+
*/
335+
private static class FacesConfigParser extends SimpleXMLParserBase
336+
{
337+
338+
private final List<FacesConfigEntry> entries = new ArrayList<FacesConfigEntry>();
339+
340+
private FacesConfigEntry currentEntry = null;
341+
342+
@Override
343+
public void processStartElement(String name)
344+
{
345+
// <managed-bean>
346+
if (elements("faces-config", "managed-bean"))
347+
{
348+
currentEntry = new FacesConfigEntry();
349+
}
350+
}
351+
352+
@Override
353+
public void processEndElement(String name)
354+
{
355+
// </managed-bean>
356+
if (elements("faces-config", "managed-bean"))
357+
{
358+
entries.add(currentEntry);
359+
currentEntry = null;
360+
}
361+
}
362+
363+
@Override
364+
public void processCharacters(String text)
365+
{
366+
367+
// <managed-bean-name>
368+
if (elements("faces-config", "managed-bean", "managed-bean-name"))
369+
{
370+
currentEntry.setName(text.trim());
371+
}
372+
373+
// <managed-bean-class>
374+
if (elements("faces-config", "managed-bean", "managed-bean-class"))
375+
{
376+
currentEntry.setBeanClass(text.trim());
377+
}
378+
379+
}
380+
381+
public List<FacesConfigEntry> getEntries()
382+
{
383+
return entries;
384+
}
385+
386+
}
353387

354388
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.ocpsoft.pretty.faces.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Stack;
6+
7+
import javax.xml.parsers.ParserConfigurationException;
8+
import javax.xml.parsers.SAXParser;
9+
import javax.xml.parsers.SAXParserFactory;
10+
11+
import org.xml.sax.Attributes;
12+
import org.xml.sax.SAXException;
13+
import org.xml.sax.helpers.DefaultHandler;
14+
15+
/**
16+
* Base class for implementing XML parsers based on the SAX API.
17+
*
18+
* @author Christian Kaltepoth <christian@kaltepoth.de>
19+
*/
20+
public abstract class SimpleXMLParserBase extends DefaultHandler
21+
{
22+
23+
/**
24+
* Used to maintain a list of parents up to the root of the XML tree
25+
*/
26+
private final Stack<String> elements = new Stack<String>();
27+
28+
/**
29+
* Parses the supplied {@link InputStream}.
30+
*
31+
* @param stream
32+
* The stream to parse
33+
* @throws IOException
34+
* for any errors during the parsing process
35+
*/
36+
public final void parse(InputStream stream) throws IOException
37+
{
38+
39+
// all XML exceptions are wrapped by IOExceptions
40+
try
41+
{
42+
43+
// setup a namespace aware SAXParserFactory
44+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
45+
saxParserFactory.setNamespaceAware(true);
46+
47+
// parse the input stream
48+
SAXParser parser = saxParserFactory.newSAXParser();
49+
parser.parse(stream, new SimpleHandler());
50+
51+
}
52+
catch (ParserConfigurationException e)
53+
{
54+
throw new IOException(e);
55+
}
56+
catch (SAXException e)
57+
{
58+
throw new IOException(e);
59+
}
60+
61+
}
62+
63+
/**
64+
* Called for every element start event
65+
*
66+
* @param name
67+
* The local name of the element
68+
*/
69+
public abstract void processStartElement(String name);
70+
71+
/**
72+
* Called for every element end event
73+
*
74+
* @param name
75+
* The local name of the element
76+
*/
77+
public abstract void processEndElement(String name);
78+
79+
/**
80+
* Called for every characters event
81+
*
82+
* @param text
83+
* The characters
84+
*/
85+
public abstract void processCharacters(String text);
86+
87+
/**
88+
* This class maintaince the parent element stack and forwards all events to
89+
* the abstract methods of {@link SimpleXMLParserBase}.
90+
*/
91+
private class SimpleHandler extends DefaultHandler
92+
{
93+
94+
@Override
95+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
96+
{
97+
98+
// add the element's local name to the parent stack
99+
elements.add(localName);
100+
101+
// forward event
102+
processStartElement(localName);
103+
104+
}
105+
106+
@Override
107+
public void characters(char[] ch, int start, int length) throws SAXException
108+
{
109+
// forward event
110+
processCharacters(String.valueOf(ch, start, length));
111+
}
112+
113+
@Override
114+
public void endElement(String uri, String localName, String qName) throws SAXException
115+
{
116+
// forward event
117+
processEndElement(localName);
118+
119+
// remove the name on top of the stack
120+
String removedName = elements.pop();
121+
122+
// check if the removed name is the expected one
123+
if (!removedName.equals(localName))
124+
{
125+
throw new IllegalStateException("Found '" + removedName + "' but expected '" + localName
126+
+ "' on the stack!");
127+
}
128+
129+
}
130+
131+
}
132+
133+
/**
134+
* Checks whether the parent stack currently contains exactly the elements
135+
* supplied by the caller. This method can be used to find the current
136+
* position in the document. The first argument is always the root element of
137+
* the document, the second is a child of the root element, and so on. The
138+
* method uses the local names of the elements only. Namespaces are ignored.
139+
*
140+
* @param name
141+
* The names of the parent elements
142+
* @return <code>true</code> if the parent element stack contains exactly
143+
* these elements
144+
*/
145+
protected boolean elements(String... name)
146+
{
147+
if (name.length != elements.size())
148+
{
149+
return false;
150+
}
151+
for (int i = 0; i < name.length; i++)
152+
{
153+
if (!name[i].equals(elements.get(i)))
154+
{
155+
return false;
156+
}
157+
}
158+
return true;
159+
}
160+
161+
}

0 commit comments

Comments
 (0)