View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2009 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.env.xml;
18  
19  import java.util.Map;
20  
21  import org.springframework.core.io.Resource;
22  import org.xml.sax.Attributes;
23  import org.xml.sax.SAXException;
24  import org.xml.sax.helpers.DefaultHandler;
25  
26  import ch.elca.el4j.env.xml.handlers.EnvGroupHandler;
27  
28  /**
29   * The main SAX handler to parse the env.xml file.
30   * It delegates the env group sections to the handlers specified in the constructor.
31   *
32   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/env/src/main/java/ch/elca/el4j/env/xml/EnvSaxHandler.java $
33   *
34   * @author Stefan Wismer (SWI)
35   */
36  public class EnvSaxHandler extends DefaultHandler {
37  	/**
38  	 * The env group handlers.
39  	 */
40  	private Map<String, EnvGroupHandler> m_handlers;
41  	
42  	/**
43  	 * The active group handler.
44  	 */
45  	private EnvGroupHandler m_currentGroupHandler;
46  	
47  	/**
48  	 * The current depth in the XML tree.
49  	 */
50  	private int m_depth = 0;
51  	
52  	/**
53  	 * @param handlers    the handlers that handle a specific env group (e.g. "placeholders" -> PlaceholdersHandler).
54  	 */
55  	public EnvSaxHandler(Map<String, EnvGroupHandler> handlers) {
56  		m_handlers = handlers;
57  	}
58  	
59  	/**
60  	 * Notify which resource will be parsed next.
61  	 * @param resource    the resource that will be parsed next
62  	 */
63  	public void startResource(Resource resource) {
64  		for (EnvGroupHandler handler : m_handlers.values()) {
65  			if (handler != null) {
66  				handler.startResource(resource);
67  			}
68  		}
69  	}
70  	
71  	/** {@inheritDoc} */
72  	@Override
73  	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
74  		if (m_depth == 0) {
75  			assert qName.equals("env");
76  		} else if (m_depth == 1) {
77  			if (qName.equals("group")) {
78  				m_currentGroupHandler = m_handlers.get(attributes.getValue("type"));
79  			} else {
80  				m_currentGroupHandler = m_handlers.get(qName);
81  			}
82  		} else if (m_currentGroupHandler != null) {
83  			m_currentGroupHandler.startElement(uri, localName, qName, attributes);
84  		}
85  		m_depth++;
86  	}
87  	
88  	/** {@inheritDoc} */
89  	@Override
90  	public void characters(char[] ch, int start, int length) throws SAXException {
91  		if (m_currentGroupHandler != null) {
92  			m_currentGroupHandler.characters(ch, start, length);
93  		}
94  	}
95  	
96  	/** {@inheritDoc} */
97  	@Override
98  	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
99  		if (m_currentGroupHandler != null) {
100 			m_currentGroupHandler.ignorableWhitespace(ch, start, length);
101 		}
102 	}
103 	
104 	/** {@inheritDoc} */
105 	@Override
106 	public void endElement(String uri, String localName, String name) throws SAXException {
107 		if (m_currentGroupHandler != null) {
108 			m_currentGroupHandler.endElement(uri, localName, name);
109 		}
110 		m_depth--;
111 	}
112 }