001: /**
002: *
003: */package com.bostechcorp.cbesb.runtime.parser;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.InputStreamReader;
009:
010: import org.w3c.dom.Element;
011:
012: import com.bostechcorp.cbesb.common.i18n.Message;
013: import com.bostechcorp.cbesb.common.i18n.Messages;
014: import com.bostechcorp.cbesb.common.mdl.IContentElement;
015: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
016: import com.bostechcorp.cbesb.common.mdl.IContentNode;
017: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
018: import com.bostechcorp.cbesb.common.util.ErrorUtil;
019: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataParser;
020:
021: /**
022: * @author mpreston
023: *
024: */
025: public class VariablePositionElementParser extends
026: VariableElementParser implements IElementParser {
027:
028: public Element parse(InputStream is,
029: IElementDefinition elementDefinition)
030: throws ParserException {
031:
032: setupScanner(elementDefinition);
033:
034: try {
035: InputStreamReader isReader = new InputStreamReader(is,
036: "utf-8");
037: Element element = LegacyDataParser.getDocument()
038: .createElementNS(
039: elementDefinition.getNamespaceURI(),
040: elementDefinition.getName());
041:
042: // Loop through the element definition
043: for (int i = 0; i < elementDefinition.getChildCount(); i++) {
044: IContentNode contentNode = elementDefinition
045: .getChildAtIndex(i);
046: // The message is content element
047: if (contentNode instanceof IContentElement) {
048: IContentElement contentElement = (IContentElement) contentNode;
049: // Get Child Element Definition
050: IElementDefinition childElementDefinition = contentElement
051: .getResolvedElementDef();
052:
053: scanner.reset(elementDefinition);
054: String[] childData = scanner
055: .getNextElement(isReader);
056:
057: // validation to check min occurs
058: if (childData.length < contentElement
059: .getMinOccurs()) {
060: throw new ParserException(
061: "There is no data for the required element '"
062: + childElementDefinition
063: .getNamespaceURI()
064: + ":"
065: + childElementDefinition
066: .getName() + "'.");
067: }
068:
069: // validation to check max occurs
070: if ((contentElement.getMaxOccurs() != -1)
071: && (childData.length > contentElement
072: .getMaxOccurs())) {
073: throw new ParserException("There are "
074: + childData.length
075: + " ocurrences found for the element '"
076: + childElementDefinition
077: .getNamespaceURI() + ":"
078: + childElementDefinition.getName()
079: + "' with max occurences allowed ("
080: + contentElement.getMaxOccurs() + ").");
081: }
082:
083: IElementParser childParser = ElementParserFactory
084: .instance().getElementParser(
085: childElementDefinition);
086: for (int j = 0; j < childData.length; j++) {
087: Element child = childParser.parse(
088: new ByteArrayInputStream(childData[j]
089: .getBytes("utf-8")),
090: childElementDefinition);
091: element.appendChild(child);
092: }
093: } else if (contentNode instanceof IContentGroup) {
094: // TODO LU what?
095: }
096: }
097:
098: return element;
099:
100: } catch (IOException e) {
101: throw new ParserException(
102: "IOException caught in VariablePositionElementParser class when converting to UTF-8 string in parsing element '"
103: + elementDefinition.getNamespaceURI()
104: + ":"
105: + elementDefinition.getName() + "'", e);
106:
107: // ErrorUtil.printError("Exception in VariablePositionElementParser(): ",e);
108: // throw new ParserException(new Message(
109: // Messages.VARIABLE_PARSER_POSITION_ERROR, e.getMessage())
110: // .getMessage());
111: }
112:
113: }
114:
115: }
|