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: XmlFormatModelMDLImpl.java 4345 2007-01-12 09:43:12Z lzheng $
023: */
024: package com.bostechcorp.cbesb.common.util.xfm.impl;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import javax.xml.namespace.QName;
030:
031: import com.bostechcorp.cbesb.common.mdl.IContentElement;
032: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
033: import com.bostechcorp.cbesb.common.mdl.IContentNode;
034: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
035: import com.bostechcorp.cbesb.common.mdl.IFixedChildAttributes;
036: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
037: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
038: import com.bostechcorp.cbesb.common.mdl.IVariableChildAttributes;
039: import com.bostechcorp.cbesb.common.mdl.IVariableFormatDefinition;
040: import com.bostechcorp.cbesb.common.mdl.util.FormatDefUtil;
041: import com.bostechcorp.cbesb.common.util.xfm.IXmlFormatModel;
042: import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelNode;
043:
044: /**
045: * Implement the IXMLFormatModel class for MDL document.
046: *XmlFormatModelMDLImpl don't need to handle elementFormDefault and attributeFormDefault because we already handle it in MDL api
047: * @version 1.0.0
048: */
049: public class XmlFormatModelMDLImpl implements IXmlFormatModel {
050:
051: /**
052: * This is the MDL document.
053: */
054: private IMDLDocument mdlDoc;
055:
056: /**
057: * This is the top level Node.
058: */
059: private XmlFormatModelNode topLevelNode;
060:
061: /**
062: * Used to store the content of refNode.
063: */
064: private Map<String, XmlFormatModelNode> refMap = new HashMap<String, XmlFormatModelNode>();
065:
066: /* (non-Javadoc)
067: * @see com.bostechcorp.cbesb.common.util.IXmlFormatModel#getTopLevelNode()
068: */
069: public XmlFormatModelNode getTopLevelNode() {
070: return topLevelNode;
071: }
072:
073: public XmlFormatModelMDLImpl(String format, String formatSpec) {
074: mdlDoc = FormatDefUtil.getMdlDocFromFormatSpec(format,
075: formatSpec);
076: topLevelNode = analyzeMDL(mdlDoc);
077:
078: }
079:
080: // /**
081: // * Constructor with a parameter which is specificed the MDL filename.
082: // *
083: // * @param filename The MDL filename.
084: // */
085: // public XmlFormatModelMDLImpl(String filename) {
086: // mdlFilename = filename;
087: // // MDLDocumentFactory factory = new MDLDocumentFactory();
088: // // Parser the MDL document.
089: //// mdlDoc = factory.getMDLDocument(mdlFilename);
090: // try {
091: // // mdlDoc = factory.getMDLDocument(EsbPathHelper.getFullPathForDef(mdlFilename), null);
092: // mdlDoc = FormatDefUtil.getMdlDocFromPath(EsbPathHelper.getFullPathForDef(mdlFilename));
093: //
094: //
095: // } catch (Exception e) {
096: // e.printStackTrace();
097: // }
098: // topLevelNode = analyzeMDL(mdlDoc);
099: // }
100:
101: // public XmlFormatModelMDLImpl(InputStream inputStream, String zipLocation) {
102: // // MDLDocumentFactory factory = new MDLDocumentFactory();
103: //// mdlDoc = factory.getMDLDocument(inputStream, zipLocation);
104: // topLevelNode = analyzeMDL(mdlDoc);
105: // }
106:
107: /* (non-Javadoc)
108: * @see com.bostechcorp.cbesb.common.util.IXmlFormatModel#getTargetNamespace()
109: */
110: public String getTargetNamespace() {
111: return mdlDoc.getTargetNamespace();
112: }
113:
114: /* (non-Javadoc)
115: * @see com.bostechcorp.cbesb.common.util.IXmlFormatModel#getRootElement(java.lang.String)
116: */
117: public XmlFormatModelNode getRootElement(String globalElementName) {
118: return topLevelNode.getNode(globalElementName);
119: }
120:
121: /**
122: * Analyze the MDL document and return the XmlFormatModelNode.
123: *
124: * @param mdlDocument IMDLDocument object.
125: * @return The XML format model node.
126: */
127: private XmlFormatModelNode analyzeMDL(IMDLDocument mdlDocument) {
128: XmlFormatModelNode topNode = new XmlFormatModelNode();
129: if (mdlDocument != null) {
130: // set all of the message definitions to the topNode.
131: IMessageDefinition messageDefinition[] = mdlDocument
132: .getAllMessageDefinitions();
133: for (int i = 0; i < messageDefinition.length; i++) {
134: XmlFormatModelNode newNode = new XmlFormatModelNode(
135: messageDefinition[i].getName(),
136: messageDefinition[i].getNamespaceURI());
137: newNode.setCanBeRootElement(true);
138: topNode.addElement(newNode);
139: newNode.getAttributeList().add(new QName(null, "name"));
140: if (messageDefinition[i].getFormatDefinition() != null) {
141: if (messageDefinition[i].getFormatDefinition()
142: .getName() != null) {
143: newNode.getAttributeList().add(
144: new QName(null, "format"));
145: }
146: if ("variable".equals(messageDefinition[i]
147: .getFormatDefinition().getName())) {
148: if (!""
149: .equals(((IVariableFormatDefinition) messageDefinition[i]
150: .getFormatDefinition())
151: .getDelimiter())) {
152: newNode.getAttributeList().add(
153: new QName(null, "delimiter"));
154: }
155: if (0 != ((IVariableFormatDefinition) messageDefinition[i]
156: .getFormatDefinition()).getEscapeChar()) {
157: newNode.getAttributeList().add(
158: new QName(null, "escapeChar"));
159: }
160: if (!""
161: .equals(((IVariableFormatDefinition) messageDefinition[i]
162: .getFormatDefinition())
163: .getRepeatDelimiter())) {
164: newNode.getAttributeList().add(
165: new QName(null, "repeatDelimiter"));
166: }
167: if (0 != ((IVariableFormatDefinition) messageDefinition[i]
168: .getFormatDefinition()).getQuoteChar()) {
169: newNode.getAttributeList().add(
170: new QName(null, "quoteChar"));
171: }
172: }
173: }
174:
175: if (messageDefinition[i].getDatatype() != null
176: && !"".equals(messageDefinition[i]
177: .getDatatype())) {
178: newNode.getAttributeList().add(
179: new QName(null, "datatype"));
180: }
181: if (messageDefinition[i].getChildCount() != 0) {
182: processMessageChild(newNode, messageDefinition[i]);
183: }
184: }
185:
186: // set all of the global element definitions to the topNode.
187: IElementDefinition elementDefinition[] = mdlDocument
188: .getAllElementDefinitions();
189: for (int i = 0; i < elementDefinition.length; i++) {
190: processGlobalElement(topNode, elementDefinition[i],
191: null);
192: }
193: }
194: return topNode;
195: }
196:
197: /**
198: * Process the message children and convert them to the XML format model node.
199: *
200: * @param node The XML formt model node.
201: * @param messageDefinition The MessageDefinition object.
202: */
203: private void processMessageChild(XmlFormatModelNode node,
204: IMessageDefinition messageDefinition) {
205: for (int i = 0; i < messageDefinition.getChildCount(); i++) {
206: IContentNode child = messageDefinition.getChildAtIndex(i);
207: if (child instanceof IContentElement) {
208: if (((IContentElement) child).getRef() != null) {
209: processRefChild(node, (IContentElement) child);
210: } else {
211: processElementChild(node,
212: (IElementDefinition) messageDefinition, i);
213: }
214:
215: } else if (child instanceof IContentGroup) {
216: processContentGroupChild(node, (IContentGroup) child);
217: }
218: }
219: }
220:
221: /**
222: * Process the children of ContentGroup and convert them to the XML format model node.
223: *
224: * @param node the IContentNode object.
225: * @param contentNode The XML format model node.
226: */
227: private void processContentGroupChild(XmlFormatModelNode node,
228: IContentNode contentNode) {
229: XmlFormatModelNode newNode = new XmlFormatModelNode();
230: IContentGroup contentGroup = (IContentGroup) contentNode;
231: newNode.setNamespaceURI(contentGroup.getNamespaceURI());
232: newNode.setName(contentGroup.getName());
233: node.addElement(newNode);
234: newNode.getAttributeList().add(new QName(null, "name"));
235: if (0 != contentGroup.getMinOccurs()) {
236: newNode.getAttributeList()
237: .add(new QName(null, "minOccurs"));
238: newNode.setMinOccurs(contentGroup.getMinOccurs());
239: }
240: if (0 != contentGroup.getMaxOccurs()) {
241: newNode.getAttributeList()
242: .add(new QName(null, "maxOccurs"));
243:
244: newNode.setMaxOccurs(contentGroup.getMaxOccurs());
245:
246: }
247: for (int i = 0; i < contentGroup.getChildCount(); i++) {
248: if (contentGroup.getChildAtIndex(i) instanceof IContentElement) {
249: if (((IContentElement) contentGroup.getChildAtIndex(i))
250: .getElementDefinition() != null
251: && !"".equals(((IContentElement) contentGroup
252: .getChildAtIndex(i))
253: .getElementDefinition().getName())) {
254: processContentElementChild(newNode,
255: (IContentElement) contentGroup
256: .getChildAtIndex(i));
257: } else {
258: processRefChild(newNode,
259: ((IContentElement) contentGroup
260: .getChildAtIndex(i)));
261: }
262: } else if (contentGroup.getChildAtIndex(i) instanceof IContentGroup) {
263: processContentGroupChild(newNode,
264: (IContentGroup) contentGroup.getChildAtIndex(i));
265: }
266: }
267: }
268:
269: private void processContentElementChild(XmlFormatModelNode node,
270: IContentElement contentElement) {
271: XmlFormatModelNode newNode = new XmlFormatModelNode();
272: IElementDefinition elementDefinition = contentElement
273: .getResolvedElementDef();
274: newNode.setNamespaceURI(elementDefinition.getNamespaceURI());
275: newNode.setName(elementDefinition.getName());
276: node.addElement(newNode);
277: newNode.getAttributeList().add(new QName(null, "name"));
278: if (elementDefinition.getName() != null) {
279: newNode.getAttributeList().add(new QName(null, "format"));
280: }
281: if (elementDefinition.getDatatype() != null
282: && !"".equals(elementDefinition.getDatatype())) {
283: newNode.getAttributeList().add(new QName(null, "datatype"));
284: }
285: if (!"".equals(contentElement.getMinOccurs())) {
286: newNode.setMinOccurs(contentElement.getMinOccurs());
287: newNode.getAttributeList()
288: .add(new QName(null, "minOccurs"));
289: }
290: if (!"".equals(contentElement.getMaxOccurs())) {
291: newNode.getAttributeList()
292: .add(new QName(null, "maxOccurs"));
293:
294: newNode.setMaxOccurs(contentElement.getMaxOccurs());
295:
296: }
297: if (contentElement.getFormatChildAttributes() instanceof IFixedChildAttributes) {
298: this .handleFixedChildAttributes(
299: (IFixedChildAttributes) contentElement
300: .getFormatChildAttributes(), newNode);
301: } else if (contentElement.getFormatChildAttributes() instanceof IVariableChildAttributes) {
302:
303: this .handleVariableChildAttributes(
304: (IVariableChildAttributes) contentElement
305: .getFormatChildAttributes(), newNode);
306: }
307:
308: if (elementDefinition.getFormatDefinition() instanceof IVariableFormatDefinition) {
309: this .handleVariableFormatDefinition(
310: (IVariableFormatDefinition) elementDefinition
311: .getFormatDefinition(), newNode);
312: }
313:
314: for (int j = 0; j < elementDefinition.getChildCount(); j++) {
315: IContentNode child = elementDefinition.getChildAtIndex(j);
316: if (child instanceof IContentElement) {
317:
318: processContentElementChild(newNode,
319: (IContentElement) child);
320:
321: } else if (elementDefinition.getChildAtIndex(j) instanceof IContentGroup) {
322: processContentGroupChild(newNode,
323: (IContentGroup) elementDefinition
324: .getChildAtIndex(j));
325: }
326: }
327: }
328:
329: private void handleFixedChildAttributes(
330: IFixedChildAttributes attributes, XmlFormatModelNode newNode) {
331: if (0 != attributes.getFillChar()) {
332: newNode.getAttributeList().add(new QName(null, "fillChar"));
333: }
334: if (0 != attributes.getLength()) {
335: newNode.getAttributeList().add(new QName(null, "length"));
336: }
337: if (0 != attributes.getJustification()) {
338: newNode.getAttributeList().add(
339: new QName(null, "justification"));
340: }
341: }
342:
343: private void handleVariableChildAttributes(
344: IVariableChildAttributes attributes,
345: XmlFormatModelNode newNode) {
346: if (!"".equals(attributes.getTag())) {
347: newNode.getAttributeList().add(new QName(null, "tag"));
348: }
349: if (-1 != attributes.getMinLength()) {
350: newNode.getAttributeList()
351: .add(new QName(null, "minLength"));
352: }
353: if (-1 != attributes.getMaxLength()) {
354: newNode.getAttributeList()
355: .add(new QName(null, "maxLength"));
356: }
357: }
358:
359: private void handleVariableFormatDefinition(
360: IVariableFormatDefinition varFormatDef,
361: XmlFormatModelNode newNode) {
362: if (!"".equals(varFormatDef.getDelimiter())) {
363: newNode.getAttributeList()
364: .add(new QName(null, "delimiter"));
365: }
366: if (!"".equals(varFormatDef.getEscapeChar())) {
367: newNode.getAttributeList().add(
368: new QName(null, "escapeChar"));
369: }
370: if (!"".equals(varFormatDef.getRepeatDelimiter())) {
371: newNode.getAttributeList().add(
372: new QName(null, "repeatDelimiter"));
373: }
374: if (!"".equals(varFormatDef.getQuoteChar())) {
375: newNode.getAttributeList()
376: .add(new QName(null, "quoteChar"));
377: }
378: if (!"".equals(varFormatDef.getIDMethod())) {
379: newNode.getAttributeList().add(new QName(null, "idMethod"));
380: }
381: if (!"".equals(varFormatDef.getTagLength())) {
382: newNode.getAttributeList()
383: .add(new QName(null, "tagLength"));
384: }
385: if (!"".equals(varFormatDef.getType())) {
386: newNode.getAttributeList().add(new QName(null, "type"));
387: }
388: }
389:
390: /**
391: * Process the elements and convert them to the XML formt model node.
392: *
393: * @param node The XML format model node.
394: * @param elementDefinition The ElementDefinition object.
395: * @param i The element child's number.
396: */
397: private void processElementChild(XmlFormatModelNode node,
398: IElementDefinition elementDefinition, int i) {
399: XmlFormatModelNode newNode = new XmlFormatModelNode();
400: IContentElement child = (IContentElement) elementDefinition
401: .getChildAtIndex(i);
402: IElementDefinition subElementDefinition = child
403: .getResolvedElementDef();
404: newNode.setNamespaceURI(subElementDefinition.getNamespaceURI());
405: newNode.setName(subElementDefinition.getName());
406: node.addElement(newNode);
407: newNode.getAttributeList().add(new QName(null, "name"));
408: if (subElementDefinition.getName() != null) {
409: newNode.getAttributeList().add(new QName(null, "format"));
410: }
411: if (subElementDefinition.getDatatype() != null
412: && !"".equals(subElementDefinition.getDatatype())) {
413: newNode.getAttributeList().add(new QName(null, "datatype"));
414: }
415: if (!"".equals(child.getRef())) {
416: newNode.getAttributeList().add(new QName(null, "ref"));
417: }
418: if (!"".equals(child.getMinOccurs())) {
419: newNode.setMinOccurs(child.getMinOccurs());
420: newNode.getAttributeList()
421: .add(new QName(null, "minOccurs"));
422: }
423: if (!"".equals(child.getMaxOccurs())) {
424: newNode.getAttributeList()
425: .add(new QName(null, "maxOccurs"));
426:
427: newNode.setMaxOccurs(child.getMaxOccurs());
428:
429: }
430: if (child.getFormatChildAttributes() instanceof IFixedChildAttributes) {
431: IFixedChildAttributes fixAttributes = ((IFixedChildAttributes) child
432: .getFormatChildAttributes());
433: this .handleFixedChildAttributes(fixAttributes, newNode);
434:
435: } else if (child.getFormatChildAttributes() instanceof IVariableChildAttributes) {
436: IVariableChildAttributes attributes = ((IVariableChildAttributes) child
437: .getFormatChildAttributes());
438: this .handleVariableChildAttributes(attributes, newNode);
439: }
440:
441: if (subElementDefinition.getFormatDefinition() instanceof IVariableFormatDefinition) {
442: IVariableFormatDefinition varFormatDef = (IVariableFormatDefinition) subElementDefinition
443: .getFormatDefinition();
444: this .handleVariableFormatDefinition(varFormatDef, newNode);
445:
446: }
447: for (int j = 0; j < subElementDefinition.getChildCount(); j++) {
448: IContentNode grandchild = subElementDefinition
449: .getChildAtIndex(j);
450: if (grandchild instanceof IContentElement) {
451: if (((IContentElement) grandchild).getRef() != null) {
452: processRefChild(newNode,
453: (IContentElement) grandchild);
454: } else {
455: processContentElementChild(newNode,
456: (IContentElement) grandchild);
457: }
458: } else {
459: processContentGroupChild(newNode,
460: (IContentGroup) grandchild);
461: }
462: }
463: }
464:
465: /**
466: * Process the referenced children.
467: *
468: * @param node The XML format model node.
469: * @param refName The value of ref attribute.
470: */
471: private void processRefChild(XmlFormatModelNode node,
472: IContentElement contentNode) {//String refName) {
473: // IElementDefinition[] allvisualElementDefinitions = mdlDoc.getAllVisibleElementDefinitions();
474:
475: // boolean flag = true;
476: //
477: // for (int i = 0; i < allvisualElementDefinitions.length; i++) {
478: // if (contentNode.getRef().getLocalPart().equals(allvisualElementDefinitions[i].getName())) {
479: // processGlobalElement(node, allvisualElementDefinitions[i], contentNode);
480: // flag = false;
481: // }
482: // }
483:
484: IElementDefinition elementDefinition = contentNode
485: .getResolvedElementDef();
486: if (refMap.get(elementDefinition.getName()) != null) {
487: node.addElement(new XmlFormatModelNode(refMap
488: .get(elementDefinition.getName())));
489: return;
490: }
491: if (elementDefinition != null) {
492: processGlobalElement(node, elementDefinition, contentNode);
493: }
494: // if a element references a external element, add the refName to the parenet.
495: else {
496: XmlFormatModelNode newNode = new XmlFormatModelNode();
497: newNode.setName(contentNode.getRef().getLocalPart());
498: node.addElement(newNode);
499: node.getAttributeList().add(new QName(null, "ref"));
500: node.getAttributeList().add(new QName(null, "format"));
501: node.getAttributeList().add(new QName(null, "datatype"));
502: node.getAttributeList().add(new QName(null, "minOccurs"));
503: node.getAttributeList().add(new QName(null, "maxOccurs"));
504: node.getAttributeList().add(new QName(null, "length"));
505: node.getAttributeList().add(new QName(null, "fillChar"));
506: node.getAttributeList().add(
507: new QName(null, "justification"));
508: node.getAttributeList().add(new QName(null, "delimiter"));
509: node.getAttributeList().add(new QName(null, "escapeChar"));
510: node.getAttributeList().add(
511: new QName(null, "repeatDelimiter"));
512: node.getAttributeList().add(new QName(null, "quoteChar"));
513: node.getAttributeList().add(new QName(null, "tag"));
514: node.getAttributeList().add(new QName(null, "minLength"));
515: node.getAttributeList().add(new QName(null, "maxLength"));
516: }
517: }
518:
519: /**
520: * Process the global elements.
521: *
522: * @param topNode The XML format model node.
523: * @param elementDefinition The IElementDefinition object.
524: */
525: private void processGlobalElement(XmlFormatModelNode topNode,
526: IElementDefinition elementDefinition,
527: IContentNode contentNode) {
528: XmlFormatModelNode newNode = new XmlFormatModelNode(
529: elementDefinition.getName(), elementDefinition
530: .getNamespaceURI());
531: newNode.setCanBeRootElement(false);
532: topNode.addElement(newNode);
533: refMap.put(elementDefinition.getName(), newNode);
534: if (contentNode != null) {
535: newNode.getAttributeList().add(new QName(null, "ref"));
536: } else {
537: newNode.getAttributeList().add(new QName(null, "name"));
538: }
539: if (elementDefinition.getFormatDefinition() != null
540: && elementDefinition.getFormatDefinition().getName() != null) {
541: newNode.getAttributeList().add(new QName(null, "format"));
542: }
543: if (elementDefinition.getDatatype() != null
544: && !"".equals(elementDefinition.getDatatype())) {
545: newNode.getAttributeList().add(new QName(null, "datatype"));
546: }
547: if (contentNode != null) {
548: if (!"".equals(((IContentElement) contentNode)
549: .getMinOccurs())) {
550: newNode.getAttributeList().add(
551: new QName(null, "minOccurs"));
552: newNode.setMinOccurs(((IContentElement) contentNode)
553: .getMinOccurs());
554: }
555: if (!"".equals(((IContentElement) contentNode)
556: .getMaxOccurs())) {
557: newNode.getAttributeList().add(
558: new QName(null, "maxOccurs"));
559:
560: newNode.setMaxOccurs(((IContentElement) contentNode)
561: .getMaxOccurs());
562:
563: }
564: if (((IContentElement) contentNode)
565: .getFormatChildAttributes() instanceof IFixedChildAttributes) {
566: IFixedChildAttributes attributes = ((IFixedChildAttributes) ((IContentElement) contentNode)
567: .getFormatChildAttributes());
568: this .handleFixedChildAttributes(attributes, newNode);
569: } else if (((IContentElement) contentNode)
570: .getFormatChildAttributes() instanceof IVariableChildAttributes) {
571: IVariableChildAttributes attributes = ((IVariableChildAttributes) ((IContentElement) contentNode)
572: .getFormatChildAttributes());
573: this .handleVariableChildAttributes(attributes, newNode);
574: }
575:
576: if (((IContentElement) contentNode).getElementDefinition() != null) {
577: IElementDefinition eleDefinition = ((IContentElement) contentNode)
578: .getElementDefinition();
579: if (eleDefinition instanceof IVariableFormatDefinition) {
580: IVariableFormatDefinition varFormatDef = (IVariableFormatDefinition) eleDefinition
581: .getFormatDefinition();
582: this .handleVariableFormatDefinition(varFormatDef,
583: newNode);
584: }
585: }
586: }
587: for (int i = 0; i < elementDefinition.getChildCount(); i++) {
588: IContentNode child = elementDefinition.getChildAtIndex(i);
589: if (child instanceof IContentElement) {
590: if (((IContentElement) child).getRef() != null) {
591: processRefChild(newNode, (IContentElement) child);
592: } else {
593: processElementChild(newNode, elementDefinition, i);
594: }
595: } else {
596: processContentGroupChild(newNode, (IContentGroup) child);
597: }
598: }
599: }
600:
601: }
|