0001: /*
0002: * ChainBuilder ESB
0003: * Visual Enterprise Integration
0004: *
0005: * Copyright (C) 2006 Bostech Corporation
0006: *
0007: * This program is free software; you can redistribute it and/or modify
0008: * it under the terms of the GNU General Public License as published by
0009: * the Free Software Foundation; either version 2 of the License, or
0010: * (at your option) any later version.
0011: *
0012: * This program is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * along with this program; if not, write to the Free Software
0019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
0020: * USA
0021: *
0022: * $Id: MDLParser.java 6617 2007-04-11 01:04:44Z zjin $
0023: *
0024: */
0025: package com.bostechcorp.cbesb.common.mdl;
0026:
0027: import java.io.File;
0028: import java.io.IOException;
0029: import java.util.Stack;
0030:
0031: import javax.xml.namespace.QName;
0032: import javax.xml.parsers.ParserConfigurationException;
0033: import javax.xml.parsers.SAXParser;
0034: import javax.xml.parsers.SAXParserFactory;
0035:
0036: import org.apache.commons.logging.Log;
0037: import org.apache.commons.logging.LogFactory;
0038: import org.xml.sax.Attributes;
0039: import org.xml.sax.InputSource;
0040: import org.xml.sax.SAXException;
0041: import org.xml.sax.helpers.DefaultHandler;
0042:
0043: import com.bostechcorp.cbesb.common.i18n.Message;
0044: import com.bostechcorp.cbesb.common.i18n.Messages;
0045: import com.bostechcorp.cbesb.common.mdl.impl.ContentElementImpl;
0046: import com.bostechcorp.cbesb.common.mdl.impl.ContentGroupImpl;
0047: import com.bostechcorp.cbesb.common.mdl.impl.DatatypeDefinitionImpl;
0048: import com.bostechcorp.cbesb.common.mdl.impl.ElementDefinitionImpl;
0049: import com.bostechcorp.cbesb.common.mdl.impl.FixedChildAttributesImpl;
0050: import com.bostechcorp.cbesb.common.mdl.impl.FixedFormatDefinitionImpl;
0051: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocReferenceImpl;
0052: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocumentImpl;
0053: import com.bostechcorp.cbesb.common.mdl.impl.MessageDefinitionImpl;
0054: import com.bostechcorp.cbesb.common.mdl.impl.PropertyImpl;
0055: import com.bostechcorp.cbesb.common.mdl.impl.VariableChildAttributesImpl;
0056: import com.bostechcorp.cbesb.common.mdl.impl.VariableFormatDefinitionImpl;
0057: import com.bostechcorp.cbesb.common.util.ErrorUtil;
0058:
0059: /**
0060: * MDL Parser
0061: *
0062: */
0063: public class MDLParser extends DefaultHandler {
0064:
0065: // Private class attributes
0066: private Stack<Object> loadStack;
0067:
0068: private IMDLDocument mdlDoc;
0069:
0070: private File mdlFile = null;
0071:
0072: private static boolean visibleFlag = false;
0073:
0074: private StringBuffer stringBuffer;
0075:
0076: private MDLDocumentFactory mdlFactory;
0077:
0078: protected static transient Log logger = LogFactory
0079: .getLog(MDLParser.class);
0080:
0081: /**
0082: * Default Constructor
0083: */
0084: public MDLParser(MDLDocumentFactory mdlFactory) {
0085: this .mdlFactory = mdlFactory;
0086: loadStack = new Stack<Object>();
0087: mdlDoc = new MDLDocumentImpl();
0088: }
0089:
0090: public static IMDLDocument load(MDLDocumentFactory mdlFactory,
0091: InputSource inputSrc) throws MDLException {
0092: MDLParser parserInstance = new MDLParser(mdlFactory);
0093: return parserInstance.parse(inputSrc);
0094: }
0095:
0096: /**
0097: * Static method to instantiate an MDLDocument object from a File.
0098: *
0099: * @param filename The File to load
0100: * @return The loaded MDLDocument
0101: * @throws MDLException If any error occurs.
0102: */
0103: // public static IMDLDocument load(File filename) throws MDLException {
0104: // MDLParser parserInstance = new MDLParser();
0105: // return parserInstance.parse(filename);
0106: // }
0107: // public static IMDLDocument load(File filename,String zipLocation) throws MDLException {
0108: // MDLParser parserInstance = new MDLParser();
0109: // return parserInstance.parse(filename,zipLocation);
0110: // }
0111: /**
0112: * Create a MDLDocument instance from the inputStream.
0113: *
0114: * @param inputStream InputStream
0115: * @return The MDLDocument instance.
0116: * @throws MDLException If any error occurs.
0117: */
0118: // public static IMDLDocument load(InputStream inputStream) throws MDLException {
0119: // MDLParser parserInstance = new MDLParser();
0120: // return parserInstance.parse(inputStream);
0121: // }
0122:
0123: /**
0124: * Create a MDLDocument instance from the inputStream. The zip location of the source
0125: * should be specificed.
0126: *
0127: * @param inStream InputStream
0128: * @param zipLocation The zip location of the source file.
0129: * @return The MDLDocument instance.
0130: * @throws MDLException If any error occurs.
0131: */
0132: // public static IMDLDocument load(InputStream inStream,String zipLocation) throws MDLException {
0133: // MDLParser parserInstance = new MDLParser();
0134: // return parserInstance.parse(inStream,zipLocation);
0135: // }
0136: /**
0137: * @param mdlFile the mdlFile to set
0138: */
0139: public void setMdlFile(File mdlFile) {
0140: this .mdlFile = mdlFile;
0141: }
0142:
0143: /**
0144: * Perform cleanup so GC can free up some memory
0145: */
0146: public void finalize() {
0147: loadStack.clear();
0148: loadStack = null;
0149: mdlDoc = null;
0150: }
0151:
0152: /* (non-Javadoc)
0153: * SAX Handler Methods
0154: * @see org.xml.sax.helpers.DefaultHandler#startDocument()
0155: */
0156: public void startDocument() throws SAXException {
0157: super .startDocument();
0158: stringBuffer = new StringBuffer();
0159: }
0160:
0161: /* (non-Javadoc)
0162: * SAX Handler Methods
0163: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
0164: */
0165: public void endDocument() throws SAXException {
0166: super .endDocument();
0167: // addMdlToElementDefinition();
0168: }
0169:
0170: /* (non-Javadoc)
0171: * SAX Handler Methods
0172: * @see org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String)
0173: */
0174: public void startPrefixMapping(String prefix, String uri)
0175: throws SAXException {
0176: super .startPrefixMapping(prefix, uri);
0177: mdlDoc.setNamespaceMapping(prefix, uri);
0178: }
0179:
0180: /* (non-Javadoc)
0181: * SAX Handler Methods
0182: * @see org.xml.sax.helpers.DefaultHandler#endPrefixMapping(java.lang.String)
0183: */
0184: public void endPrefixMapping(String prefix) throws SAXException {
0185: super .endPrefixMapping(prefix);
0186: }
0187:
0188: /* (non-Javadoc)
0189: * SAX Handler Methods
0190: * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
0191: */
0192: public void startElement(String uri, String localName,
0193: String qName, Attributes attributes) throws SAXException {
0194: super .startElement(uri, localName, qName, attributes);
0195: // Ignore elements that do not belong to the MDL Namespace
0196: if (uri.equals(MDLDocConstants.MDL_NAMESPACE)) {
0197: try {
0198: if (localName.equals(MDLDocConstants.MDL_DEFINITION)) {
0199: startDefinitionNode(attributes);
0200: } else if (localName
0201: .equals(MDLDocConstants.MDL_INCLUDE)) {
0202: startIncludeNode(attributes);
0203: } else if (localName.equals(MDLDocConstants.MDL_IMPORT)) {
0204: startImportNode(attributes);
0205: } else if (localName
0206: .equals(MDLDocConstants.MDL_MESSAGE)) {
0207: startMessageNode(attributes);
0208: } else if (localName
0209: .equals(MDLDocConstants.MDL_ELEMENT)) {
0210: startElementNode(attributes);
0211: } else if (localName
0212: .equals(MDLDocConstants.MDL_DATATYPE)) {
0213: startDatatypeNode(attributes);
0214: } else if (localName
0215: .equals(MDLDocConstants.MDL_PROPERTY)) {
0216: startPropertyNode(attributes);
0217: } else if (localName
0218: .equals(MDLDocConstants.MDL_DESCRIPTION)) {
0219: startDescriptionNode(attributes);
0220: } else if (localName.equals(MDLDocConstants.MDL_GROUP)) {
0221: startGroupNode(attributes);
0222: } else if (localName
0223: .equals(MDLDocConstants.MDL_CONTENT)) {
0224: startContentNode(attributes);
0225: }
0226: } catch (MDLException e) {
0227: // SAX API forces us to wrap the exception in SAXException
0228: ErrorUtil
0229: .printError("Exception in startElement(): ", e);
0230: throw new SAXException(e);
0231: }
0232: }
0233: }
0234:
0235: /* (non-Javadoc)
0236: * SAX Handler Methods
0237: * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
0238: */
0239: public void endElement(String uri, String localName, String qName)
0240: throws SAXException {
0241: super .endElement(uri, localName, qName);
0242: // Ignore elements that do not belong to the MDL Namespace
0243: if (uri.equals(MDLDocConstants.MDL_NAMESPACE)) {
0244: try {
0245: if (localName.equals(MDLDocConstants.MDL_DEFINITION)) {
0246: endDefinitionNode();
0247: } else if (localName
0248: .equals(MDLDocConstants.MDL_INCLUDE)) {
0249: endIncludeNode();
0250: } else if (localName.equals(MDLDocConstants.MDL_IMPORT)) {
0251: endImportNode();
0252: } else if (localName
0253: .equals(MDLDocConstants.MDL_MESSAGE)) {
0254: endMessageNode();
0255: } else if (localName
0256: .equals(MDLDocConstants.MDL_ELEMENT)) {
0257: endElementNode();
0258: } else if (localName
0259: .equals(MDLDocConstants.MDL_DATATYPE)) {
0260: endDatatypeNode();
0261: } else if (localName
0262: .equals(MDLDocConstants.MDL_PROPERTY)) {
0263: endPropertyNode();
0264: } else if (localName
0265: .equals(MDLDocConstants.MDL_DESCRIPTION)) {
0266: endDescriptionNode();
0267: } else if (localName.equals(MDLDocConstants.MDL_GROUP)) {
0268: endGroupNode();
0269: } else if (localName
0270: .equals(MDLDocConstants.MDL_CONTENT)) {
0271: endContentNode();
0272: }
0273: } catch (MDLException e) {
0274: ErrorUtil.printError("Exception in endElement(): ", e);
0275: // SAX API forces us to wrap the exception in SAXException
0276: throw new SAXException(e);
0277: }
0278: }
0279: }
0280:
0281: /* (non-Javadoc)
0282: * SAX Handler Methods
0283: * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
0284: */
0285: public void characters(char[] ch, int start, int length)
0286: throws SAXException {
0287: super .characters(ch, start, length);
0288: stringBuffer.append(ch, start, length);
0289: }
0290:
0291: public IMDLDocument parse(InputSource inputSrc) throws MDLException {
0292: try {
0293: SAXParserFactory factory = SAXParserFactory.newInstance();
0294: factory.setNamespaceAware(true);
0295: SAXParser saxParser = factory.newSAXParser();
0296: saxParser.parse(inputSrc, this );
0297: return mdlDoc;
0298: } catch (SAXException se) {
0299: ErrorUtil.printError("Exception in parse(): ", se);
0300: // If this SAXException is wrapping an MDLException, unwrap it and re-throw
0301: // If not, then create a new MDL Exception
0302: if ((se.getException() != null)
0303: && (se.getException() instanceof MDLException)) {
0304: MDLException me = (MDLException) se.getException();
0305: throw me;
0306: } else {
0307: MDLException me = new MDLException(se);
0308: throw me;
0309: }
0310: } catch (ParserConfigurationException pce) {
0311: ErrorUtil.printError("Exception in parse(): ", pce);
0312: MDLException me = new MDLException(pce);
0313: throw me;
0314: } catch (IOException ioe) {
0315: ErrorUtil.printError("Exception in parse(): ", ioe);
0316: MDLException me = new MDLException(ioe);
0317: throw me;
0318: }
0319: }
0320:
0321: /**
0322: * MDL Specific Methods: process the Definition node. set the targetNamespace and elementFormDefault.
0323: *
0324: * @param attributes Attributes
0325: * @throws MDLException MDLException
0326: */
0327: private void startDefinitionNode(Attributes attributes)
0328: throws MDLException {
0329: String targetNamespace = attributes.getValue("",
0330: MDLDocConstants.MDL_TARGETNAMESPACE);
0331: if (targetNamespace != null) {
0332: mdlDoc.setTargetNamespace(targetNamespace);
0333: }
0334: String elementFormDefault = attributes.getValue("",
0335: MDLDocConstants.MDL_ELEMENTFORMDEFAULT);
0336: if (elementFormDefault != null) {
0337: if (elementFormDefault
0338: .equals(MDLDocConstants.MDL_ELEMFORMDEF_QUALIFIED)) {
0339: mdlDoc
0340: .setElementFormDefault(MDLDocumentImpl.ELEMENT_FORM_DEFAULT_QUALIFIED);
0341: } else if (elementFormDefault
0342: .equals(MDLDocConstants.MDL_ELEMFORMDEF_UNQUALIFIED)) {
0343: mdlDoc
0344: .setElementFormDefault(MDLDocumentImpl.ELEMENT_FORM_DEFAULT_UNQUALIFIED);
0345: } else {
0346: throw new MDLException(new Message(
0347: Messages.INVALID_ATTRIBUTE_VALUE,
0348: MDLDocConstants.MDL_ELEMENTFORMDEFAULT,
0349: elementFormDefault).getMessage());
0350: }
0351: }
0352: loadStack.push(mdlDoc);
0353: }
0354:
0355: /**
0356: * MDL Specific Methods: end to process the definition node.
0357: *
0358: * @throws MDLException MDLException
0359: */
0360: private void endDefinitionNode() throws MDLException {
0361: if (!(loadStack.peek() instanceof IMDLDocument)) {
0362: throw new MDLException(new Message(
0363: Messages.EXPECTED_DOCUMENT_TOP_OF_STACK)
0364: .getMessage());
0365: }
0366: loadStack.pop();
0367: }
0368:
0369: /**
0370: * MDL Specific Methods: process the include node.
0371: *
0372: * @param attributes Attributes
0373: * @throws MDLException MDLException
0374: */
0375: private void startIncludeNode(Attributes attributes)
0376: throws MDLException {
0377: visibleFlag = true;
0378: IMDLDocReference docRef = new MDLDocReferenceImpl();
0379: docRef.setType(MDLDocReferenceImpl.TYPE_INCLUDE);
0380: String defLocation = attributes.getValue("",
0381: MDLDocConstants.MDL_DEFLOCATION);
0382: if (defLocation == null) {
0383: throw new MDLException(new Message(
0384: Messages.MISSING_REQUIRED_ATTRIBUTE,
0385: MDLDocConstants.MDL_DEFINITION).getMessage());
0386: }
0387:
0388: docRef.setDefLocation(defLocation);
0389: IMDLDocument refDef = null;
0390:
0391: if (mdlFile != null) {
0392: File baseDir = mdlFile.getParentFile();
0393: refDef = mdlFactory.getMDLDocument(defLocation, baseDir
0394: .getAbsolutePath());
0395: } else {
0396: refDef = mdlFactory.getMDLDocument(defLocation, null);
0397: }
0398:
0399: docRef.setMDLDocument(refDef);
0400:
0401: IElementDefinition[] visibleElementDefinition;
0402: IMessageDefinition[] visibleMessageDefinition;
0403: IDatatypeDefinition[] visibleDatatypeDefinitions;
0404: IProperty[] visibleProperties;
0405:
0406: visibleElementDefinition = refDef
0407: .getAllVisibleElementDefinitions();
0408: if (visibleElementDefinition != null) {
0409: int defLength = visibleElementDefinition.length;
0410: for (int i = 0; i < defLength; i++) {
0411: ((MDLDocumentImpl) mdlDoc)
0412: .addElementDefinitionToVisibleList(visibleElementDefinition[i]);
0413: }
0414: }
0415:
0416: visibleMessageDefinition = refDef
0417: .getAllVisibleMessageDefinitions();
0418: if (visibleMessageDefinition != null) {
0419: int defLength = visibleMessageDefinition.length;
0420: for (int i = 0; i < defLength; i++) {
0421: ((MDLDocumentImpl) mdlDoc)
0422: .addMessageDefinitionToVisibleList(visibleMessageDefinition[i]);
0423: }
0424: }
0425:
0426: visibleDatatypeDefinitions = refDef.getAllDatatypeDefinitions();
0427: if (visibleDatatypeDefinitions != null) {
0428: int defLength = visibleDatatypeDefinitions.length;
0429: for (int i = 0; i < defLength; i++) {
0430: ((MDLDocumentImpl) mdlDoc)
0431: .addDatatypeDefinitionToVisibleList(visibleDatatypeDefinitions[i]);
0432: }
0433: }
0434:
0435: visibleProperties = refDef.getAllProperties();
0436: if (visibleProperties != null) {
0437: int defLength = visibleProperties.length;
0438: for (int i = 0; i < defLength; i++) {
0439: ((MDLDocumentImpl) mdlDoc)
0440: .addPropertyToVisibleList(visibleProperties[i]);
0441: }
0442: }
0443:
0444: mdlDoc.addMDLDocReference(docRef);
0445: }
0446:
0447: /**
0448: * MDL Specific Methods: end to process enclude node.
0449: */
0450: private void endIncludeNode() {
0451: visibleFlag = false;
0452: }
0453:
0454: /**
0455: * MDL Specific Methods: process the import node.
0456: *
0457: * @param attributes Attributes
0458: * @throws MDLException MDLException
0459: */
0460: private void startImportNode(Attributes attributes)
0461: throws MDLException {
0462: visibleFlag = true;
0463: IMDLDocReference docRef = new MDLDocReferenceImpl();
0464: docRef.setType(MDLDocReferenceImpl.TYPE_IMPORT);
0465: String defLocation = attributes.getValue("",
0466: MDLDocConstants.MDL_DEFLOCATION);
0467: if (defLocation != null) {
0468: docRef.setDefLocation(defLocation);
0469: IMDLDocument refDef = null;
0470: if (mdlFile != null) {
0471: File baseDir = mdlFile.getParentFile();
0472: refDef = mdlFactory.getMDLDocument(defLocation, baseDir
0473: .getAbsolutePath());
0474: } else {
0475: refDef = mdlFactory.getMDLDocument(defLocation, null);
0476: }
0477:
0478: docRef.setMDLDocument(refDef);
0479:
0480: IElementDefinition[] visibleElementDefinition;
0481: IMessageDefinition[] visibleMessageDefinition;
0482: IDatatypeDefinition[] visibleDatatypeDefinitions;
0483: IProperty[] visibleProperties;
0484:
0485: visibleElementDefinition = refDef
0486: .getAllVisibleElementDefinitions();
0487: if (visibleElementDefinition != null) {
0488: int defLength = visibleElementDefinition.length;
0489: for (int i = 0; i < defLength; i++) {
0490: ((MDLDocumentImpl) mdlDoc)
0491: .addElementDefinitionToVisibleList(visibleElementDefinition[i]);
0492: }
0493: }
0494:
0495: visibleMessageDefinition = refDef
0496: .getAllVisibleMessageDefinitions();
0497: if (visibleMessageDefinition != null) {
0498: int defLength = visibleMessageDefinition.length;
0499: for (int i = 0; i < defLength; i++) {
0500: ((MDLDocumentImpl) mdlDoc)
0501: .addMessageDefinitionToVisibleList(visibleMessageDefinition[i]);
0502: }
0503: }
0504:
0505: visibleDatatypeDefinitions = refDef
0506: .getAllDatatypeDefinitions();
0507: if (visibleDatatypeDefinitions != null) {
0508: int defLength = visibleDatatypeDefinitions.length;
0509: for (int i = 0; i < defLength; i++) {
0510: ((MDLDocumentImpl) mdlDoc)
0511: .addDatatypeDefinitionToVisibleList(visibleDatatypeDefinitions[i]);
0512: }
0513: }
0514:
0515: visibleProperties = refDef.getAllProperties();
0516: if (visibleProperties != null) {
0517: int defLength = visibleProperties.length;
0518: for (int i = 0; i < defLength; i++) {
0519: ((MDLDocumentImpl) mdlDoc)
0520: .addPropertyToVisibleList(visibleProperties[i]);
0521: }
0522: }
0523: }
0524:
0525: String namespace = attributes.getValue("",
0526: MDLDocConstants.MDL_IMPORT_NAMESPACE);
0527: if (namespace != null) {
0528: docRef.setNamespace(namespace);
0529: } else {
0530: docRef.setNamespace("");
0531: }
0532:
0533: if (docRef.getMDLDocument() != null) {
0534: String docRefTargetNamespace = docRef.getMDLDocument()
0535: .getTargetNamespace();
0536: if (docRefTargetNamespace != null
0537: && !(docRefTargetNamespace.equals(docRef
0538: .getNamespace()))) {
0539: throw new MDLException(new Message(
0540: Messages.NAMESPACE_NOT_MATCH, docRef
0541: .getNamespace(), docRefTargetNamespace)
0542: .getMessage());
0543: }
0544: }
0545:
0546: if (defLocation == null && namespace == null) {
0547: throw new MDLException(new Message(
0548: Messages.LOSE_IMPORTNAMESPACE_OR_DEFLOCATION,
0549: MDLDocConstants.MDL_IMPORT_NAMESPACE,
0550: MDLDocConstants.MDL_DEFINITION).getMessage());
0551: }
0552:
0553: mdlDoc.addMDLDocReference(docRef);
0554: }
0555:
0556: /**
0557: * MDL Specific Methods: end to process the import node.
0558: */
0559: private void endImportNode() {
0560: visibleFlag = false;
0561: }
0562:
0563: /**
0564: * MDL Specific Methods: process the message node.
0565: *
0566: * @param attributes Attributes
0567: * @throws MDLException MDLException
0568: */
0569: private void startMessageNode(Attributes attributes)
0570: throws MDLException {
0571: IMessageDefinition msgDef = new MessageDefinitionImpl();
0572: if (mdlDoc.getTargetNamespace() != null) {
0573: msgDef.setNamespaceURI(mdlDoc.getTargetNamespace());
0574: }
0575: String name = attributes.getValue("", MDLDocConstants.MDL_NAME);
0576: if (name != null) {
0577: msgDef.setName(name);
0578: }
0579: String format = attributes.getValue("",
0580: MDLDocConstants.MDL_FORMAT);
0581: if (format != null) {
0582: if (format.equals(MDLDocConstants.MDL_FIXED)) {
0583: IFixedFormatDefinition fixedDef = new FixedFormatDefinitionImpl();
0584: ((FixedFormatDefinitionImpl) fixedDef).mdlDocument = mdlDoc;
0585: fixedDef.setName(IFormatDefinition.FORMAT_FIXED);
0586: msgDef.setFormatDefinition(fixedDef);
0587: getFixedAttributes(fixedDef, attributes);
0588: } else if (format.equals(MDLDocConstants.MDL_VARIABLE)) {
0589: IVariableFormatDefinition varDef = new VariableFormatDefinitionImpl();
0590: ((VariableFormatDefinitionImpl) varDef).mdlDocument = mdlDoc;
0591: varDef.setName(IFormatDefinition.FORMAT_VARIABLE);
0592: msgDef.setFormatDefinition(varDef);
0593: getVariableAttributes(varDef, attributes);
0594: } else {
0595: throw new MDLException(new Message(
0596: Messages.UNSUPPORTED_FORMAT, format)
0597: .getMessage());
0598: }
0599: } else {
0600: String datatype = attributes.getValue("",
0601: MDLDocConstants.MDL_DATATYPE);
0602: msgDef.setDatatype(mdlDoc.getQNameFromReference(datatype));
0603: }
0604: msgDef.setGlobal(true);
0605: loadStack.push(msgDef);
0606: }
0607:
0608: /**
0609: * MDL Specific Methods: end to process the message node.
0610: *
0611: * @throws MDLException MDLException
0612: */
0613: private void endMessageNode() throws MDLException {
0614: if (!(loadStack.peek() instanceof IMessageDefinition)) {
0615: throw new MDLException(new Message(
0616: Messages.EXPECTED_MESSAGE_TOP_OF_STACK)
0617: .getMessage());
0618: }
0619: IMessageDefinition msgDefinition = (MessageDefinitionImpl) loadStack
0620: .pop();
0621:
0622: if (visibleFlag) {
0623: mdlDoc.addMessageDefinition(msgDefinition);
0624: mdlDoc.removeMessageDefinition(msgDefinition
0625: .getNamespaceURI(), msgDefinition.getName());
0626: } else {
0627: mdlDoc.addMessageDefinition(msgDefinition);
0628: }
0629: }
0630:
0631: /**
0632: * MDL Specific Methods: process the element node.
0633: *
0634: * @param attributes Attributes
0635: * @throws MDLException MDLException
0636: */
0637: private void startElementNode(Attributes attributes)
0638: throws MDLException {
0639: if (loadStack.peek() instanceof IMessageDefinition) {
0640: // This defines the content of another element
0641: IElementDefinition elemDef = processContentElement(attributes);
0642: if (elemDef != null) {
0643: // This content element is also a local definition
0644: processElementDefinition(elemDef, attributes, true);
0645: loadStack.push(elemDef);
0646: }
0647: } else if (loadStack.peek() instanceof IContentGroup) {
0648: IElementDefinition iElementDefinition = null;
0649:
0650: // Add the contentNode to the ContentDefinition
0651: IContentGroup contentGroup = (ContentGroupImpl) loadStack
0652: .peek();
0653: IContentElement contentElement = new ContentElementImpl();
0654: ((ContentElementImpl) contentElement).mdlDocument = mdlDoc;
0655: contentGroup.appendChild(contentElement);
0656: getContentNodeAttributes(contentElement, attributes);
0657:
0658: boolean findParentFlag = true;
0659: for (int size = 1; findParentFlag; size++) {
0660: if (loadStack.get(loadStack.size() - size) instanceof IMessageDefinition) {
0661: IMessageDefinition parentDef = (MessageDefinitionImpl) loadStack
0662: .get(loadStack.size() - size);
0663: IFormatDefinition parentFormatDef = parentDef
0664: .getFormatDefinition();
0665: if (parentFormatDef != null) {
0666: String format = parentDef.getFormatDefinition()
0667: .getName();
0668: if (MDLDocConstants.MDL_FIXED.equals(format)) {
0669: IFixedChildAttributes fixedAttr = new FixedChildAttributesImpl();
0670: getFixedContentAttributes(fixedAttr,
0671: attributes);
0672: contentElement
0673: .setFormatChildAttributes(fixedAttr);
0674: } else if (MDLDocConstants.MDL_VARIABLE
0675: .equals(format)) {
0676: IVariableChildAttributes varAttr = new VariableChildAttributesImpl();
0677: getVariableContentAttributes(varAttr,
0678: attributes);
0679: contentElement
0680: .setFormatChildAttributes(varAttr);
0681: }
0682: }
0683: findParentFlag = false;
0684: } else if (loadStack.get(loadStack.size() - size) instanceof IElementDefinition) {
0685: IElementDefinition parentDef = (IElementDefinition) loadStack
0686: .get(loadStack.size() - size);
0687: IFormatDefinition parentFormatDef = parentDef
0688: .getFormatDefinition();
0689: if (parentFormatDef != null) {
0690: String format = parentDef.getFormatDefinition()
0691: .getName();
0692: if (MDLDocConstants.MDL_FIXED.equals(format)) {
0693: IFixedChildAttributes fixedAttr = new FixedChildAttributesImpl();
0694: getFixedContentAttributes(fixedAttr,
0695: attributes);
0696: contentElement
0697: .setFormatChildAttributes(fixedAttr);
0698: } else if (MDLDocConstants.MDL_VARIABLE
0699: .equals(format)) {
0700: IVariableChildAttributes varAttr = new VariableChildAttributesImpl();
0701: getVariableContentAttributes(varAttr,
0702: attributes);
0703: contentElement
0704: .setFormatChildAttributes(varAttr);
0705: }
0706: }
0707: findParentFlag = false;
0708: }
0709: }
0710: // If there was no 'ref' attribute in this element,
0711: // then assume this is a local element definition.
0712: // Create a new ElementDefinition for it.
0713: if (contentElement.getRef() == null
0714: && contentElement.getElementDef() == null) {
0715: iElementDefinition = new ElementDefinitionImpl();
0716: contentElement.setElementDefinition(iElementDefinition);
0717: }
0718:
0719: if (iElementDefinition != null) {
0720: // This content element is also a local definition
0721: processElementDefinition(iElementDefinition,
0722: attributes, true);
0723: loadStack.push(iElementDefinition);
0724: }
0725: } else if (loadStack.peek() instanceof IElementDefinition) {
0726: IElementDefinition elemDef = processContentElement(attributes);
0727: if (elemDef != null) {
0728: // This content element is also a local definition
0729: processElementDefinition(elemDef, attributes, true);
0730: loadStack.push(elemDef);
0731: } else {
0732: elemDef = new ElementDefinitionImpl();
0733: loadStack.push(elemDef);
0734: }
0735: } else {
0736: // This is a global element definition
0737: IElementDefinition elemDef = new ElementDefinitionImpl();
0738: elemDef.setGlobal(true);
0739: processElementDefinition(elemDef, attributes, false);
0740: loadStack.push(elemDef);
0741: }
0742: }
0743:
0744: /**
0745: * MDL Specific Methods: process the element definition.
0746: *
0747: * @param elemDef IElementDefinition
0748: * @param attributes Attributes
0749: * @param local boolean
0750: * @throws MDLException MDLException
0751: */
0752: private void processElementDefinition(IElementDefinition elemDef,
0753: Attributes attributes, boolean local) throws MDLException {
0754: if (mdlDoc.getTargetNamespace() != null) {
0755: if (!local
0756: || (local && (mdlDoc.getElementFormDefault() == MDLDocumentImpl.ELEMENT_FORM_DEFAULT_QUALIFIED))) {
0757: elemDef.setNamespaceURI(mdlDoc.getTargetNamespace());
0758: }
0759: }
0760: String name = attributes.getValue("", MDLDocConstants.MDL_NAME);
0761: if (name != null) {
0762: elemDef.setName(name);
0763: }
0764: String datatype = attributes.getValue("",
0765: MDLDocConstants.MDL_DATATYPE);
0766: if (datatype != null) {
0767: elemDef.setDatatype(mdlDoc.getQNameFromReference(datatype));
0768: }
0769: String format = attributes.getValue("",
0770: MDLDocConstants.MDL_FORMAT);
0771: if (format != null) {
0772: if (format.equals(MDLDocConstants.MDL_FIXED)) {
0773: IFixedFormatDefinition fixedDef = new FixedFormatDefinitionImpl();
0774: ((FixedFormatDefinitionImpl) fixedDef).mdlDocument = mdlDoc;
0775: fixedDef.setName(format);
0776: elemDef.setFormatDefinition(fixedDef);
0777: getFixedAttributes(fixedDef, attributes);
0778: } else if (format.equals(MDLDocConstants.MDL_VARIABLE)) {
0779: IVariableFormatDefinition varDef = new VariableFormatDefinitionImpl();
0780: ((VariableFormatDefinitionImpl) varDef).mdlDocument = mdlDoc;
0781: varDef.setName(format);
0782: elemDef.setFormatDefinition(varDef);
0783: getVariableAttributes(varDef, attributes);
0784: } else {
0785: throw new MDLException(new Message(
0786: Messages.UNSUPPORTED_FORMAT, format)
0787: .getMessage());
0788: }
0789: }
0790: }
0791:
0792: /**
0793: * MDL Specific Methods: end to process the element node.
0794: *
0795: * @throws MDLException MDLException
0796: */
0797: private void endElementNode() throws MDLException {
0798: if (loadStack.peek() instanceof IElementDefinition
0799: && !(loadStack.peek() instanceof IMessageDefinition)) {
0800: IElementDefinition elemDef = (ElementDefinitionImpl) loadStack
0801: .pop();
0802: if (!(loadStack.peek() instanceof IMessageDefinition)
0803: && !(loadStack.peek() instanceof IElementDefinition)
0804: && !(loadStack.peek() instanceof IContentGroup)) {
0805: // if (visibleFlag) {
0806: mdlDoc.addElementDefinition(elemDef);
0807: // } else {
0808: // if (!(loadStack.peek() instanceof IContentGroup)) {
0809: // mdlDoc.addElementDefinition(elemDef);
0810: // }
0811: // }
0812: }
0813: }
0814: }
0815:
0816: /**
0817: * MDL Specific Methods: process the datatype node.
0818: *
0819: * @param attributes Attributes
0820: * @throws MDLException MDLException
0821: */
0822: private void startDatatypeNode(Attributes attributes)
0823: throws MDLException {
0824: IDatatypeDefinition datatypeDefinition = new DatatypeDefinitionImpl();
0825: if (mdlDoc.getTargetNamespace() != null) {
0826: datatypeDefinition.setNamespaceURI(mdlDoc
0827: .getTargetNamespace());
0828: }
0829: String name = attributes.getValue("", MDLDocConstants.MDL_NAME);
0830: if (name != null) {
0831: datatypeDefinition.setName(name);
0832: }
0833: String baseType = attributes.getValue("",
0834: MDLDocConstants.MDL_DATATYPE_BASETYPE);
0835: if (baseType != null) {
0836: datatypeDefinition.setBaseType(mdlDoc
0837: .getQNameFromReference(baseType));
0838: }
0839: mdlDoc.addDatatypeDefinition(datatypeDefinition);
0840: loadStack.push(datatypeDefinition);
0841: }
0842:
0843: /**
0844: * MDL Specific Methods: end to process the datatype node.
0845: *
0846: * @throws MDLException MDLException
0847: */
0848: private void endDatatypeNode() throws MDLException {
0849: if (!(loadStack.peek() instanceof IDatatypeDefinition)) {
0850: throw new MDLException(new Message(
0851: Messages.EXPECTED_DATATYPE_TOP_OF_STACK)
0852: .getMessage());
0853: }
0854: IDatatypeDefinition datatypeDefinition = (IDatatypeDefinition) loadStack
0855: .pop();
0856: if (visibleFlag) {
0857: mdlDoc.addDatatypeDefinition(datatypeDefinition);
0858: mdlDoc.removeDatatypeDefinition(datatypeDefinition
0859: .getNamespaceURI(), datatypeDefinition.getName());
0860: } else {
0861: mdlDoc.addDatatypeDefinition(datatypeDefinition);
0862: }
0863: }
0864:
0865: /**
0866: * MDL Specific Methods: process the property node.
0867: *
0868: * @param attributes Attributes
0869: * @throws MDLException MDLException
0870: */
0871: private void startPropertyNode(Attributes attributes)
0872: throws MDLException {
0873: String name = attributes.getValue("", MDLDocConstants.MDL_NAME);
0874: String value = attributes.getValue("",
0875: MDLDocConstants.MDL_PROPERTY_VALUE);
0876: if (name != null && value != null) {
0877: PropertyImpl property = new PropertyImpl();
0878: property.setName(name);
0879: property.setNamespaceURI(mdlDoc.getTargetNamespace());
0880: property.setValue(handleEscapedProperties(value));
0881: mdlDoc.addProperty(property);
0882: } else {
0883: throw new MDLException(new Message(
0884: Messages.UNSUPPORTED_PROPERTY).getMessage());
0885: }
0886: }
0887:
0888: /**
0889: * MDL Specific Methods: end to process the property node.
0890: */
0891: private void endPropertyNode() {
0892: // Nothing to do.
0893: }
0894:
0895: /**
0896: * MDL Specific Methods: process the description node.
0897: *
0898: * @param attributes Attributes
0899: * @throws MDLException MDLException
0900: */
0901: private void startDescriptionNode(Attributes attributes)
0902: throws MDLException {
0903: stringBuffer.setLength(0);
0904: }
0905:
0906: /**
0907: * MDL Specific Methods: end to process the description node.
0908: *
0909: * @throws MDLException MDLException
0910: */
0911: private void endDescriptionNode() throws MDLException {
0912: if (!stringBuffer.toString().equals("")) {
0913: String description = stringBuffer.toString();
0914: if (loadStack.peek() instanceof IElementDefinition
0915: && !(loadStack.peek() instanceof IMessageDefinition)) {
0916: IElementDefinition elementDefinition = (ElementDefinitionImpl) loadStack
0917: .pop();
0918: elementDefinition.setDescription(description);
0919: loadStack.push(elementDefinition);
0920: } else if (loadStack.peek() instanceof IMessageDefinition) {
0921: IMessageDefinition messageDefinition = (IMessageDefinition) loadStack
0922: .pop();
0923: messageDefinition.setDescription(description);
0924: loadStack.push(messageDefinition);
0925: } else if (loadStack.peek() instanceof IDatatypeDefinition) {
0926: IDatatypeDefinition datatypeDefinition = (IDatatypeDefinition) loadStack
0927: .pop();
0928: datatypeDefinition.setDescription(description);
0929: loadStack.push(datatypeDefinition);
0930: }
0931: }
0932: }
0933:
0934: /**
0935: * MDL Specific Methods: process the group node.
0936: *
0937: * @param attributes Attributes
0938: * @throws MDLException MDLException
0939: */
0940: private void startGroupNode(Attributes attributes)
0941: throws MDLException {
0942: if (loadStack.peek() instanceof IMessageDefinition
0943: || loadStack.peek() instanceof IElementDefinition) {
0944: IContentGroup group = new ContentGroupImpl();
0945: // This content group is also a local definition
0946: processGroupDefinition(group, attributes, true);
0947: loadStack.push(group);
0948: } else {
0949: // This is a global group definition
0950: IContentGroup group = new ContentGroupImpl();
0951: processGroupDefinition(group, attributes, false);
0952: loadStack.push(group);
0953: }
0954: }
0955:
0956: /**
0957: * MDL Specific Methods: process the group definition.
0958: *
0959: * @param contentGroup IContentGroup
0960: * @param attributes Attributes
0961: * @param local boolean
0962: * @throws MDLException MDLException
0963: */
0964: private void processGroupDefinition(IContentGroup contentGroup,
0965: Attributes attributes, boolean local) throws MDLException {
0966:
0967: String name = attributes.getValue("", MDLDocConstants.MDL_NAME);
0968: if (name != null) {
0969: contentGroup.setName(name);
0970: }
0971:
0972: if (mdlDoc.getTargetNamespace() != null) {
0973: if (!local
0974: || (local && (mdlDoc.getElementFormDefault() == IMDLDocument.ELEMENT_FORM_DEFAULT_QUALIFIED))) {
0975: contentGroup.setNamespaceURI(mdlDoc
0976: .getTargetNamespace());
0977: }
0978: }
0979:
0980: String minOccurs = attributes.getValue("",
0981: MDLDocConstants.MDL_CONTENT_MINOCCURS);
0982: if (minOccurs != null) {
0983: try {
0984: int min = Integer.parseInt(minOccurs);
0985: if (min < 0) {
0986: throw new MDLException(
0987: new Message(
0988: Messages.MINOCCURS_INVALID_VALUE,
0989: minOccurs).getMessage());
0990: }
0991: contentGroup.setMinOccurs(min);
0992: } catch (NumberFormatException nfe) {
0993: ErrorUtil.printError(
0994: "Exception in processGroupDefinition(): ", nfe);
0995: throw new MDLException(new Message(
0996: Messages.MINOCCURS_INVALID_VALUE, minOccurs)
0997: .getMessage());
0998: }
0999: }
1000:
1001: String maxOccurs = attributes.getValue("",
1002: MDLDocConstants.MDL_CONTENT_MAXOCCURS);
1003: if (maxOccurs != null) {
1004: if (maxOccurs.equals(MDLDocConstants.MDL_CONTENT_UNBOUNDED)) {
1005: contentGroup.setMaxOccurs(-1);
1006: } else {
1007: try {
1008: int max = Integer.parseInt(maxOccurs);
1009: if (max <= 0) {
1010: throw new MDLException(new Message(
1011: Messages.MAXOCCURS_INVALID_VALUE,
1012: maxOccurs).getMessage());
1013: }
1014: contentGroup.setMaxOccurs(max);
1015: } catch (NumberFormatException nfe) {
1016: ErrorUtil.printError(
1017: "Exception in processGroupDefinition(): ",
1018: nfe);
1019: throw new MDLException(
1020: new Message(
1021: Messages.MAXOCCURS_INVALID_VALUE,
1022: maxOccurs).getMessage());
1023: }
1024: }
1025: }
1026:
1027: }
1028:
1029: /**
1030: * MDL Specific Methods: end to process the group node.
1031: *
1032: * @throws MDLException MDLException
1033: */
1034: private void endGroupNode() throws MDLException {
1035: if (loadStack.peek() instanceof IContentGroup) {
1036: IContentGroup contentGroup = (IContentGroup) loadStack
1037: .pop();
1038: if (!(loadStack.peek() instanceof IMessageDefinition)
1039: && loadStack.peek() instanceof IElementDefinition) {
1040: IElementDefinition elemDef = (IElementDefinition) loadStack
1041: .pop();
1042: // Validate Element Def contains all required attributes here
1043: elemDef.appendChild(contentGroup);
1044: loadStack.push(elemDef);
1045: } else if (!(loadStack.peek() instanceof IMessageDefinition)
1046: && loadStack.peek() instanceof IContentGroup) {
1047: IContentGroup group = (IContentGroup) loadStack.pop();
1048: group.appendChild(contentGroup);
1049: loadStack.push(group);
1050: } else {
1051: IMessageDefinition msgDef = (IMessageDefinition) loadStack
1052: .pop();
1053: msgDef.appendChild(contentGroup);
1054: loadStack.push(msgDef);
1055: }
1056: }
1057: }
1058:
1059: /**
1060: * MDL Specific Methods: process the content node.
1061: *
1062: * @param attributes Attributes
1063: * @throws MDLException MDLException
1064: */
1065: private void startContentNode(Attributes attributes)
1066: throws MDLException {
1067: if (loadStack.peek() instanceof IMessageDefinition) {
1068: IMessageDefinition messageDefinition = (IMessageDefinition) loadStack
1069: .pop();
1070: if (messageDefinition.getFormatDefinition() instanceof IVariableFormatDefinition) {
1071: IVariableFormatDefinition formatDefinition = (VariableFormatDefinitionImpl) messageDefinition
1072: .getFormatDefinition();
1073: String type = attributes.getValue("",
1074: MDLDocConstants.MDL_CONTENT_TYPE);
1075: if (type != null) {
1076: if (type
1077: .equals(MDLDocConstants.MDL_CONTENT_SEQUENCE)) {
1078: formatDefinition
1079: .setType(VariableFormatDefinitionImpl.TYPE_SEQUENCE);
1080: } else if (type
1081: .equals(MDLDocConstants.MDL_CONTENT_CHOICE)) {
1082: formatDefinition
1083: .setType(VariableFormatDefinitionImpl.TYPE_CHOICE);
1084: } else {
1085: throw new MDLException(new Message(
1086: Messages.TYPE_INVALID_VALUE, type)
1087: .getMessage());
1088: }
1089: }
1090: String idMethod = attributes.getValue("",
1091: MDLDocConstants.MDL_CONTENT_IDMETHOD);
1092: if (idMethod != null) {
1093: if (idMethod
1094: .equals(MDLDocConstants.MDL_CONTENT_POSITION)) {
1095: formatDefinition
1096: .setIDMethod(VariableFormatDefinitionImpl.ID_METHOD_POSITION);
1097: } else if (idMethod
1098: .equals(MDLDocConstants.MDL_CONTENT_TAG)) {
1099: formatDefinition
1100: .setIDMethod(VariableFormatDefinitionImpl.ID_METHOD_TAG);
1101: } else {
1102: throw new MDLException(new Message(
1103: Messages.IDMETHOD_INVALID_VALUE,
1104: idMethod).getMessage());
1105: }
1106: }
1107: String tagLength = attributes.getValue("",
1108: MDLDocConstants.MDL_CONTENT_TAGLENGTH);
1109: if (tagLength != null) {
1110: try {
1111: int len = Integer.parseInt(tagLength);
1112: formatDefinition.setTagLength(len);
1113: } catch (NumberFormatException nfe) {
1114: ErrorUtil.printError(
1115: "Exception in startContentNode(): ",
1116: nfe);
1117: throw new MDLException(new Message(
1118: Messages.TAGLENGTH_INVALID_VALUE,
1119: tagLength).getMessage());
1120: }
1121: }
1122: String tagLengthRef = attributes.getValue("",
1123: MDLDocConstants.MDL_CONTENT_TAGLENGTH_REF);
1124: if (tagLengthRef != null) {
1125: formatDefinition.setTagLengthRef(mdlDoc
1126: .getQNameFromReference(tagLengthRef));
1127: }
1128: String tagDelimiter = attributes.getValue("",
1129: MDLDocConstants.MDL_CONTENT_TAGDELIMITER);
1130: if (tagDelimiter != null) {
1131: formatDefinition
1132: .setTagDelimiter(handleEscapedProperties(tagDelimiter));
1133: }
1134: String tagDelimiterRef = attributes.getValue("",
1135: MDLDocConstants.MDL_CONTENT_TAGDELIMITER_REF);
1136: if (tagDelimiterRef != null) {
1137: formatDefinition.setTagDelimiterRef(mdlDoc
1138: .getQNameFromReference(tagDelimiterRef));
1139: }
1140: messageDefinition.setFormatDefinition(formatDefinition);
1141: } else if (messageDefinition.getFormatDefinition() instanceof IFixedFormatDefinition) {
1142: IFixedFormatDefinition formatDefinition = (FixedFormatDefinitionImpl) messageDefinition
1143: .getFormatDefinition();
1144: messageDefinition.setFormatDefinition(formatDefinition);
1145: }
1146: loadStack.push(messageDefinition);
1147: } else if (loadStack.peek() instanceof IElementDefinition) {
1148: IElementDefinition elementDefinition = (IElementDefinition) loadStack
1149: .pop();
1150: if (elementDefinition.getFormatDefinition() instanceof IVariableFormatDefinition) {
1151: IVariableFormatDefinition formatDefinition = (VariableFormatDefinitionImpl) elementDefinition
1152: .getFormatDefinition();
1153: String type = attributes.getValue("",
1154: MDLDocConstants.MDL_CONTENT_TYPE);
1155: if (type != null) {
1156: if (type
1157: .equals(MDLDocConstants.MDL_CONTENT_SEQUENCE)) {
1158: formatDefinition
1159: .setType(VariableFormatDefinitionImpl.TYPE_SEQUENCE);
1160: } else if (type
1161: .equals(MDLDocConstants.MDL_CONTENT_CHOICE)) {
1162: formatDefinition
1163: .setType(VariableFormatDefinitionImpl.TYPE_CHOICE);
1164: } else {
1165: throw new MDLException(new Message(
1166: Messages.TYPE_INVALID_VALUE, type)
1167: .getMessage());
1168: }
1169: }
1170: String idMethod = attributes.getValue("",
1171: MDLDocConstants.MDL_CONTENT_IDMETHOD);
1172: if (idMethod != null) {
1173: if (idMethod
1174: .equals(MDLDocConstants.MDL_CONTENT_POSITION)) {
1175: formatDefinition
1176: .setIDMethod(VariableFormatDefinitionImpl.ID_METHOD_POSITION);
1177: } else if (idMethod
1178: .equals(MDLDocConstants.MDL_CONTENT_TAG)) {
1179: formatDefinition
1180: .setIDMethod(VariableFormatDefinitionImpl.ID_METHOD_TAG);
1181: } else {
1182: throw new MDLException(new Message(
1183: Messages.IDMETHOD_INVALID_VALUE,
1184: idMethod).getMessage());
1185: }
1186: }
1187: String tagLength = attributes.getValue("",
1188: MDLDocConstants.MDL_CONTENT_TAGLENGTH);
1189: if (tagLength != null) {
1190: try {
1191: int len = Integer.parseInt(tagLength);
1192: formatDefinition.setTagLength(len);
1193: } catch (NumberFormatException nfe) {
1194: ErrorUtil.printError(
1195: "Exception in startContentNode(): ",
1196: nfe);
1197: throw new MDLException(new Message(
1198: Messages.TAGLENGTH_INVALID_VALUE,
1199: tagLength).getMessage());
1200: }
1201: }
1202: String tagLengthRef = attributes.getValue("",
1203: MDLDocConstants.MDL_CONTENT_TAGLENGTH_REF);
1204: if (tagLengthRef != null) {
1205: formatDefinition.setTagLengthRef(mdlDoc
1206: .getQNameFromReference(tagLengthRef));
1207: }
1208: String tagDelimiter = attributes.getValue("",
1209: MDLDocConstants.MDL_CONTENT_TAGDELIMITER);
1210: if (tagDelimiter != null) {
1211: formatDefinition
1212: .setTagDelimiter(handleEscapedProperties(tagDelimiter));
1213: }
1214: String tagDelimiterRef = attributes.getValue("",
1215: MDLDocConstants.MDL_CONTENT_TAGDELIMITER_REF);
1216: if (tagDelimiterRef != null) {
1217: formatDefinition.setTagDelimiterRef(mdlDoc
1218: .getQNameFromReference(tagDelimiterRef));
1219: }
1220: elementDefinition.setFormatDefinition(formatDefinition);
1221: } else if (elementDefinition.getFormatDefinition() instanceof IFixedFormatDefinition) {
1222: IFixedFormatDefinition formatDefinition = (FixedFormatDefinitionImpl) elementDefinition
1223: .getFormatDefinition();
1224: elementDefinition.setFormatDefinition(formatDefinition);
1225: }
1226: loadStack.push(elementDefinition);
1227: }
1228: }
1229:
1230: /**
1231: * MDL Specific Methods: process the content element definition.
1232: *
1233: * @param attributes Attributes
1234: * @return IElementDefinition
1235: * @throws MDLException MDLException
1236: */
1237: private IElementDefinition processContentElement(
1238: Attributes attributes) throws MDLException {
1239: IElementDefinition returnElementDef = null;
1240:
1241: IContentElement contentElement = null;
1242: IFormatDefinition parentFormatDef = null;
1243: String format = "";
1244: if (loadStack.peek() instanceof IMessageDefinition) {
1245: // Add the contentNode to the ContentDefinition
1246: IMessageDefinition messageDefinition = (MessageDefinitionImpl) loadStack
1247: .peek();
1248: contentElement = new ContentElementImpl();
1249: ((ContentElementImpl) contentElement).mdlDocument = mdlDoc;
1250: messageDefinition.appendChild(contentElement);
1251: getContentNodeAttributes(contentElement, attributes);
1252: IMessageDefinition parentDef = (MessageDefinitionImpl) loadStack
1253: .get(loadStack.size() - 1);
1254: format = parentDef.getFormatDefinition().getName();
1255: parentFormatDef = parentDef.getFormatDefinition();
1256: } else if (loadStack.peek() instanceof IElementDefinition) {
1257: IElementDefinition elementDefinition = (ElementDefinitionImpl) loadStack
1258: .peek();
1259: contentElement = new ContentElementImpl();
1260: ((ContentElementImpl) contentElement).mdlDocument = mdlDoc;
1261: elementDefinition.appendChild(contentElement);
1262: getContentNodeAttributes(contentElement, attributes);
1263: IElementDefinition parentDef = (ElementDefinitionImpl) loadStack
1264: .get(loadStack.size() - 1);
1265: format = parentDef.getFormatDefinition().getName();
1266: parentFormatDef = parentDef.getFormatDefinition();
1267: }
1268: if (parentFormatDef != null) {
1269: if (MDLDocConstants.MDL_FIXED.equals(format)) {
1270: IFixedChildAttributes fixedAttr = new FixedChildAttributesImpl();
1271: getFixedContentAttributes(fixedAttr, attributes);
1272: contentElement.setFormatChildAttributes(fixedAttr);
1273: } else if (MDLDocConstants.MDL_VARIABLE.equals(format)) {
1274: IVariableChildAttributes varAttr = new VariableChildAttributesImpl();
1275: getVariableContentAttributes(varAttr, attributes);
1276: contentElement.setFormatChildAttributes(varAttr);
1277: }
1278: }
1279:
1280: // If there was no 'ref' attribute in this element,
1281: // then assume this is a local element definition.
1282: // Create a new ElementDefinition for it.
1283: if (contentElement.getRef() == null
1284: && contentElement.getElementDef() == null) {
1285: returnElementDef = new ElementDefinitionImpl();
1286: contentElement.setElementDefinition(returnElementDef);
1287: }
1288:
1289: return returnElementDef;
1290: }
1291:
1292: /**
1293: * MDL Specific Methods: end to process the content node.
1294: *
1295: * @throws MDLException MDLException
1296: */
1297: private void endContentNode() throws MDLException {
1298: if (!(loadStack.peek() instanceof IMessageDefinition)
1299: && !(loadStack.peek() instanceof IElementDefinition)) {
1300: throw new MDLException(new Message(
1301: Messages.EXPECTED_CONTENT_TOP_OF_STACK)
1302: .getMessage());
1303: }
1304: }
1305:
1306: /**
1307: * MDL Specific Methods:
1308: *
1309: * @param fixedDef IFixedFormatDefinition
1310: * @param attributes Attributes
1311: * @throws MDLException MDLException
1312: */
1313: private void getFixedAttributes(IFixedFormatDefinition fixedDef,
1314: Attributes attributes) throws MDLException {
1315: }
1316:
1317: private void getFixedContentAttributes(
1318: IFixedChildAttributes fixedAttr, Attributes attributes)
1319: throws MDLException {
1320: String length = attributes.getValue("",
1321: MDLDocConstants.MDL_FIXED_LENGTH);
1322: if (length != null) {
1323: try {
1324: int len = Integer.parseInt(length);
1325: fixedAttr.setLength(len);
1326: } catch (NumberFormatException nfe) {
1327: ErrorUtil.printError(
1328: "Exception in getFixedContentAttributes(): ",
1329: nfe);
1330: throw new MDLException(new Message(
1331: Messages.FIXEDFORMAT_LENGTH_INVALID_VALUE,
1332: length).getMessage());
1333: }
1334: }
1335: String fillChar = attributes.getValue("",
1336: MDLDocConstants.MDL_FIXED_FILLCHAR);
1337: if (fillChar != null) {
1338: if (fillChar.length() != 1) {
1339: throw new MDLException(new Message(
1340: Messages.FIXEDFORMAT_FILLCHAR_INVALID_VALUE,
1341: fillChar).getMessage());
1342: }
1343: fixedAttr.setFillChar(fillChar.charAt(0));
1344: }
1345: String justification = attributes.getValue("",
1346: MDLDocConstants.MDL_FIXED_JUSTIFICATION);
1347: if (justification != null) {
1348: if (justification.equals(MDLDocConstants.MDL_FIXED_LEFT)) {
1349: fixedAttr
1350: .setJustification(FixedChildAttributesImpl.JUSTIFICATION_LEFT);
1351: } else if (justification
1352: .equals(MDLDocConstants.MDL_FIXED_RIGHT)) {
1353: fixedAttr
1354: .setJustification(FixedChildAttributesImpl.JUSTIFICATION_RIGHT);
1355: } else {
1356: throw new MDLException(
1357: new Message(
1358: Messages.FIXEDFORMAT_JUSTIFICATION_INVALID_VALUE,
1359: justification).getMessage());
1360: }
1361: }
1362: }
1363:
1364: private void getVariableAttributes(
1365: IVariableFormatDefinition varDef, Attributes attributes)
1366: throws MDLException {
1367: String delimiter = attributes.getValue("",
1368: MDLDocConstants.MDL_VARIABLE_DELIMITER);
1369: if (delimiter != null) {
1370: varDef.setDelimiter(handleEscapedProperties(delimiter));
1371: }
1372: String delimiterRef = attributes.getValue("",
1373: MDLDocConstants.MDL_VARIABLE_DELIMITER_REF);
1374: if (delimiterRef != null) {
1375: varDef.setDelimiterRef(mdlDoc
1376: .getQNameFromReference(delimiterRef));
1377: }
1378: String escapeChar = attributes.getValue("",
1379: MDLDocConstants.MDL_VARIABLE_ESCAPECHAR);
1380: if (escapeChar != null) {
1381: escapeChar = handleEscapedProperties(escapeChar);
1382: if (escapeChar.length() != 1) {
1383: throw new MDLException(
1384: new Message(
1385: Messages.VARIABLEFORMAT_ESCAPECHAR_INVALID_VALUE,
1386: escapeChar).getMessage());
1387: }
1388: varDef.setEscapeChar(escapeChar.charAt(0));
1389: }
1390: String escapeCharRef = attributes.getValue("",
1391: MDLDocConstants.MDL_VARIABLE_ESCAPECHAR_REF);
1392: if (escapeCharRef != null) {
1393: varDef.setEscapeCharRef(mdlDoc
1394: .getQNameFromReference(escapeCharRef));
1395: }
1396: String quoteChar = attributes.getValue("",
1397: MDLDocConstants.MDL_VARIABLE_QUOTECHAR);
1398: if (quoteChar != null) {
1399: quoteChar = handleEscapedProperties(quoteChar);
1400: if (quoteChar.length() != 1) {
1401: throw new MDLException(
1402: new Message(
1403: Messages.VARIABLEFORMAT_QUOTECHAR_INVALID_VALUE,
1404: quoteChar).getMessage());
1405: }
1406: varDef.setQuoteChar(quoteChar.charAt(0));
1407: }
1408: String quoteCharRef = attributes.getValue("",
1409: MDLDocConstants.MDL_VARIABLE_QUOTECHAR_REF);
1410: if (quoteCharRef != null) {
1411: varDef.setQuoteCharRef(mdlDoc
1412: .getQNameFromReference(quoteCharRef));
1413: }
1414: String repeatDelimiter = attributes.getValue("",
1415: MDLDocConstants.MDL_VARIABLE_REPEATDELIMITER);
1416: if (repeatDelimiter != null) {
1417: varDef
1418: .setRepeatDelimiter(handleEscapedProperties(repeatDelimiter));
1419: }
1420: String repeatDelimiterRef = attributes.getValue("",
1421: MDLDocConstants.MDL_VARIABLE_REPEATDELIMITER_REF);
1422: if (repeatDelimiterRef != null) {
1423: varDef.setRepeatDelimiterRef(mdlDoc
1424: .getQNameFromReference(repeatDelimiterRef));
1425: }
1426: }
1427:
1428: private void getVariableContentAttributes(
1429: IVariableChildAttributes varAttr, Attributes attributes)
1430: throws MDLException {
1431: String minLength = attributes.getValue("",
1432: MDLDocConstants.MDL_VARIABLE_MINLENGTH);
1433: if (minLength != null) {
1434: try {
1435: int len = Integer.parseInt(minLength);
1436: varAttr.setMinLength(len);
1437: } catch (NumberFormatException nfe) {
1438: ErrorUtil
1439: .printError(
1440: "Exception in getVariableContentAttributes(): ",
1441: nfe);
1442: throw new MDLException(
1443: new Message(
1444: Messages.VARIABLEFORMAT_MINLENGTH_INVALID_VALUE,
1445: minLength).getMessage());
1446: }
1447: }
1448: String maxLength = attributes.getValue("",
1449: MDLDocConstants.MDL_VARIABLE_MAXLENGTH);
1450: if (maxLength != null) {
1451: try {
1452: int len = Integer.parseInt(maxLength);
1453: varAttr.setMaxLength(len);
1454: } catch (NumberFormatException nfe) {
1455: ErrorUtil
1456: .printError(
1457: "Exception in getVariableContentAttributes(): ",
1458: nfe);
1459: throw new MDLException(
1460: new Message(
1461: Messages.VARIABLEFORMAT_MAXLENGTH_INVALID_VALUE,
1462: maxLength).getMessage());
1463: }
1464: }
1465: String tagValue = attributes.getValue("",
1466: MDLDocConstants.MDL_VARIABLE_TAG);
1467: if (tagValue != null) {
1468: varAttr.setTag(handleEscapedProperties(tagValue));
1469: }
1470: }
1471:
1472: private void getContentNodeAttributes(IContentElement contentNode,
1473: Attributes attributes) throws MDLException {
1474: String minOccurs = attributes.getValue("",
1475: MDLDocConstants.MDL_CONTENT_MINOCCURS);
1476: if (minOccurs != null) {
1477: try {
1478: int min = Integer.parseInt(minOccurs);
1479: if (min < 0) {
1480: throw new MDLException(new Message(
1481: Messages.CONTENT_MINOCCURS_INVALID_VALUE,
1482: minOccurs).getMessage());
1483: }
1484: contentNode.setMinOccurs(min);
1485: } catch (NumberFormatException nfe) {
1486: ErrorUtil.printError(
1487: "Exception in getContentNodeAttributes(): ",
1488: nfe);
1489: throw new MDLException(new Message(
1490: Messages.CONTENT_MINOCCURS_INVALID_VALUE,
1491: minOccurs).getMessage());
1492: }
1493: }
1494: String maxOccurs = attributes.getValue("",
1495: MDLDocConstants.MDL_CONTENT_MAXOCCURS);
1496: if (maxOccurs != null) {
1497: if (maxOccurs.equals(MDLDocConstants.MDL_CONTENT_UNBOUNDED)) {
1498: contentNode.setMaxOccurs(-1);
1499: } else {
1500: try {
1501: int max = Integer.parseInt(maxOccurs);
1502: if (max <= 0) {
1503: throw new MDLException(
1504: new Message(
1505: Messages.CONTENT_MAXOCCURS_INVALID_VALUE,
1506: maxOccurs).getMessage());
1507: }
1508: contentNode.setMaxOccurs(max);
1509: } catch (NumberFormatException nfe) {
1510: ErrorUtil
1511: .printError(
1512: "Exception in getContentNodeAttributes(): ",
1513: nfe);
1514: throw new MDLException(new Message(
1515: Messages.CONTENT_MAXOCCURS_INVALID_VALUE,
1516: maxOccurs).getMessage());
1517: }
1518: }
1519: }
1520: String ref = attributes.getValue("",
1521: MDLDocConstants.MDL_CONTENT_REF);
1522: if (ref != null) {
1523: QName qname = mdlDoc.getQNameFromReference(ref);
1524: contentNode.setRef(qname);
1525: }
1526: String elementDef = attributes.getValue("",
1527: MDLDocConstants.MDL_CONTENT_ELEMENT_DEF);
1528: if (elementDef != null) {
1529: QName qname = mdlDoc.getQNameFromReference(elementDef);
1530: contentNode.setElementDef(qname);
1531: String elementDefName = attributes.getValue("",
1532: MDLDocConstants.MDL_NAME);
1533: ((ContentElementImpl) contentNode)
1534: .setElementDefName(elementDefName);
1535: }
1536: }
1537:
1538: private String handleEscapedProperties(String data) {
1539: StringBuffer sb = new StringBuffer();
1540: for (int i = 0; i < data.length(); i++) {
1541: char c = data.charAt(i);
1542: if (c == '\\') {
1543: if (data.length() > i + 1) {
1544: char nextChar = data.charAt(++i);
1545: if (nextChar == 'r') {
1546: sb.append('\r');
1547: } else if (nextChar == 'n') {
1548: sb.append('\n');
1549: } else if (nextChar == 't') {
1550: sb.append('\t');
1551: } else if (nextChar == 'x') {
1552: //Handle hex codes
1553: if (data.length() >= i + 2) {
1554: String hexData = data.substring(i + 1,
1555: i + 3);
1556: int charInt = Integer.valueOf(hexData, 16);
1557: sb.append((char) charInt);
1558: i = i + 2;
1559: }
1560: } else {
1561: //Not a recognized escape sequence,
1562: //Just append it as is.
1563: sb.append("\\" + nextChar);
1564: }
1565: } else {
1566: //Handle a single backslash as is.
1567: sb.append('\\');
1568: }
1569: } else {
1570: sb.append(c);
1571: }
1572:
1573: }
1574: return sb.toString();
1575: }
1576:
1577: // /**
1578: // * Add a MDL documemt to the ElementEdfinition.
1579: // */
1580: // private void addMdlToElementDefinition() {
1581: // IMessageDefinition[] messageDefinition = mdlDoc.getAllMessageDefinitions();
1582: // for (int i = 0; i < messageDefinition.length; i++) {
1583: // if (messageDefinition[i].getChildCount() != 0) {
1584: // messageDefinition[i].setMDLDocument(mdlDoc);
1585: // processMessageChild(messageDefinition[i]);
1586: // }
1587: // }
1588: //
1589: // // set all of the global element definitions to the topNode.
1590: // IElementDefinition[] elementDefinition = mdlDoc.getAllElementDefinitions();
1591: // for (int i = 0; i < elementDefinition.length; i++) {
1592: // processGlobalElement(elementDefinition[i]);
1593: // }
1594: // }
1595:
1596: // /**
1597: // * Process children of message element.
1598: // *
1599: // * @param messageDefinition IMessageDefinition
1600: // */
1601: // private void processMessageChild(IMessageDefinition messageDefinition) {
1602: // for (int i = 0; i < messageDefinition.getChildCount(); i++) {
1603: // if (messageDefinition.getChildAtIndex(i) instanceof IContentElement) {
1604: // IContentElement contentElement = (IContentElement) messageDefinition.getChildAtIndex(i);
1605: // processElementChild(contentElement);
1606: // } else if (messageDefinition.getChildAtIndex(i) instanceof IContentGroup) {
1607: // IContentGroup contentGroup = (IContentGroup) messageDefinition.getChildAtIndex(i);
1608: // processContentGroupChild(contentGroup);
1609: // }
1610: // }
1611: // }
1612:
1613: // /**
1614: // * Process global element.
1615: // *
1616: // * @param elementDefinition IElementDefinition
1617: // */
1618: // private void processGlobalElement(IElementDefinition elementDefinition) {
1619: // elementDefinition.setMDLDocument(mdlDoc);
1620: // for (int i = 0; i < elementDefinition.getChildCount(); i++) {
1621: // if (elementDefinition.getChildAtIndex(i) instanceof IContentElement) {
1622: // IContentElement element = (IContentElement) elementDefinition.getChildAtIndex(i);
1623: // processElementChild(element);
1624: // } else if (elementDefinition.getChildAtIndex(i) instanceof IContentGroup) {
1625: // IContentGroup contentGroup = (IContentGroup) elementDefinition.getChildAtIndex(i);
1626: // processContentGroupChild(contentGroup);
1627: // }
1628: // }
1629: // }
1630:
1631: /**
1632: * Process the children of ContentGroup.
1633: *
1634: * @param contentGroup IContentGroup
1635: */
1636: private void processContentGroupChild(IContentGroup contentGroup) {
1637: for (int i = 0; i < contentGroup.getChildCount(); i++) {
1638: if (contentGroup.getChildAtIndex(i) instanceof IContentElement) {
1639: IContentElement contentElement = (IContentElement) contentGroup
1640: .getChildAtIndex(i);
1641: processElementChild(contentElement);
1642: } else if (contentGroup.getChildAtIndex(i) instanceof IContentGroup) {
1643: IContentGroup group = (IContentGroup) contentGroup
1644: .getChildAtIndex(i);
1645: processContentGroupChild(group);
1646: }
1647: }
1648: }
1649:
1650: /**
1651: * Process the elements and the children of element.
1652: *
1653: * @param contentElement IContentElement
1654: */
1655: private void processElementChild(IContentElement contentElement) {
1656: contentElement.getElementDefinition().setMDLDocument(mdlDoc);
1657: for (int i = 0; i < contentElement.getElementDefinition()
1658: .getChildCount(); i++) {
1659: if (contentElement.getElementDefinition()
1660: .getChildAtIndex(i) instanceof IContentElement) {
1661: IContentElement element = (IContentElement) contentElement
1662: .getElementDefinition().getChildAtIndex(i);
1663: processElementChild(element);
1664: } else if (contentElement.getElementDefinition()
1665: .getChildAtIndex(i) instanceof IContentGroup) {
1666: IContentGroup contentGroup = (IContentGroup) contentElement
1667: .getElementDefinition().getChildAtIndex(i);
1668: processContentGroupChild(contentGroup);
1669: }
1670: }
1671: }
1672: }
|