001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql.xml;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.xml.transform.TransformerException;
030:
031: import org.apache.xpath.XPathAPI;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.traversal.NodeIterator;
036:
037: import biz.hammurapi.config.ConfigurationException;
038: import biz.hammurapi.config.Context;
039: import biz.hammurapi.config.DomConfigFactory;
040: import biz.hammurapi.sql.SQLProcessor;
041: import biz.hammurapi.xml.dom.AbstractDomObject;
042:
043: /**
044: * @author Pavel Vlasov
045: * @version $Revision: 1.2 $
046: */
047: public class CompositeCommand extends AbstractDomObject implements
048: Command {
049: private Collection commands = new ArrayList();
050:
051: public void addCommand(Command command) {
052: commands.add(command);
053: }
054:
055: private SQLProcessor processor;
056:
057: public Document execute(Document document, SQLProcessor processor,
058: Context context) {
059: Element element = document.createElement("sql2xml");
060: document.appendChild(element);
061: try {
062: execute(element, processor, context);
063: } catch (SQL2XMLException e) {
064: log(e);
065: Element errorElement = element.getOwnerDocument()
066: .createElement("error");
067: toDom(e, errorElement);
068: element.setAttribute("result", "error");
069: element.appendChild(errorElement);
070: }
071: return document;
072: }
073:
074: /**
075: * Override for proper logging
076: * @param e
077: */
078: public void log(SQL2XMLException e) {
079: e.printStackTrace();
080: }
081:
082: public void execute(Element holder, SQLProcessor processor,
083: Context context) throws SQL2XMLException {
084: Iterator it = commands.iterator();
085: while (it.hasNext()) {
086: ((Command) it.next())
087: .execute(holder, this .processor == null ? processor
088: : this .processor, context);
089: }
090: }
091:
092: public void configure(Node configNode, Context context)
093: throws ConfigurationException {
094: try {
095: // TODO Handle <transaction> and <update> here as well
096: NodeIterator nit = XPathAPI.selectNodeIterator(configNode,
097: "query|update|transaction|message|java");
098: Node n;
099: while ((n = nit.nextNode()) != null) {
100: if ("query".equals(n.getNodeName())) {
101: Select select = new Select();
102: select.configure(n, context);
103: addCommand(select);
104: } else if ("update".equals(n.getNodeName())) {
105: Update update = new Update();
106: update.configure(n, context);
107: addCommand(update);
108: } else if ("transaction".equals(n.getNodeName())) {
109: Transaction transaction = new Transaction();
110: transaction.configure(n, context);
111: addCommand(transaction);
112: } else if ("message".equals(n.getNodeName())) {
113: Message message = new Message();
114: message.configure(n, context);
115: addCommand(message);
116: } else if ("java".equals(n.getNodeName())) {
117: DomConfigFactory factory = new DomConfigFactory(
118: this .getClass().getClassLoader(), context);
119: factory.create(n);
120: }
121:
122: processor = SQLCommand.initProcessor(configNode);
123: }
124: } catch (TransformerException e) {
125: throw new ConfigurationException(e);
126: }
127: }
128:
129: public void toDom(Throwable throwable, Element holder) {
130: holder.setAttribute("type", throwable.getClass().getName());
131: if (throwable.getMessage() != null) {
132: holder.setAttribute("message", throwable.getMessage());
133: }
134:
135: Document ownerDocument = holder.getOwnerDocument();
136: StackTraceElement[] frames = throwable.getStackTrace();
137: for (int i = 0; i < frames.length; i++) {
138: Element frameElement = ownerDocument.createElement("frame");
139: holder.appendChild(frameElement);
140: frameElement.appendChild(ownerDocument
141: .createTextNode(frames[i].toString()));
142: }
143:
144: if (throwable.getCause() != null) {
145: Element causeElement = ownerDocument.createElement("cause");
146: holder.appendChild(causeElement);
147: toDom(throwable.getCause(), causeElement);
148: }
149: }
150: }
|