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
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) 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
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: ElementDefinitionImpl.java 7485 2007-05-21 02:14:15Z shou $
024: */
025: package com.bostechcorp.cbesb.common.mdl.impl;
026:
027: import java.util.Iterator;
028: import java.util.Vector;
029:
030: import javax.xml.namespace.QName;
031:
032: import org.jdom.Attribute;
033: import org.jdom.Element;
034:
035: import com.bostechcorp.cbesb.common.mdl.IContentElement;
036: import com.bostechcorp.cbesb.common.mdl.IContentNode;
037: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
038: import com.bostechcorp.cbesb.common.mdl.IFormatDefinition;
039: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
040: import com.bostechcorp.cbesb.common.mdl.IVariableFormatDefinition;
041: import com.bostechcorp.cbesb.common.mdl.MDLDocConstants;
042:
043: /**
044: * Represents an instance of an "element" tag in an MDL document.
045: *
046: */
047: public class ElementDefinitionImpl implements IElementDefinition {
048:
049: /**
050: * The name of the message or element.
051: */
052: private String name;
053:
054: /**
055: * The namespace URI.
056: */
057: private String namespaceURI;
058:
059: /**
060: * Judge the element is global or not.
061: */
062: private boolean isGlobal;
063:
064: /**
065: * If this element definition is a leaf node (it contains no child elements),
066: * then a datatype must be specified.
067: */
068: private QName datatype;
069:
070: /**
071: * The format definition of an element or message.
072: */
073: private IFormatDefinition formatDefinition;
074:
075: /**
076: * The contents of element or message.
077: */
078: private Vector<IContentNode> content;
079:
080: /**
081: * The description of element or message.
082: */
083: private String description;
084:
085: /**
086: * The current MDL document object.
087: */
088: private IMDLDocument mdlDocument;
089:
090: /**
091: * Constructor.
092: */
093: public ElementDefinitionImpl() {
094: name = null;
095: namespaceURI = null;
096: isGlobal = false;
097: datatype = null;
098: formatDefinition = null;
099: content = new Vector<IContentNode>();
100: description = null;
101: mdlDocument = null;
102: }
103:
104: /* (non-Javadoc)
105: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getName()
106: */
107: public String getName() {
108: return name;
109: }
110:
111: /* (non-Javadoc)
112: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setName(java.lang.String)
113: */
114: public void setName(String name) {
115:
116: this .name = name;
117: }
118:
119: /* (non-Javadoc)
120: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getNamespaceURI()
121: */
122: public String getNamespaceURI() {
123:
124: return namespaceURI;
125: }
126:
127: /* (non-Javadoc)
128: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setNamespaceURI(java.lang.String)
129: */
130: public void setNamespaceURI(String namespaceURI) {
131:
132: this .namespaceURI = namespaceURI;
133: }
134:
135: /* (non-Javadoc)
136: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#isGlobal()
137: */
138: public boolean isGlobal() {
139: return isGlobal;
140: }
141:
142: /* (non-Javadoc)
143: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setGlobal(boolean)
144: */
145: public void setGlobal(boolean isGlobal) {
146: this .isGlobal = isGlobal;
147: }
148:
149: /* (non-Javadoc)
150: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getDatatype()
151: */
152: public QName getDatatype() {
153:
154: return datatype;
155: }
156:
157: /* (non-Javadoc)
158: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setDatatype(java.lang.String, java.lang.String)
159: */
160: public void setDatatype(QName datatype) {
161: this .datatype = datatype;
162: }
163:
164: /* (non-Javadoc)
165: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getFormatDefinition()
166: */
167: public IFormatDefinition getFormatDefinition() {
168:
169: return formatDefinition;
170: }
171:
172: /* (non-Javadoc)
173: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setFormatDefinition(com.bostechcorp.cbesb.common.mdl.IFormatDefinition)
174: */
175: public void setFormatDefinition(IFormatDefinition formatDefinition) {
176:
177: this .formatDefinition = formatDefinition;
178: }
179:
180: /* (non-Javadoc)
181: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getChildCount()
182: */
183: public int getChildCount() {
184: return content.size();
185: }
186:
187: /* (non-Javadoc)
188: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getChildAtIndex(int)
189: */
190: public IContentNode getChildAtIndex(int index) {
191: if (index >= 0 && index < content.size()) {
192: return (IContentNode) content.get(index);
193: } else {
194: return null;
195: }
196: }
197:
198: /* (non-Javadoc)
199: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getChildren()
200: */
201: public IContentNode[] getChildren() {
202: IContentNode[] contentNodes = new IContentNode[content.size()];
203:
204: Iterator iterator = content.iterator();
205: int index = 0;
206: while (iterator.hasNext()) {
207: contentNodes[index] = (IContentNode) iterator.next();
208: index++;
209: }
210: return contentNodes;
211: }
212:
213: /* (non-Javadoc)
214: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#appendChild(com.bostechcorp.cbesb.common.mdl.IContentNode)
215: */
216: public void appendChild(IContentNode child) {
217: content.add(child);
218: }
219:
220: /* (non-Javadoc)
221: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#insertChildAtIndex(com.bostechcorp.cbesb.common.mdl.IContentNode, int)
222: */
223: public void insertChildAtIndex(IContentNode child, int index) {
224: content.insertElementAt(child, index);
225: }
226:
227: /* (non-Javadoc)
228: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#removeChildAtIndex(int)
229: */
230: public void removeChildAtIndex(int index) {
231: content.remove(index);
232: }
233:
234: /* (non-Javadoc)
235: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getDescription()
236: */
237: public String getDescription() {
238: return description;
239: }
240:
241: /* (non-Javadoc)
242: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setDescription(java.lang.String)
243: */
244: public void setDescription(String description) {
245: this .description = description;
246: }
247:
248: /* (non-Javadoc)
249: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#getMDLDocument()
250: */
251: public IMDLDocument getMDLDocument() {
252: return mdlDocument;
253: }
254:
255: /* (non-Javadoc)
256: * @see com.bostechcorp.cbesb.common.mdl.IElementDefinition#setMDLDocument(com.bostechcorp.cbesb.common.mdl.IMDLDocument)
257: */
258: public void setMDLDocument(IMDLDocument document) {
259: mdlDocument = document;
260: }
261:
262: public boolean isIdMethodByTag() {
263: //If this element is Variable and IDMethod = Tag, then return true
264: if (getFormatDefinition() instanceof IVariableFormatDefinition) {
265: IVariableFormatDefinition varFormatDef = (IVariableFormatDefinition) getFormatDefinition();
266: if (varFormatDef.getIDMethod() == IVariableFormatDefinition.ID_METHOD_TAG) {
267: return true;
268: }
269: }
270: //Recursively check children for IDMethod = Tag
271: IContentNode[] children = getChildren();
272: for (int i = 0; i < children.length; i++) {
273: //Since this element is not using IDMethod=Tag,
274: //we know there are no IContentGroup children
275: IContentElement childContentElem = (IContentElement) children[i];
276: IElementDefinition childDef = childContentElem
277: .getElementDefinition();
278: if (childDef.isIdMethodByTag()) {
279: return true;
280: }
281: }
282: //None of the children are using IDMethod = tag
283: return false;
284: }
285:
286: public void serializeToJDom(MDLSerializerUtil serializerUtil,
287: Element mdlParent) {
288: Element elemMessage = serializerUtil
289: .createElement(MDLDocConstants.MDL_ELEMENT);
290: populateJDom(serializerUtil, elemMessage);
291: mdlParent.addContent(elemMessage);
292: }
293:
294: protected void populateJDom(MDLSerializerUtil serializerUtil,
295: Element elemDef) {
296: if (name != null && !name.equals("")) {
297: Attribute attrName = serializerUtil.createAttribute(
298: MDLDocConstants.MDL_NAME, name);
299: elemDef.setAttribute(attrName);
300: }
301: if (description != null && !description.equals("")) {
302: Element elemDescription = serializerUtil
303: .createElement(MDLDocConstants.MDL_DESCRIPTION);
304: elemDescription.addContent(description);
305: elemDef.addContent(elemDescription);
306: }
307:
308: if (formatDefinition != null) {
309: //This is not a leaf node
310: ((FormatDefinitionImpl) formatDefinition)
311: .populateJDomElementDefAttributes(serializerUtil,
312: elemDef);
313:
314: if (getChildCount() > 0) {
315: Element elemContent = serializerUtil
316: .createElement(MDLDocConstants.MDL_CONTENT);
317: elemDef.addContent(elemContent);
318: ((FormatDefinitionImpl) formatDefinition)
319: .populateJDomContentAttributes(serializerUtil,
320: elemContent);
321:
322: for (int i = 0; i < getChildCount(); i++) {
323: ((ContentNodeImpl) getChildAtIndex(i))
324: .serializeToJDom(serializerUtil,
325: elemContent);
326: }
327: }
328:
329: } else {
330: //This is a leaf node, it should have a datatype
331: String attrValue = serializerUtil
332: .getAttributeRefValueFromQName(datatype);
333: Attribute attrDatatype = serializerUtil.createAttribute(
334: MDLDocConstants.MDL_DATATYPE, attrValue);
335: elemDef.setAttribute(attrDatatype);
336: }
337: }
338: }
|