001: /*
002: * File: MOSHandler.java
003: * Project: jMOS, com.aranova.java.jmos.handler
004: * Revision: 0.9 - Inicial
005: * Date: 05-oct-2005 10:31:54
006: *
007: * Copyright (C) Aragón Innovación Tecnológica S.L.L.
008: * All rights reserved.
009: *
010: * This software is distributed under the terms of the Aranova License version 1.0.
011: * See the terms of the Aranova License in the documentation provided with this software.
012: */
013:
014: package com.aranova.java.jmos.handler;
015:
016: import java.util.HashMap;
017: import java.util.Map;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.xml.sax.Attributes;
022: import org.xml.sax.SAXException;
023:
024: import com.aranova.java.jmos.annotations.MOSMessage;
025: import com.aranova.java.jmos.io.XMLInputStream;
026: import com.aranova.java.jmos.messages.InfoMessage;
027: import com.aranova.java.jmos.messages.Message;
028:
029: /**
030: * Class for handler SAX2 events for MOS.
031: *
032: * @author <a href="http://www.aranova.net/contactar/">Daniel Sánchez</a>
033: * @version 0.9.1
034: * @since 0.9
035: */
036: public class MOSHandler extends BaseHandler {
037: private static final Log _log = LogFactory.getLog(MOSHandler.class);
038: private static final Map<String, InfoMessage> _typeMessages;
039: private String _qName;
040: private Message _message;
041: private StringBuilder _xmlBuilder;
042: private int _nivelInicial;
043: private InfoMessage _infoMessage;
044: private final Map<String, Object> _headerInfo;
045:
046: static {
047: _typeMessages = Message.getMessages();
048: }
049:
050: /**
051: * @param XMLis
052: */
053: public MOSHandler(final XMLInputStream XMLis) {
054: super (XMLis);
055: _headerInfo = new HashMap<String, Object>();
056: }
057:
058: /* (non-Javadoc)
059: * @see com.aranova.java.jmos.handler.BaseHandler#characters(char[], int, int)
060: */
061: @Override
062: public void characters(final char[] ch, final int start,
063: final int length) throws SAXException {
064: super .characters(ch, start, length);
065: final String value = new String(ch, start, length);
066: if (_xmlBuilder != null) {
067: _xmlBuilder.append(value);
068: } else if (getNivel() == 2) {
069: _headerInfo.put(_qName, value);
070: } else if (_message != null && getNivel() >= 3) {
071: _message.set(_qName, value);
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see com.aranova.java.jmos.handler.BaseHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
077: */
078: @Override
079: public void endElement(final String uri, final String localName,
080: final String qName) throws SAXException {
081: if (_xmlBuilder != null) {
082: if (_qName == qName && _nivelInicial == getNivel()) {
083: _message.set(_qName, _xmlBuilder.toString());
084: _xmlBuilder = null;
085: } else {
086: _xmlBuilder.append("</" + qName + ">");
087: }
088: } else if (_typeMessages.containsKey(qName)) {
089: if (_message.getMessagePrevio() != null) {
090: Message previo = _message.getMessagePrevio();
091: previo.set(qName, _message);
092: _message = previo;
093: _infoMessage = _typeMessages.get(_message.getName());
094: }
095: }
096: super .endElement(uri, localName, qName);
097: }
098:
099: /* (non-Javadoc)
100: * @see com.aranova.java.jmos.handler.BaseHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
101: */
102: @Override
103: public void startElement(final String uri, final String localName,
104: final String qName, final Attributes attributes)
105: throws SAXException {
106: super .startElement(uri, localName, qName, attributes);
107: if (_xmlBuilder != null) {
108: _xmlBuilder.append("<" + qName + ">");
109: } else {
110: _qName = qName;
111: if (_infoMessage != null && _infoMessage.isXMLField(qName)) {
112: _xmlBuilder = new StringBuilder();
113: _nivelInicial = getNivel();
114: } else if (_typeMessages.containsKey(qName)) {
115: createMessage(_typeMessages.get(qName).getMessage());
116: for (int i = 0; i < attributes.getLength(); i++) {
117: _message.set(attributes.getQName(i), attributes
118: .getValue(i));
119: }
120: }
121: }
122: }
123:
124: private void createMessage(final MOSMessage mos)
125: throws SAXException {
126: try {
127: StringBuilder clazz = new StringBuilder(Message.class
128: .getPackage().getName());
129: clazz.append(".profile");
130: if (mos.profile() != -1) {
131: clazz.append(mos.profile());
132: }
133: clazz.append(".");
134: clazz.append(mos.name());
135: Message message = (Message) Class.forName(clazz.toString())
136: .newInstance();
137: if (_message == null) {
138: _log.info("Procesando mensaje: " + message);
139: for (final String name : _headerInfo.keySet()) {
140: message.set(name, _headerInfo.get(name));
141: }
142: } else {
143: message.setMessagePrevio(_message);
144: }
145: _infoMessage = _typeMessages.get(mos.name());
146: _message = message;
147: } catch (ClassNotFoundException e) {
148: _log.warn("Message " + mos.name() + " no implementado", e);
149: throw new SAXException("createMessage", e);
150: } catch (InstantiationException e) {
151: _log.warn("Error en el message " + mos.name(), e);
152: throw new SAXException("createMessage", e);
153: } catch (IllegalAccessException e) {
154: _log.warn("Error en el message " + mos.name(), e);
155: throw new SAXException("createMessage", e);
156: }
157: }
158:
159: @Override
160: public final void startDocument() throws SAXException {
161: super .startDocument();
162: _headerInfo.clear();
163: _message = null;
164: _infoMessage = null;
165: }
166:
167: /**
168: * @return InfoMessage
169: */
170: public Message getMessage() {
171: return _message;
172: }
173:
174: @Override
175: public void endDocument() throws SAXException {
176: if (_message == null) {
177: throw new SAXException("createMessage");
178: }
179: super.endDocument();
180: }
181:
182: }
|