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: MDLDocumentImpl.java 8174 2007-07-06 02:00:09Z lzheng $
024: */
025: package com.bostechcorp.cbesb.common.mdl.impl;
026:
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.io.OutputStreamWriter;
030: import java.io.UnsupportedEncodingException;
031: import java.io.Writer;
032: import java.util.Collection;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.LinkedHashMap;
036: import java.util.Map;
037:
038: import javax.xml.namespace.QName;
039:
040: import org.jdom.Attribute;
041: import org.jdom.Document;
042: import org.jdom.Element;
043: import org.jdom.output.Format;
044: import org.jdom.output.XMLOutputter;
045:
046: import com.bostechcorp.cbesb.common.mdl.IDatatypeDefinition;
047: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
048: import com.bostechcorp.cbesb.common.mdl.IMDLDocReference;
049: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
050: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
051: import com.bostechcorp.cbesb.common.mdl.IProperty;
052: import com.bostechcorp.cbesb.common.mdl.MDLDocConstants;
053: import com.bostechcorp.cbesb.common.util.ErrorUtil;
054:
055: /**
056: * Provides methods to manipulate an MDL document.
057: *
058: */
059: public class MDLDocumentImpl implements IMDLDocument {
060:
061: /**
062: * The targetNamespace.
063: */
064: private String targetNamespace;
065:
066: /**
067: * The elementFormDefault.
068: */
069: private byte elementFormDefault;
070:
071: /**
072: * Map used to resolve namespace prefixes
073: */
074: private Map<String, String> namespaceMap;
075:
076: /**
077: * The MDLReference map.
078: */
079: private Map<String, IMDLDocReference> mdlDocRefMap;
080:
081: /**
082: * The MessageDefinition map.
083: */
084: private Map<String, IMessageDefinition> messageDefMap;
085:
086: /**
087: * The visible MessageDefinition map.
088: */
089: private Map<String, IMessageDefinition> visibleMessageDefMap;
090:
091: /**
092: * The ElementDefinition map.
093: */
094: private Map<String, IElementDefinition> elementDefMap;
095:
096: /**
097: * The visible ElementDefinition map.
098: */
099: private Map<String, IElementDefinition> visibleElementDefMap;
100:
101: /**
102: * The DatatypeDefinition map.
103: */
104: private Map<String, IDatatypeDefinition> datatypeDefMap;
105:
106: /**
107: * The visible DatatypeDefinition map.
108: */
109: private Map<String, IDatatypeDefinition> visibleDatatypeDefMap;
110:
111: /**
112: * The Property map.
113: */
114: private Map<String, IProperty> propertyMap;
115:
116: /**
117: * The visible Property map.
118: */
119: private Map<String, IProperty> visiblePropertyMap;
120:
121: /**
122: * Constructors
123: */
124: public MDLDocumentImpl() {
125: targetNamespace = "";
126: elementFormDefault = ELEMENT_FORM_DEFAULT_UNQUALIFIED;
127: namespaceMap = new HashMap<String, String>();
128: mdlDocRefMap = new LinkedHashMap<String, IMDLDocReference>();
129: messageDefMap = new LinkedHashMap<String, IMessageDefinition>();
130: visibleMessageDefMap = new LinkedHashMap<String, IMessageDefinition>();
131: elementDefMap = new LinkedHashMap<String, IElementDefinition>();
132: visibleElementDefMap = new LinkedHashMap<String, IElementDefinition>();
133: datatypeDefMap = new LinkedHashMap<String, IDatatypeDefinition>();
134: visibleDatatypeDefMap = new LinkedHashMap<String, IDatatypeDefinition>();
135: propertyMap = new LinkedHashMap<String, IProperty>();
136: visiblePropertyMap = new LinkedHashMap<String, IProperty>();
137: }
138:
139: /* (non-Javadoc)
140: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getTargetNamespace()
141: */
142: public String getTargetNamespace() {
143: return targetNamespace;
144: }
145:
146: /* (non-Javadoc)
147: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#setTargetNamespace(java.lang.String)
148: */
149: public void setTargetNamespace(String targetNamespace) {
150: this .targetNamespace = targetNamespace;
151: }
152:
153: /* (non-Javadoc)
154: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getElementFormDefault()
155: */
156: public byte getElementFormDefault() {
157: return elementFormDefault;
158: }
159:
160: /* (non-Javadoc)
161: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#setElementFormDefault(byte)
162: */
163: public void setElementFormDefault(byte elementFormDefault) {
164: this .elementFormDefault = elementFormDefault;
165: }
166:
167: public void setNamespaceMapping(String prefix, String URI) {
168: namespaceMap.put(prefix, URI);
169: }
170:
171: public String resolveNamespacePrefix(String prefix) {
172: return namespaceMap.get(prefix);
173: }
174:
175: public QName getQNameFromReference(String reference) {
176: QName returnVal;
177: if (reference.indexOf(":") == -1) {
178: //No namespace prefix
179: if (targetNamespace == null) {
180: returnVal = new QName(reference);
181: } else {
182: returnVal = new QName(targetNamespace, reference);
183: }
184: } else {
185: String prefix = reference.substring(0, reference
186: .indexOf(":"));
187: String localname = reference.substring(reference
188: .indexOf(":") + 1);
189: returnVal = new QName(resolveNamespacePrefix(prefix),
190: localname);
191: }
192:
193: return returnVal;
194: }
195:
196: /* (non-Javadoc)
197: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllMDLDocReferences()
198: */
199: public IMDLDocReference[] getAllMDLDocReferences() {
200: IMDLDocReference refList[] = new IMDLDocReference[mdlDocRefMap
201: .size()];
202:
203: Collection collection = mdlDocRefMap.values();
204: Iterator iter = collection.iterator();
205: int index = 0;
206: while (iter.hasNext()) {
207: refList[index] = (IMDLDocReference) iter.next();
208: index++;
209: }
210: return refList;
211: }
212:
213: /* (non-Javadoc)
214: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#addMDLDocReference(com.bostechcorp.cbesb.common.mdl.IMDLDocReference)
215: */
216: public void addMDLDocReference(IMDLDocReference docReference) {
217: String key = "";
218: if (IMDLDocReference.TYPE_INCLUDE == docReference.getType()) {
219: key = docReference.getType() + ":: ::"
220: + docReference.getDefLocation();
221: } else if (IMDLDocReference.TYPE_IMPORT == docReference
222: .getType()) {
223: key = docReference.getType() + "::"
224: + docReference.getNamespace() + "::"
225: + docReference.getDefLocation();
226: }
227: mdlDocRefMap.put(key, docReference);
228: }
229:
230: /* (non-Javadoc)
231: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#removeMDLDocReference(com.bostechcorp.cbesb.common.mdl.IMDLDocReference)
232: */
233: public void removeMDLDocReference(IMDLDocReference docReference) {
234: String key = "";
235: if (IMDLDocReference.TYPE_INCLUDE == docReference.getType()) {
236: key = docReference.getType() + ":: ::"
237: + docReference.getDefLocation();
238: } else if (IMDLDocReference.TYPE_IMPORT == docReference
239: .getType()) {
240: key = docReference.getType() + "::"
241: + docReference.getNamespace() + "::"
242: + docReference.getDefLocation();
243: }
244: mdlDocRefMap.remove(key);
245: }
246:
247: /* (non-Javadoc)
248: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getMessageDefinition(java.lang.String, java.lang.String)
249: */
250: public IMessageDefinition getMessageDefinition(String namespaceURI,
251: String localName) {
252: String key = namespaceURI + "::" + localName;
253: return (IMessageDefinition) visibleMessageDefMap.get(key);
254: }
255:
256: /* (non-Javadoc)
257: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllMessageDefinitions()
258: */
259: public IMessageDefinition[] getAllMessageDefinitions() {
260: IMessageDefinition defList[] = new MessageDefinitionImpl[messageDefMap
261: .size()];
262:
263: Collection defs = messageDefMap.values();
264:
265: Iterator iter = defs.iterator();
266: int index = 0;
267: while (iter.hasNext()) {
268: defList[index] = (IMessageDefinition) iter.next();
269: index++;
270: }
271: return defList;
272: }
273:
274: /* (non-Javadoc)
275: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllVisibleMessageDefinitions()
276: */
277: public IMessageDefinition[] getAllVisibleMessageDefinitions() {
278: IMessageDefinition defList[] = new MessageDefinitionImpl[visibleMessageDefMap
279: .size()];
280:
281: Collection defs = visibleMessageDefMap.values();
282:
283: Iterator iter = defs.iterator();
284: int index = 0;
285: while (iter.hasNext()) {
286: defList[index] = (IMessageDefinition) iter.next();
287: index++;
288: }
289: return defList;
290: }
291:
292: /* (non-Javadoc)
293: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#addMessageDefinition(com.bostechcorp.cbesb.common.mdl.IMessageDefinition)
294: */
295: public void addMessageDefinition(IMessageDefinition messageDef) {
296: String key = messageDef.getNamespaceURI() + "::"
297: + messageDef.getName();
298: visibleMessageDefMap.put(key, messageDef);
299: messageDefMap.put(key, messageDef);
300: }
301:
302: /*
303: * This method should only be used by the MDL loader to handle definitions
304: * from external documents.
305: */
306: public void addMessageDefinitionToVisibleList(
307: IMessageDefinition messageDef) {
308: String key = messageDef.getNamespaceURI() + "::"
309: + messageDef.getName();
310: visibleMessageDefMap.put(key, messageDef);
311: }
312:
313: /* (non-Javadoc)
314: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#removeMessageDefinition(java.lang.String, java.lang.String)
315: */
316: public void removeMessageDefinition(String namespaceURI,
317: String localName) {
318: String key = namespaceURI + "::" + localName;
319: messageDefMap.remove(key);
320: visibleMessageDefMap.remove(key);
321: }
322:
323: /* (non-Javadoc)
324: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getElementDefinition(java.lang.String, java.lang.String)
325: */
326: public IElementDefinition getElementDefinition(String namespaceURI,
327: String localName) {
328: String key = namespaceURI + "::" + localName;
329: return (IElementDefinition) visibleElementDefMap.get(key);
330: }
331:
332: /* (non-Javadoc)
333: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllElementDefinitions()
334: */
335: public IElementDefinition[] getAllElementDefinitions() {
336: IElementDefinition defList[] = new IElementDefinition[elementDefMap
337: .size()];
338:
339: Collection defs = elementDefMap.values();
340:
341: Iterator iter = defs.iterator();
342: int index = 0;
343: while (iter.hasNext()) {
344: defList[index] = (IElementDefinition) iter.next();
345: index++;
346: }
347: return defList;
348: }
349:
350: /* (non-Javadoc)
351: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllVisibleElementDefinitions()
352: */
353: public IElementDefinition[] getAllVisibleElementDefinitions() {
354: IElementDefinition defList[] = new IElementDefinition[visibleElementDefMap
355: .size()];
356:
357: Collection defs = visibleElementDefMap.values();
358:
359: Iterator iter = defs.iterator();
360: int index = 0;
361: while (iter.hasNext()) {
362: defList[index] = (IElementDefinition) iter.next();
363: index++;
364: }
365: return defList;
366: }
367:
368: /* (non-Javadoc)
369: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#addElementDefinition(com.bostechcorp.cbesb.common.mdl.IElementDefinition)
370: */
371: public void addElementDefinition(
372: IElementDefinition elementDefinition) {
373: String key = elementDefinition.getNamespaceURI() + "::"
374: + elementDefinition.getName();
375: visibleElementDefMap.put(key, elementDefinition);
376: elementDefMap.put(key, elementDefinition);
377: }
378:
379: /**
380: * This method should only be used by the MDL loader to handle definitions
381: * @param elementDefinition
382: */
383: public void addElementDefinitionToVisibleList(
384: IElementDefinition elementDefinition) {
385: String key = elementDefinition.getNamespaceURI() + "::"
386: + elementDefinition.getName();
387: visibleElementDefMap.put(key, elementDefinition);
388: }
389:
390: /* (non-Javadoc)
391: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#removeElementDefinition(java.lang.String, java.lang.String)
392: */
393: public void removeElementDefinition(String namespaceURI,
394: String localName) {
395: String key = namespaceURI + "::" + localName;
396: visibleElementDefMap.remove(key);
397: elementDefMap.remove(key);
398: }
399:
400: /* (non-Javadoc)
401: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getDatatypeDefinition(java.lang.String, java.lang.String)
402: */
403: public IDatatypeDefinition getDatatypeDefinition(
404: String namespaceURI, String localName) {
405: String key = namespaceURI + "::" + localName;
406: return (IDatatypeDefinition) visibleDatatypeDefMap.get(key);
407: }
408:
409: /* (non-Javadoc)
410: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllDatatypeDefinitions()
411: */
412: public IDatatypeDefinition[] getAllDatatypeDefinitions() {
413: IDatatypeDefinition defList[] = new IDatatypeDefinition[datatypeDefMap
414: .size()];
415:
416: Collection defs = datatypeDefMap.values();
417:
418: Iterator iter = defs.iterator();
419: int index = 0;
420: while (iter.hasNext()) {
421: defList[index] = (IDatatypeDefinition) iter.next();
422: index++;
423: }
424: return defList;
425: }
426:
427: /* (non-Javadoc)
428: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllVisibleDatatypeDefinitions()
429: */
430: public IDatatypeDefinition[] getAllVisibleDatatypeDefinitions() {
431: IDatatypeDefinition defList[] = new IDatatypeDefinition[visibleDatatypeDefMap
432: .size()];
433:
434: Collection defs = visibleDatatypeDefMap.values();
435:
436: Iterator iter = defs.iterator();
437: int index = 0;
438: while (iter.hasNext()) {
439: defList[index] = (IDatatypeDefinition) iter.next();
440: index++;
441: }
442: return defList;
443: }
444:
445: /* (non-Javadoc)
446: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#addDatatypeDefinition(com.bostechcorp.cbesb.common.mdl.IDatatypeDefinition)
447: */
448: public void addDatatypeDefinition(IDatatypeDefinition datatypeDef) {
449: String key = datatypeDef.getNamespaceURI() + "::"
450: + datatypeDef.getName();
451: visibleDatatypeDefMap.put(key, datatypeDef);
452: datatypeDefMap.put(key, datatypeDef);
453: }
454:
455: /**
456: * This method should only be used by the MDL loader to handle definitions
457: * @param datatypeDef
458: */
459: public void addDatatypeDefinitionToVisibleList(
460: IDatatypeDefinition datatypeDef) {
461: String key = datatypeDef.getNamespaceURI() + "::"
462: + datatypeDef.getName();
463: visibleDatatypeDefMap.put(key, datatypeDef);
464: }
465:
466: /* (non-Javadoc)
467: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#removeDatatypeDefinition(java.lang.String, java.lang.String)
468: */
469: public void removeDatatypeDefinition(String namespaceURI,
470: String localName) {
471: String key = namespaceURI + "::" + localName;
472: visibleDatatypeDefMap.remove(key);
473: datatypeDefMap.remove(key);
474: }
475:
476: public void serialize(OutputStream outputStream) {
477: MDLSerializerUtil serializerUtil = new MDLSerializerUtil();
478:
479: Element mdlRoot = serializerUtil
480: .createElement(MDLDocConstants.MDL_DEFINITION);
481: Document mdlDoc = serializerUtil.createDocument(mdlRoot);
482:
483: //If target namespace is defined, then add targetNamespace attribute
484: //and elementFormDefault setting
485: if (!"".equals(targetNamespace)) {
486: serializerUtil.setTargetNamespace(targetNamespace);
487:
488: Attribute attrTargetNS = serializerUtil.createAttribute(
489: MDLDocConstants.MDL_TARGETNAMESPACE,
490: targetNamespace);
491: mdlRoot.setAttribute(attrTargetNS);
492:
493: Attribute attrElemFormDefault;
494: if (elementFormDefault == IMDLDocument.ELEMENT_FORM_DEFAULT_QUALIFIED) {
495: attrElemFormDefault = serializerUtil.createAttribute(
496: MDLDocConstants.MDL_ELEMENTFORMDEFAULT,
497: MDLDocConstants.MDL_ELEMFORMDEF_QUALIFIED);
498: } else {
499: attrElemFormDefault = serializerUtil.createAttribute(
500: MDLDocConstants.MDL_ELEMENTFORMDEFAULT,
501: MDLDocConstants.MDL_ELEMFORMDEF_QUALIFIED);
502: }
503: mdlRoot.setAttribute(attrElemFormDefault);
504: }
505:
506: //First process all Doc References (Include/Import)
507: IMDLDocReference[] docRefs = getAllMDLDocReferences();
508: for (int i = 0; i < docRefs.length; i++) {
509: ((MDLDocReferenceImpl) docRefs[i]).serializeToJDom(
510: serializerUtil, mdlRoot);
511: }
512:
513: //Process all Properties
514: IProperty[] properties = getAllProperties();
515: for (int i = 0; i < properties.length; i++) {
516: ((PropertyImpl) properties[i]).serializeToJDom(
517: serializerUtil, mdlRoot);
518: }
519:
520: //Process all Messages
521: IMessageDefinition[] messages = getAllMessageDefinitions();
522: for (int i = 0; i < messages.length; i++) {
523: ((MessageDefinitionImpl) messages[i]).serializeToJDom(
524: serializerUtil, mdlRoot);
525: }
526:
527: //Process all Elements
528: IElementDefinition[] elemDefinitions = getAllElementDefinitions();
529: for (int i = 0; i < elemDefinitions.length; i++) {
530: ((ElementDefinitionImpl) elemDefinitions[i])
531: .serializeToJDom(serializerUtil, mdlRoot);
532: }
533:
534: //Process all Datatypes
535: IDatatypeDefinition[] dataTypeDefs = getAllDatatypeDefinitions();
536: for (int i = 0; i < dataTypeDefs.length; i++) {
537: ((DatatypeDefinitionImpl) dataTypeDefs[i]).serializeToJDom(
538: serializerUtil, mdlRoot);
539: }
540:
541: //Add all of the namespace declarations to the root element
542: serializerUtil.AddNamespaceDeclarationsToRoot(mdlRoot);
543: Writer writer = null;
544: try {
545: writer = new OutputStreamWriter(outputStream, "utf-8");
546: } catch (UnsupportedEncodingException e1) {
547:
548: e1.printStackTrace();
549: writer = new OutputStreamWriter(outputStream);
550: }
551: //Output the DOM tree
552: XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
553: try {
554: xmlOut.output(mdlDoc, writer);
555: } catch (IOException e) {
556: ErrorUtil.printError("Exception in serialize() ", e);
557: }
558: }
559:
560: /* (non-Javadoc)
561: * @see com.bostechcorp.cbesb.legacydata.mdl.MDLDocument#toSchema(java.io.OutputStream)
562: */
563: public void toSchema(OutputStream outputStream) {
564:
565: }
566:
567: /* (non-Javadoc)
568: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#addProperty(java.lang.String, java.lang.String)
569: */
570: public void addProperty(IProperty property) {
571: String key = property.getNamespaceURI() + "::"
572: + property.getName();
573: propertyMap.put(key, property);
574: visiblePropertyMap.put(key, property);
575: }
576:
577: /**
578: * This method should only be used by the MDL loader to handle definitions
579: * @param property
580: */
581: public void addPropertyToVisibleList(IProperty property) {
582: String key = property.getNamespaceURI() + "::"
583: + property.getName();
584: visiblePropertyMap.put(key, property);
585: }
586:
587: /* (non-Javadoc)
588: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getProperty(java.lang.String)
589: */
590: public IProperty getProperty(String namespaceURI, String localName) {
591: String key = namespaceURI + "::" + localName;
592: return (IProperty) visiblePropertyMap.get(key);
593: }
594:
595: /* (non-Javadoc)
596: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#getAllProperties()
597: */
598: public IProperty[] getAllProperties() {
599: IProperty propList[] = new IProperty[propertyMap.size()];
600:
601: Collection defs = propertyMap.values();
602:
603: Iterator iter = defs.iterator();
604: int index = 0;
605: while (iter.hasNext()) {
606: propList[index] = (IProperty) iter.next();
607: index++;
608: }
609: return propList;
610: }
611:
612: public IProperty[] getAllVisibleProperties() {
613: IProperty propList[] = new IProperty[visiblePropertyMap.size()];
614:
615: Collection defs = visiblePropertyMap.values();
616:
617: Iterator iter = defs.iterator();
618: int index = 0;
619: while (iter.hasNext()) {
620: propList[index] = (IProperty) iter.next();
621: index++;
622: }
623: return propList;
624: }
625:
626: /* (non-Javadoc)
627: * @see com.bostechcorp.cbesb.common.mdl.IMDLDocument#removeProperty(java.lang.String)
628: */
629: public void removeProperty(String namespaceURI, String localName) {
630: String key = namespaceURI + "::" + localName;
631: propertyMap.remove(key);
632: visiblePropertyMap.remove(key);
633: }
634: }
|