001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: SerializerBase.java 9486 2007-10-09 15:28:44Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.parser;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Node;
030:
031: import com.bostechcorp.cbesb.common.i18n.Message;
032: import com.bostechcorp.cbesb.common.i18n.Messages;
033: import com.bostechcorp.cbesb.common.mdl.IContentElement;
034: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
035: import com.bostechcorp.cbesb.common.mdl.IContentNode;
036: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
037: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
038:
039: public class SerializerBase {
040:
041: protected transient Log log = LogFactory
042: .getLog(SerializerBase.class);
043:
044: protected void validateElementName(Element element,
045: IElementDefinition elemDef) throws ParserException {
046: String ElementNamespace = element.getNamespaceURI();
047: if (ElementNamespace == null) {
048: ElementNamespace = "";
049: }
050: String ElementName = element.getLocalName();
051: if (ElementName == null) {
052: ElementName = element.getNodeName();
053: }
054: String defNS = elemDef.getNamespaceURI();
055: if (defNS == null) {
056: defNS = "";
057: }
058:
059: if (!ElementNamespace.equals(defNS)
060: || !ElementName.equals(elemDef.getName())) {
061: Object[] args = new Object[] { elemDef.getNamespaceURI(),
062: elemDef.getName(), element.getNamespaceURI(),
063: element.getLocalName() };
064: throw new ParserException(new Message(
065: Messages.NAME_VALIDATION_ERROR, args).getMessage());
066: }
067: }
068:
069: public boolean validateElementName(Element element,
070: IContentNode contentNode, IElementDefinition parentElemDef) {
071: String ElementNamespace = element.getNamespaceURI();
072: if (ElementNamespace == null) {
073: ElementNamespace = "";
074: }
075: String ElementName = element.getLocalName();
076: if (ElementName == null) {
077: ElementName = element.getNodeName();
078: }
079:
080: if (contentNode instanceof IContentElement) {
081: IMDLDocument mdlDoc = parentElemDef.getMDLDocument();
082: IElementDefinition elemDef = ((IContentElement) contentNode)
083: .getResolvedElementDef();
084: // resolveChildElementDefinition((IContentElement) contentNode, mdlDoc);
085: String defNS = elemDef.getNamespaceURI();
086: if (defNS == null) {
087: defNS = "";
088: }
089: if (!ElementNamespace.equals(defNS)
090: || !ElementName.equals(elemDef.getName())) {
091: return false;
092:
093: }
094: } else if (contentNode instanceof IContentGroup) {
095: IContentGroup contentGroup = (IContentGroup) contentNode;
096: String defNS = contentGroup.getNamespaceURI();
097: if (defNS == null) {
098: defNS = "";
099: }
100: if (!ElementNamespace.equals(defNS)
101: || !ElementName.equals(contentGroup.getName())) {
102: return false;
103: }
104: }
105:
106: return true;
107: }
108:
109: /**
110: * @param element
111: * @return
112: */
113: public static Element getFirstChildElement(Element element) {
114: Node child = element.getFirstChild();
115: while (child != null && !(child instanceof Element)) {
116: child = child.getNextSibling();
117: }
118: return (Element) child;
119: }
120:
121: /**
122: * @param element
123: * @return
124: */
125: public static Element getNextSiblingElement(Element element) {
126: Node sibling = element.getNextSibling();
127: while (sibling != null && !(sibling instanceof Element)) {
128: sibling = sibling.getNextSibling();
129: }
130: return (Element) sibling;
131: }
132:
133: }
|