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: VariableElementParser.java 3622 2006-12-12 03:04:08Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.parser;
025:
026: import java.io.InputStream;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.w3c.dom.Element;
031:
032: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
033: import com.bostechcorp.cbesb.common.mdl.IVariableFormatDefinition;
034:
035: /**
036: * The VariableElementParser class implements ElementParser. It is used to parse
037: * elements defined as variable format.
038: *
039: * Basic flow of the parse method: 1. Set the variable format attributes in the
040: * VariableDataScanner (delimiter, escape character, etc). 2. If the
041: * VariableFormatDefinition specifies an IDMethod = tag, call parseTag, if the
042: * IDMethod = position, call parsePosition.
043: *
044: * Basic flow of parseTag method: 1. Create a new Element with the name and
045: * namespace provided in the ElementDefinition. 2. Initialize Finite State
046: * Machine with content model defined by tags in ElementDefinition. 3. Call
047: * getNextElement in the VariableDataScanner to retrieve the next piece of data
048: * from the InputStream. 4. Repeatedly feed in the element data to the FSM until
049: * the end of the InputStream is reached. 5. If the FSM matches all elements
050: * successfully, then do the following for each successfully matched element: a.
051: * Create a new InputStream containing this data only. b. Get the
052: * ElementDefinition from the ContentElement that defines the child. c. Get an
053: * instance of ElementParser from ElementParserFactory using the child
054: * ElementDefinition as input. d. Call the parse method of this ElementParser
055: * passing in the new InputStream and child ElementDefinition. e. Append the
056: * returned DOM Element to the Element created in step 1.
057: *
058: * Basic flow of parsePosition method: 1. Create a new Element with the name and
059: * namespace provided in the ElementDefinition. 2. Loop over all of the child
060: * ContentElement nodes defined in the ElementDefinition. For each child, do the
061: * following: a. Call getNextElement in the VariableDataScanner to retrieve the
062: * next piece of data from the InputStream. b. If the getNextElement method
063: * returned multiple strings, then there were multiple occurrences of this
064: * element in the data. For each Occurrence, do the following: i. Create a new
065: * InputStream containing this data only. ii. Get the ElementDefinition from the
066: * ContentElement that defines the child. iii. Get an instance of ElementParser
067: * from ElementParserFactory using the child ElementDefinition as input. iv.
068: * Call the parse method of this ElementParser passing in the new InputStream
069: * and child ElementDefinition. v. Append the returned DOM Element to the
070: * Element created in step 1.
071: *
072: */
073: public abstract class VariableElementParser implements IElementParser {
074:
075: private static final Log logger = LogFactory
076: .getLog(VariableElementParser.class);
077:
078: protected VariableDataScanner scanner = new VariableDataScanner();
079:
080: protected void setupScanner(IElementDefinition elementDefinition) {
081: IVariableFormatDefinition varDef = (IVariableFormatDefinition) elementDefinition
082: .getFormatDefinition();
083: scanner.setDelimiter(varDef.getResolvedDelimiter());
084: scanner.setRepeatDelimiter(varDef.getResolvedRepeatDelimiter());
085: scanner.setEscapeChar(varDef.getResolvedEscapeChar());
086: scanner.setQuoteChar(varDef.getResolvedQuoteChar());
087: scanner.setTagLength(varDef.getResolvedTagLength());
088: scanner.setTagDelimiter(varDef.getResolvedTagDelimiter());
089:
090: if (logger.isDebugEnabled()) {
091: logger.debug("VariableElementParser scanner settings:");
092: logger.debug("Delimiter = ["
093: + getHexString(scanner.getDelimiter()) + "]");
094: logger.debug("RepeatDelimiter = ["
095: + getHexString(scanner.getRepeatDelimiter()) + "]");
096: logger.debug("EscapeChar = ["
097: + getHexString(scanner.getEscapeChar()) + "]");
098: logger.debug("QuoteChar = ["
099: + getHexString(scanner.getQuoteChar()) + "]");
100: logger.debug("Tag Length = [" + scanner.getTagLength()
101: + "]");
102: logger.debug("Tag Delimiter = ["
103: + getHexString(scanner.getTagDelimiter()) + "]");
104: }
105: }
106:
107: protected String getHexString(String data) {
108: StringBuffer sb = new StringBuffer();
109: if (data != null) {
110: for (int i = 0; i < data.length(); i++) {
111: char c = data.charAt(i);
112: String hex = Integer.toHexString(c);
113: sb.append(hex);
114: }
115: }
116: return sb.toString();
117: }
118:
119: protected String getHexString(char c) {
120: return Integer.toHexString(c);
121: }
122:
123: public abstract Element parse(InputStream is,
124: IElementDefinition elementDefinition)
125: throws ParserException;
126:
127: }
|