001: package org.objectweb.celtix.bus.bindings.xml;
002:
003: import java.io.*;
004: import javax.xml.namespace.QName;
005: import org.w3c.dom.*;
006: import org.objectweb.celtix.helpers.XMLUtils;
007:
008: public final class XMLMessageFactory {
009:
010: private XMLUtils xmlUtils = new XMLUtils();
011:
012: private XMLMessageFactory() {
013: }
014:
015: public XMLMessage createMessage() throws XMLBindingException {
016: return new XMLMessage();
017: }
018:
019: public XMLMessage createMessage(InputStream in)
020: throws XMLBindingException {
021: XMLMessage message = new XMLMessage();
022: try {
023: Document doc = xmlUtils.parse(in);
024: if (doc != null) {
025: message.setRoot(doc);
026: }
027:
028: buildFaultMessage(message, doc);
029: } catch (Exception exp) {
030: exp.printStackTrace();
031: throw new XMLBindingException(
032: "Create XML binding message exception:", exp);
033: }
034: return message;
035: }
036:
037: private void buildFaultMessage(XMLMessage message, Document doc) {
038: assert doc != null;
039: if (doc.hasChildNodes()) {
040: doBuild(message, doc);
041: }
042: }
043:
044: private void doBuild(XMLMessage message, Node node) {
045: NodeList children = node.getChildNodes();
046: for (int i = 0; i < children.getLength(); i++) {
047: Node child = children.item(i);
048: if (isFaultNode(child)) {
049: addFault(message, child);
050: }
051:
052: if (isFaultStringNode(child)) {
053: addFaultString(message, child);
054: }
055:
056: if (isDetailNode(child)) {
057: addDetail(message, child);
058: }
059:
060: if (child.hasChildNodes()) {
061: doBuild(message, child);
062: }
063: }
064: }
065:
066: private void addFaultString(XMLMessage message, Node child) {
067: XMLFault xmlFault = message.getFault();
068: if (xmlFault == null) {
069: xmlFault = message.addFault();
070: }
071: xmlFault.setFaultString(child.getTextContent());
072: }
073:
074: private boolean isFaultStringNode(Node child) {
075: QName name = getQNameFromNode(child);
076: return XMLConstants.XML_FAULT_STRING.equals(name);
077: }
078:
079: private void addDetail(XMLMessage message, Node child) {
080: XMLFault xmlFault = message.getFault();
081: if (xmlFault == null) {
082: xmlFault = message.addFault();
083: }
084:
085: xmlFault.setFaultDetail(child);
086: }
087:
088: private boolean isDetailNode(Node child) {
089: QName name = getQNameFromNode(child);
090: return XMLConstants.XML_FAULT_DETAIL.equals(name);
091: }
092:
093: private void addFault(XMLMessage message, Node child) {
094: XMLFault xmlFault = new XMLFault();
095: xmlFault.setFaultRoot(child);
096: message.setFault(xmlFault);
097: }
098:
099: private boolean isFaultNode(Node child) {
100: QName name = getQNameFromNode(child);
101: return XMLConstants.XML_FAULT_ROOT.equals(name);
102: }
103:
104: private QName getQNameFromNode(Node node) {
105: String localName = node.getLocalName();
106: String namespaceURI = node.getNamespaceURI();
107: return localName == null ? null : new QName(namespaceURI,
108: localName);
109: }
110:
111: public static XMLMessageFactory newInstance() {
112: return new XMLMessageFactory();
113: }
114: }
|