示例#1
0
  /* (non-Javadoc)
   * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
   */
  public void parse(InputSource inputSource) throws IOException, SAXException {
    if (contentHandler == null) {
      throw new IllegalStateException("'contentHandler' not set.  Cannot parse Record stream.");
    }
    if (execContext == null) {
      throw new IllegalStateException("'execContext' not set.  Cannot parse Record stream.");
    }

    try {
      Reader recordReader;

      // Create the record parser....
      RecordParser recordParser = parserFactory.newRecordParser();
      recordParser.setRecordParserFactory(parserFactory);
      recordParser.setDataSource(inputSource);

      // Start the document and add the root "record-set" element...
      contentHandler.startDocument();
      contentHandler.startElement(
          XMLConstants.NULL_NS_URI, rootElementName, StringUtils.EMPTY, EMPTY_ATTRIBS);

      // Output each of the CVS line entries...
      int lineNumber = 0;

      Record record = recordParser.nextRecord();
      while (record != null) {
        lineNumber++; // First line is line "1"

        if (record != null) {
          List<Field> recordFields = record.getFields();

          if (indent) {
            contentHandler.characters(INDENT_LF, 0, 1);
            contentHandler.characters(INDENTCHARS, 0, 1);
          }

          AttributesImpl attrs = new AttributesImpl();
          attrs.addAttribute(
              XMLConstants.NULL_NS_URI,
              RECORD_NUMBER_ATTR,
              RECORD_NUMBER_ATTR,
              "xs:int",
              Integer.toString(lineNumber));

          RecordMetaData recordMetaData = record.getRecordMetaData();
          if (recordFields.size() < recordMetaData.getUnignoredFieldCount()) {
            attrs.addAttribute(
                XMLConstants.NULL_NS_URI,
                RECORD_TRUNCATED_ATTR,
                RECORD_TRUNCATED_ATTR,
                "xs:boolean",
                Boolean.TRUE.toString());
          }

          contentHandler.startElement(
              XMLConstants.NULL_NS_URI, record.getName(), StringUtils.EMPTY, attrs);
          for (Field recordField : recordFields) {
            String fieldName = recordField.getName();

            if (indent) {
              contentHandler.characters(INDENT_LF, 0, 1);
              contentHandler.characters(INDENTCHARS, 0, 2);
            }

            contentHandler.startElement(
                XMLConstants.NULL_NS_URI, fieldName, StringUtils.EMPTY, EMPTY_ATTRIBS);

            String value = recordField.getValue();
            contentHandler.characters(value.toCharArray(), 0, value.length());
            contentHandler.endElement(XMLConstants.NULL_NS_URI, fieldName, StringUtils.EMPTY);
          }

          if (indent) {
            contentHandler.characters(INDENT_LF, 0, 1);
            contentHandler.characters(INDENTCHARS, 0, 1);
          }

          contentHandler.endElement(XMLConstants.NULL_NS_URI, record.getName(), StringUtils.EMPTY);
        }

        record = recordParser.nextRecord();
      }

      if (indent) {
        contentHandler.characters(INDENT_LF, 0, 1);
      }

      // Close out the "csv-set" root element and end the document..
      contentHandler.endElement(XMLConstants.NULL_NS_URI, rootElementName, StringUtils.EMPTY);
      contentHandler.endDocument();
    } finally {
      // These properties need to be reset for every execution (e.g. when reader is pooled).
      contentHandler = null;
      execContext = null;
    }
  }