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: ContentElementImpl.java 7492 2007-05-21 06:56:38Z shou $
024: */
025: package com.bostechcorp.cbesb.common.mdl.impl;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.jdom.Attribute;
030: import org.jdom.Element;
031:
032: import com.bostechcorp.cbesb.common.mdl.IContentElement;
033: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
034: import com.bostechcorp.cbesb.common.mdl.IFormatChildAttributes;
035: import com.bostechcorp.cbesb.common.mdl.MDLDocConstants;
036:
037: /**
038: * Represents a child element.
039: *
040: */
041: public class ContentElementImpl extends ContentNodeImpl implements
042: IContentElement {
043:
044: /**
045: * The definition of the child element.
046: */
047: private IElementDefinition elementDefinition;
048:
049: /**
050: * These are the child level format attributes.
051: */
052: private IFormatChildAttributes attributes;
053:
054: /**
055: * This tag references a global element definition, then the ref attribute specifies the name of that element.
056: */
057: private QName ref;
058:
059: private QName elementDef;
060:
061: private String elementDefName;
062:
063: /**
064: * Constructor.
065: */
066: public ContentElementImpl() {
067: elementDefinition = null;
068: attributes = null;
069: ref = null;
070: elementDef = null;
071: elementDefName = null;
072: }
073:
074: /* (non-Javadoc)
075: * @see com.bostechcorp.cbesb.legacydata.mdl.ContentElement#getElementDefinition()
076: */
077: public IElementDefinition getElementDefinition() {
078: return elementDefinition;
079: }
080:
081: /* (non-Javadoc)
082: * @see com.bostechcorp.cbesb.legacydata.mdl.ContentElement#setElementDefinition(com.bostechcorp.cbesb.legacydata.mdl.ElementDefinition)
083: */
084: public void setElementDefinition(
085: IElementDefinition elementDefinition) {
086: this .elementDefinition = elementDefinition;
087: }
088:
089: /* (non-Javadoc)
090: * @see com.bostechcorp.cbesb.legacydata.mdl.ContentElement#getFormatChildAttributes()
091: */
092: public IFormatChildAttributes getFormatChildAttributes() {
093: return attributes;
094: }
095:
096: /* (non-Javadoc)
097: * @see com.bostechcorp.cbesb.legacydata.mdl.ContentElement#setFormatChildAttributes(com.bostechcorp.cbesb.legacydata.mdl.FormatChildAttributes)
098: */
099: public void setFormatChildAttributes(
100: IFormatChildAttributes attributes) {
101: this .attributes = attributes;
102: }
103:
104: public QName getElementDef() {
105: return elementDef;
106: }
107:
108: public void setElementDef(QName elementDef) {
109: this .elementDef = elementDef;
110: }
111:
112: /**
113: * @return the elementDefName
114: */
115: public String getElementDefName() {
116: return elementDefName;
117: }
118:
119: /**
120: * @param elementDefName the elementDefName to set
121: */
122: public void setElementDefName(String elementDefName) {
123: this .elementDefName = elementDefName;
124: }
125:
126: public QName getRef() {
127: return ref;
128: }
129:
130: public void setRef(QName ref) {
131: this .ref = ref;
132: }
133:
134: public IElementDefinition getResolvedElementDef() {
135: if (elementDefinition != null) {
136: return elementDefinition;
137: }
138: if (ref != null) {
139: return mdlDocument.getElementDefinition(ref
140: .getNamespaceURI(), ref.getLocalPart());
141: }
142: if (elementDef != null) {
143: ElementDefinitionImpl elemDef = new ElementDefinitionImpl();
144: elemDef.setName(elementDefName);
145: elemDef.setNamespaceURI(mdlDocument.getTargetNamespace());
146: elemDef.setMDLDocument(mdlDocument);
147: IElementDefinition baseDef = mdlDocument
148: .getElementDefinition(elementDef.getNamespaceURI(),
149: elementDef.getLocalPart());
150: //Copy the attributes of the base definition to the new definition
151: elemDef.setDatatype(baseDef.getDatatype());
152: elemDef.setDescription(baseDef.getDescription());
153: elemDef.setFormatDefinition(baseDef.getFormatDefinition());
154: elemDef.setGlobal(false);
155: for (int i = 0; i < baseDef.getChildCount(); i++) {
156: elemDef.appendChild(baseDef.getChildAtIndex(i));
157: }
158: //Set the new definition to this content element
159: //for faster access the next time its used.
160: elementDefinition = elemDef;
161: return elemDef;
162: }
163: return null;
164: }
165:
166: public void serializeToJDom(MDLSerializerUtil serializerUtil,
167: Element mdlParent) {
168: Element elemDef = serializerUtil
169: .createElement(MDLDocConstants.MDL_ELEMENT);
170: mdlParent.addContent(elemDef);
171:
172: if (elementDefinition != null && elementDef == null) {
173: ((ElementDefinitionImpl) elementDefinition).populateJDom(
174: serializerUtil, elemDef);
175:
176: } else if (ref != null) {
177: String attrValue = serializerUtil
178: .getAttributeRefValueFromQName(ref);
179: Attribute attrRef = serializerUtil.createAttribute(
180: MDLDocConstants.MDL_CONTENT_REF, attrValue);
181: elemDef.setAttribute(attrRef);
182:
183: } else if (elementDef != null) {
184: Attribute attrName = serializerUtil.createAttribute(
185: MDLDocConstants.MDL_NAME, elementDefName);
186: elemDef.setAttribute(attrName);
187:
188: String attrValue = serializerUtil
189: .getAttributeRefValueFromQName(elementDef);
190: Attribute attrElementDef = serializerUtil.createAttribute(
191: MDLDocConstants.MDL_CONTENT_ELEMENT_DEF, attrValue);
192: elemDef.setAttribute(attrElementDef);
193: }
194:
195: if (attributes != null) {
196: ((FormatChildAttributesImpl) attributes)
197: .populateJDomAttributes(serializerUtil, elemDef);
198: }
199:
200: populateJDomAttributes(serializerUtil, elemDef);
201:
202: }
203: }
|