001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/jsf/widgets/src/java/org/theospi/jsf/impl/XmlDocumentHandler.java $
003: * $Id: XmlDocumentHandler.java 9140 2006-05-08 23:25:46Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.jsf.impl;
021:
022: import java.io.IOException;
023: import java.util.Stack;
024:
025: import javax.faces.component.UIComponent;
026: import javax.faces.context.FacesContext;
027:
028: import org.theospi.jsf.intf.ComponentWrapper;
029: import org.theospi.jsf.intf.XmlTagFactory;
030: import org.theospi.jsf.intf.XmlTagHandler;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.helpers.DefaultHandler;
034:
035: /**
036: * Created by IntelliJ IDEA.
037: * User: John Ellis
038: * Date: Dec 29, 2005
039: * Time: 1:52:58 PM
040: * To change this template use File | Settings | File Templates.
041: */
042: public class XmlDocumentHandler extends DefaultHandler {
043:
044: private XmlTagFactory factory;
045: private FacesContext context;
046: private UIComponent rootView;
047: private Stack components = new Stack();
048: private XmlTagHandler defaultHandler;
049:
050: public XmlDocumentHandler(FacesContext context,
051: XmlTagFactory factory, UIComponent rootView) {
052: this .context = context;
053: this .factory = factory;
054: this .rootView = rootView;
055: this .defaultHandler = factory.getDefaultHandler();
056: }
057:
058: public void startElement(String uri, String localName,
059: String qName, Attributes attributes) throws SAXException {
060: try {
061: ComponentWrapper wrapper = defaultHandler.startElement(
062: context, getCurrentComponent(), uri, localName,
063: qName, attributes);
064: components.push(wrapper);
065: } catch (IOException e) {
066: throw new SAXException(e);
067: }
068: }
069:
070: public void characters(char ch[], int start, int length)
071: throws SAXException {
072: ComponentWrapper component = (ComponentWrapper) components
073: .peek();
074: XmlTagHandler handler = component.getHandler();
075: if (handler == null) {
076: handler = defaultHandler;
077: }
078: try {
079: handler.characters(context, component, ch, start, length);
080: } catch (IOException e) {
081: throw new SAXException(e);
082: }
083: }
084:
085: public void endElement(String uri, String localName, String qName)
086: throws SAXException {
087: ComponentWrapper component = (ComponentWrapper) components
088: .pop();
089: XmlTagHandler handler = component.getHandler();
090: if (handler == null) {
091: handler = defaultHandler;
092: }
093: try {
094: handler.endElement(context, component, uri, localName,
095: qName);
096: if (components.empty()) {
097: handler.endDocument(context, component);
098: }
099: } catch (IOException e) {
100: throw new SAXException(e);
101: }
102: }
103:
104: protected XmlTagHandler getCurrentHandler() {
105: ComponentWrapper component = getCurrentComponent();
106:
107: if (component == null) {
108: return defaultHandler;
109: }
110: return component.getHandler();
111: }
112:
113: public ComponentWrapper getCurrentComponent() {
114: ComponentWrapper component = null;
115: if (!components.isEmpty()) {
116: component = (ComponentWrapper) components.peek();
117: } else {
118: component = new ComponentWrapper(null, rootView, null);
119: }
120: return component;
121: }
122:
123: public XmlTagFactory getFactory() {
124: return factory;
125: }
126:
127: public void setFactory(XmlTagFactory factory) {
128: this .factory = factory;
129: }
130:
131: public FacesContext getContext() {
132: return context;
133: }
134:
135: public void setContext(FacesContext context) {
136: this.context = context;
137: }
138:
139: }
|