Java Code Examples for org.xml.sax.helpers.AttributesImpl#clear()

The following examples show how to use org.xml.sax.helpers.AttributesImpl#clear() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Add the peak identity information into the XML document
 *
 * @param identity
 * @param element
 */
private void fillIdentityElement(PeakIdentity identity, TransformerHandler hd)
    throws SAXException {

  AttributesImpl atts = new AttributesImpl();

  Map<String, String> idProperties = identity.getAllProperties();

  for (Entry<String, String> property : idProperties.entrySet()) {
    String propertyValue = property.getValue();
    atts.clear();
    atts.addAttribute("", "", PeakListElementName.NAME.getElementName(), "CDATA",
        property.getKey());

    hd.startElement("", "", PeakListElementName.IDPROPERTY.getElementName(), atts);
    hd.characters(propertyValue.toCharArray(), 0, propertyValue.length());
    hd.endElement("", "", PeakListElementName.IDPROPERTY.getElementName());
  }

}
 
Example 2
Source Project: ontopia   File: CanonicalXTMWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void write(TopicNameIF basename, int number) {
  AttributesImpl attributes = reifier(basename);
  attributes.addAttribute("", "", AT_NUMBER, null, "" + number);
  
  startElement(EL_NAME, attributes);
  attributes.clear();
  write(basename.getValue());
  writeType(basename);
  write(basename.getScope());

  Object[] variants = basename.getVariants().toArray();
  Arrays.sort(variants, variantComparator);
  for (int ix = 0; ix < variants.length; ix++)
    write((VariantNameIF) variants[ix], ix + 1);

  writeLocators(basename.getItemIdentifiers(), EL_ITEMIDENTIFIERS);

  endElement(EL_NAME);
}
 
Example 3
private void fillInformationElement(PeakInformation information, TransformerHandler hd)
    throws SAXException {
  if (information == null)
    return;

  AttributesImpl atts = new AttributesImpl();

  for (Entry<String, String> property : information.getAllProperties().entrySet()) {
    String value = property.getValue();

    atts.clear();
    atts.addAttribute("", "", PeakListElementName.NAME.getElementName(), "CDATA",
        property.getKey());

    hd.startElement("", "", PeakListElementName.INFO_PROPERTY.getElementName(), atts);
    hd.characters(property.getValue().toCharArray(), 0, value.length());
    hd.endElement("", "", PeakListElementName.INFO_PROPERTY.getElementName());
  }
}
 
Example 4
protected void startXMLDocument() throws SAXException {
    AttributesImpl attrList = new AttributesImpl();
    attrList.clear();
    final ContentHandler contentHandler = getContentHandler();
    if (contentHandler == null) {
        throw new SAXException("No ContentHandler configured for EDIReader");
    }
    contentHandler.startDocument();
    String rootTag = getXMLTags().getRootTag();
    if (isNamespaceEnabled()) {
        contentHandler.startElement(BERRYWORKS_NAMESPACE, rootTag, rootTag, attrList);
    } else {
        startElement(rootTag, attrList);
    }
}
 
Example 5
Source Project: ontopia   File: CanonicalTopicMapWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * PUBLIC: Exports the topic map to the given ContentHandler.
 */

public void export(TopicMapIF topicmap, ContentHandler dh)
  throws IOException, SAXException {

  dh.startDocument();

  AttributesImpl atts = new AttributesImpl();
  atts.addAttribute("", "", "xmlns", CDATA,
                    "http://www.topicmaps.org/cxtm/1.0/");
  dh.startElement("", "", "topicMap", atts);
  atts.clear();

  // topics
  ContextHolder context = createContext(topicmap);
  Iterator<TopicIF> it = context.topicsInOrder(topicmap.getTopics());
  while (it.hasNext()) 
    writeTopic(it.next(), dh, context);

  // associations
  Iterator<AssociationIF> ait = context.assocsInOrder(topicmap.getAssociations());
  while (ait.hasNext()) 
    writeAssociation(ait.next(), dh, context);
      
  dh.endElement("", "", "topicMap");
  dh.endDocument();
}
 
Example 6
Source Project: syncope   File: UserReportlet.java    License: Apache License 2.0 5 votes vote down vote up
private void doExtractConf(final ContentHandler handler) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    handler.startElement("", "", "configurations", null);
    handler.startElement("", "", "userAttributes", atts);

    for (Feature feature : conf.getFeatures()) {
        atts.clear();
        handler.startElement("", "", "feature", atts);
        handler.characters(feature.name().toCharArray(), 0, feature.name().length());
        handler.endElement("", "", "feature");
    }

    for (String attr : conf.getPlainAttrs()) {
        atts.clear();
        handler.startElement("", "", "attribute", atts);
        handler.characters(attr.toCharArray(), 0, attr.length());
        handler.endElement("", "", "attribute");
    }

    for (String derAttr : conf.getDerAttrs()) {
        atts.clear();
        handler.startElement("", "", "derAttribute", atts);
        handler.characters(derAttr.toCharArray(), 0, derAttr.length());
        handler.endElement("", "", "derAttribute");
    }

    for (String virAttr : conf.getVirAttrs()) {
        atts.clear();
        handler.startElement("", "", "virAttribute", atts);
        handler.characters(virAttr.toCharArray(), 0, virAttr.length());
        handler.endElement("", "", "virAttribute");
    }

    handler.endElement("", "", "userAttributes");
    handler.endElement("", "", "configurations");
}
 
Example 7
Source Project: ontopia   File: RelationMapping.java    License: Apache License 2.0 5 votes vote down vote up
protected void outputEntities(Relation rel, ContentHandler dh) throws SAXException {
  AttributesImpl atts = new AttributesImpl();

  for (Entity entity : rel.getEntities()) {
    if (entity.getEntityType() == Entity.TYPE_TOPIC) {
      // <topic>
      if (entity.getId() != null)
        addAttribute(atts, "id", "CDATA", entity.getId());
      addAttribute(atts, "type", "CDATA", entity.getAssociationType());
      dh.startElement("", "", "topic", atts);
      atts.clear();
      
      outputFields(entity, dh);
      
      // </topic>
      dh.endElement("", "", "topic");
      
    } else if (entity.getEntityType() == Entity.TYPE_ASSOCIATION) {
      
      // <association>
      if (entity.getId() != null)
        addAttribute(atts, "id", "CDATA", entity.getId());
      addAttribute(atts, "type", "CDATA", entity.getAssociationType());
      addAttribute(atts, "scope", "CDATA", entity.getScope());
      
      dh.startElement("", "", "association", atts);
      atts.clear();
      
      outputFields(entity, dh);
      
      // </association>
      dh.endElement("", "", "association");
    }
  }
}
 
Example 8
/**
 * Add the row information into the XML document
 *
 * @param row
 * @param element
 * @throws IOException
 */
private void fillRowElement(PeakListRow row, TransformerHandler hd)
    throws SAXException, IOException {

  // <PEAK_IDENTITY>
  PeakIdentity preferredIdentity = row.getPreferredPeakIdentity();
  PeakIdentity[] identities = row.getPeakIdentities();
  AttributesImpl atts = new AttributesImpl();

  for (int i = 0; i < identities.length; i++) {

    if (canceled)
      return;

    atts.addAttribute("", "", PeakListElementName.ID.getElementName(), "CDATA",
        String.valueOf(i));
    atts.addAttribute("", "", PeakListElementName.PREFERRED.getElementName(), "CDATA",
        String.valueOf(identities[i] == preferredIdentity));
    hd.startElement("", "", PeakListElementName.PEAK_IDENTITY.getElementName(), atts);
    fillIdentityElement(identities[i], hd);
    hd.endElement("", "", PeakListElementName.PEAK_IDENTITY.getElementName());
  }

  // <PEAK_INFORMATION>

  // atts.clear();

  if (canceled)
    return;

  // atts.addAttribute("", "", PeakListElementName.ID.getElementName(),
  // "CDATA", "INFORMATION");
  hd.startElement("", "", PeakListElementName.PEAK_INFORMATION.getElementName(), atts);
  fillInformationElement(row.getPeakInformation(), hd);
  hd.endElement("", "", PeakListElementName.PEAK_INFORMATION.getElementName());

  // <PEAK>
  Feature[] peaks = row.getPeaks();
  for (Feature p : peaks) {
    if (canceled)
      return;

    atts.clear();
    String dataFileID = dataFilesIDMap.get(p.getDataFile());
    atts.addAttribute("", "", PeakListElementName.COLUMN.getElementName(), "CDATA", dataFileID);
    atts.addAttribute("", "", PeakListElementName.MZ.getElementName(), "CDATA",
        String.valueOf(p.getMZ()));
    // In the project file, retention time is represented in seconds,
    // for historical reasons
    double rt = p.getRT() * 60d;
    atts.addAttribute("", "", PeakListElementName.RT.getElementName(), "CDATA",
        String.valueOf(rt));
    atts.addAttribute("", "", PeakListElementName.HEIGHT.getElementName(), "CDATA",
        String.valueOf(p.getHeight()));
    atts.addAttribute("", "", PeakListElementName.AREA.getElementName(), "CDATA",
        String.valueOf(p.getArea()));
    atts.addAttribute("", "", PeakListElementName.STATUS.getElementName(), "CDATA",
        p.getFeatureStatus().toString());
    atts.addAttribute("", "", PeakListElementName.CHARGE.getElementName(), "CDATA",
        String.valueOf(p.getCharge()));
    atts.addAttribute("", "", PeakListElementName.PARENT_CHROMATOGRAM_ROW_ID.getElementName(),
        "CDATA",
        p.getParentChromatogramRowID() != null ? String.valueOf(p.getParentChromatogramRowID())
            : "");
    hd.startElement("", "", PeakListElementName.PEAK.getElementName(), atts);

    fillPeakElement(p, hd);
    hd.endElement("", "", PeakListElementName.PEAK.getElementName());
  }

}
 
Example 9
Source Project: syncope   File: GroupReportlet.java    License: Apache License 2.0 4 votes vote down vote up
private void doExtract(final ContentHandler handler, final List<Group> groups) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    for (Group group : groups) {
        atts.clear();

        for (Feature feature : conf.getFeatures()) {
            String type = null;
            String value = null;
            switch (feature) {
                case key:
                    type = ReportXMLConst.XSD_STRING;
                    value = group.getKey();
                    break;

                case name:
                    type = ReportXMLConst.XSD_STRING;
                    value = String.valueOf(group.getName());
                    break;

                case groupOwner:
                    type = ReportXMLConst.XSD_STRING;
                    value = group.getGroupOwner().getKey();
                    break;

                case userOwner:
                    type = ReportXMLConst.XSD_STRING;
                    value = group.getUserOwner().getKey();
                    break;

                default:
            }

            if (type != null && value != null) {
                atts.addAttribute("", "", feature.name(), type, value);
            }
        }

        handler.startElement("", "", "group", atts);

        // Using GroupTO for attribute values, since the conversion logic of
        // values to String is already encapsulated there
        GroupTO groupTO = groupDataBinder.getGroupTO(group, true);

        doExtractAttributes(handler, groupTO, conf.getPlainAttrs(), conf.getDerAttrs(), conf.getVirAttrs());

        // to get resources associated to a group
        if (conf.getFeatures().contains(Feature.resources)) {
            doExtractResources(handler, groupTO);
        }
        //to get users asscoiated to a group is preferred GroupDAO to GroupTO
        if (conf.getFeatures().contains(Feature.users)) {
            handler.startElement("", "", "users", null);

            for (UMembership memb : groupDAO.findUMemberships(group)) {
                atts.clear();

                atts.addAttribute("", "", "key", ReportXMLConst.XSD_STRING,
                        memb.getLeftEnd().getKey());
                atts.addAttribute("", "", "username", ReportXMLConst.XSD_STRING,
                        String.valueOf(memb.getLeftEnd().getUsername()));

                handler.startElement("", "", "user", atts);
                handler.endElement("", "", "user");
            }

            handler.endElement("", "", "users");
        }

        handler.endElement("", "", "group");
    }
}
 
Example 10
/**
 * Add the peaks information into the XML document
 *
 * @param peak
 * @param element
 * @param dataFileID
 * @throws IOException
 */
private void fillPeakElement(Feature peak, TransformerHandler hd)
    throws SAXException, IOException {
  AttributesImpl atts = new AttributesImpl();

  // <REPRESENTATIVE_SCAN>
  hd.startElement("", "", PeakListElementName.REPRESENTATIVE_SCAN.getElementName(), atts);
  hd.characters(String.valueOf(peak.getRepresentativeScanNumber()).toCharArray(), 0,
      String.valueOf(peak.getRepresentativeScanNumber()).length());
  hd.endElement("", "", PeakListElementName.REPRESENTATIVE_SCAN.getElementName());

  // <FRAGMENT_SCAN>
  hd.startElement("", "", PeakListElementName.FRAGMENT_SCAN.getElementName(), atts);
  hd.characters(String.valueOf(peak.getMostIntenseFragmentScanNumber()).toCharArray(), 0,
      String.valueOf(peak.getMostIntenseFragmentScanNumber()).length());
  hd.endElement("", "", PeakListElementName.FRAGMENT_SCAN.getElementName());

  // <ALL_MS2_FRAGMENT_SCANS>
  fillAllMS2FragmentScanNumbers(peak.getAllMS2FragmentScanNumbers(), hd);

  int scanNumbers[] = peak.getScanNumbers();

  // <ISOTOPE_PATTERN>
  IsotopePattern isotopePattern = peak.getIsotopePattern();
  if (isotopePattern != null) {
    atts.addAttribute("", "", PeakListElementName.STATUS.getElementName(), "CDATA",
        String.valueOf(isotopePattern.getStatus()));
    atts.addAttribute("", "", PeakListElementName.DESCRIPTION.getElementName(), "CDATA",
        isotopePattern.getDescription());
    hd.startElement("", "", PeakListElementName.ISOTOPE_PATTERN.getElementName(), atts);
    atts.clear();

    fillIsotopePatternElement(isotopePattern, hd);

    hd.endElement("", "", PeakListElementName.ISOTOPE_PATTERN.getElementName());

  }

  // <MZPEAK>
  atts.addAttribute("", "", PeakListElementName.QUANTITY.getElementName(), "CDATA",
      String.valueOf(scanNumbers.length));
  hd.startElement("", "", PeakListElementName.MZPEAKS.getElementName(), atts);
  atts.clear();

  // <SCAN_ID> <MASS> <HEIGHT>
  ByteArrayOutputStream byteScanStream = new ByteArrayOutputStream();
  DataOutputStream dataScanStream = new DataOutputStream(byteScanStream);

  ByteArrayOutputStream byteMassStream = new ByteArrayOutputStream();
  DataOutputStream dataMassStream = new DataOutputStream(byteMassStream);

  ByteArrayOutputStream byteHeightStream = new ByteArrayOutputStream();
  DataOutputStream dataHeightStream = new DataOutputStream(byteHeightStream);

  float mass, height;
  for (int scan : scanNumbers) {
    dataScanStream.writeInt(scan);
    dataScanStream.flush();
    DataPoint mzPeak = peak.getDataPoint(scan);
    if (mzPeak != null) {
      mass = (float) mzPeak.getMZ();
      height = (float) mzPeak.getIntensity();
    } else {
      mass = 0f;
      height = 0f;
    }
    dataMassStream.writeFloat(mass);
    dataMassStream.flush();
    dataHeightStream.writeFloat(height);
    dataHeightStream.flush();
  }

  byte[] bytes = Base64.encode(byteScanStream.toByteArray());
  hd.startElement("", "", PeakListElementName.SCAN_ID.getElementName(), atts);
  String sbytes = new String(bytes);
  hd.characters(sbytes.toCharArray(), 0, sbytes.length());
  hd.endElement("", "", PeakListElementName.SCAN_ID.getElementName());

  bytes = Base64.encode(byteMassStream.toByteArray());
  hd.startElement("", "", PeakListElementName.MZ.getElementName(), atts);
  sbytes = new String(bytes);
  hd.characters(sbytes.toCharArray(), 0, sbytes.length());
  hd.endElement("", "", PeakListElementName.MZ.getElementName());

  bytes = Base64.encode(byteHeightStream.toByteArray());
  hd.startElement("", "", PeakListElementName.HEIGHT.getElementName(), atts);
  sbytes = new String(bytes);
  hd.characters(sbytes.toCharArray(), 0, sbytes.length());
  hd.endElement("", "", PeakListElementName.HEIGHT.getElementName());

  hd.endElement("", "", PeakListElementName.MZPEAKS.getElementName());
}
 
Example 11
public void adapt() throws XMLStreamException, SAXException {
    QName qname;
    String prefix, localPart;
    AttributesImpl attrs = new AttributesImpl();
    char[] buffer;
    int nsc;
    int nat;

    _handler.startDocument();

    try {

        while (_reader.hasNext()) {
            int event = _reader.next();


            switch(event) {
            case  XMLStreamConstants.START_ELEMENT: {
                // Report namespace events first
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
                        _reader.getNamespaceURI(i));
                }

                // Collect list of attributes
                attrs.clear();
                nat = _reader.getAttributeCount();
                for (int i = 0; i < nat; i++) {
                    QName q = _reader.getAttributeName(i);
                    String qName = _reader.getAttributePrefix(i);
                    if (qName == null || qName == "") {
                        qName = q.getLocalPart();
                    } else {
                        qName = qName + ":" +  q.getLocalPart();
                    }
                    attrs.addAttribute(_reader.getAttributeNamespace(i),
                                       q.getLocalPart(),
                                       qName,
                                       _reader.getAttributeType(i),
                                       _reader.getAttributeValue(i));
                }

                // Report start element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.startElement(_reader.getNamespaceURI(),
                                      localPart,
                                      (prefix.length() > 0) ?
                                          (prefix + ":" + localPart) : localPart,
                                      attrs);
                break;
            }
            case  XMLStreamConstants.END_ELEMENT: {
                // Report end element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.endElement(_reader.getNamespaceURI(),
                                    localPart,
                                    (prefix.length() > 0) ?
                                        (prefix + ":" + localPart) : localPart);

                // Report end namespace events
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS:
                _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.COMMENT:
                _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                break;
            default:
                throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
            } // switch
        }
    }
    catch (XMLStreamException e) {
        _handler.endDocument();     // flush whatever we have
        throw e;
    }

    _handler.endDocument();
}
 
Example 12
public void adapt() throws XMLStreamException, SAXException {
    QName qname;
    String prefix, localPart;
    AttributesImpl attrs = new AttributesImpl();
    char[] buffer;
    int nsc;
    int nat;

    _handler.startDocument();

    try {

        while (_reader.hasNext()) {
            int event = _reader.next();


            switch(event) {
            case  XMLStreamConstants.START_ELEMENT: {
                // Report namespace events first
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
                        _reader.getNamespaceURI(i));
                }

                // Collect list of attributes
                attrs.clear();
                nat = _reader.getAttributeCount();
                for (int i = 0; i < nat; i++) {
                    QName q = _reader.getAttributeName(i);
                    String qName = _reader.getAttributePrefix(i);
                    if (qName == null || qName == "") {
                        qName = q.getLocalPart();
                    } else {
                        qName = qName + ":" +  q.getLocalPart();
                    }
                    attrs.addAttribute(_reader.getAttributeNamespace(i),
                                       q.getLocalPart(),
                                       qName,
                                       _reader.getAttributeType(i),
                                       _reader.getAttributeValue(i));
                }

                // Report start element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.startElement(_reader.getNamespaceURI(),
                                      localPart,
                                      (prefix.length() > 0) ?
                                          (prefix + ":" + localPart) : localPart,
                                      attrs);
                break;
            }
            case  XMLStreamConstants.END_ELEMENT: {
                // Report end element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.endElement(_reader.getNamespaceURI(),
                                    localPart,
                                    (prefix.length() > 0) ?
                                        (prefix + ":" + localPart) : localPart);

                // Report end namespace events
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS:
                _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.COMMENT:
                _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                break;
            default:
                throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
            } // switch
        }
    }
    catch (XMLStreamException e) {
        _handler.endDocument();     // flush whatever we have
        throw e;
    }

    _handler.endDocument();
}
 
Example 13
public void adapt() throws XMLStreamException, SAXException {
    QName qname;
    String prefix, localPart;
    AttributesImpl attrs = new AttributesImpl();
    char[] buffer;
    int nsc;
    int nat;

    _handler.startDocument();

    try {

        while (_reader.hasNext()) {
            int event = _reader.next();


            switch(event) {
            case  XMLStreamConstants.START_ELEMENT: {
                // Report namespace events first
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
                        _reader.getNamespaceURI(i));
                }

                // Collect list of attributes
                attrs.clear();
                nat = _reader.getAttributeCount();
                for (int i = 0; i < nat; i++) {
                    QName q = _reader.getAttributeName(i);
                    String qName = _reader.getAttributePrefix(i);
                    if (qName == null || qName == "") {
                        qName = q.getLocalPart();
                    } else {
                        qName = qName + ":" +  q.getLocalPart();
                    }
                    attrs.addAttribute(_reader.getAttributeNamespace(i),
                                       q.getLocalPart(),
                                       qName,
                                       _reader.getAttributeType(i),
                                       _reader.getAttributeValue(i));
                }

                // Report start element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.startElement(_reader.getNamespaceURI(),
                                      localPart,
                                      (prefix.length() > 0) ?
                                          (prefix + ":" + localPart) : localPart,
                                      attrs);
                break;
            }
            case  XMLStreamConstants.END_ELEMENT: {
                // Report end element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.endElement(_reader.getNamespaceURI(),
                                    localPart,
                                    (prefix.length() > 0) ?
                                        (prefix + ":" + localPart) : localPart);

                // Report end namespace events
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS:
                _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.COMMENT:
                _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                break;
            default:
                throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
            } // switch
        }
    }
    catch (XMLStreamException e) {
        _handler.endDocument();     // flush whatever we have
        throw e;
    }

    _handler.endDocument();
}
 
Example 14
protected void startElement(String name, AttributesImpl attrs, TransformerHandler handler) throws SAXException {
  handler.startElement("", name, name, attrs);
  attrs.clear();
}
 
Example 15
public void adapt() throws XMLStreamException, SAXException {
    QName qname;
    String prefix, localPart;
    AttributesImpl attrs = new AttributesImpl();
    char[] buffer;
    int nsc;
    int nat;

    _handler.startDocument();

    try {

        while (_reader.hasNext()) {
            int event = _reader.next();


            switch(event) {
            case  XMLStreamConstants.START_ELEMENT: {
                // Report namespace events first
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
                        _reader.getNamespaceURI(i));
                }

                // Collect list of attributes
                attrs.clear();
                nat = _reader.getAttributeCount();
                for (int i = 0; i < nat; i++) {
                    QName q = _reader.getAttributeName(i);
                    String qName = _reader.getAttributePrefix(i);
                    if (qName == null || qName == "") {
                        qName = q.getLocalPart();
                    } else {
                        qName = qName + ":" +  q.getLocalPart();
                    }
                    attrs.addAttribute(_reader.getAttributeNamespace(i),
                                       q.getLocalPart(),
                                       qName,
                                       _reader.getAttributeType(i),
                                       _reader.getAttributeValue(i));
                }

                // Report start element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.startElement(_reader.getNamespaceURI(),
                                      localPart,
                                      (prefix.length() > 0) ?
                                          (prefix + ":" + localPart) : localPart,
                                      attrs);
                break;
            }
            case  XMLStreamConstants.END_ELEMENT: {
                // Report end element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.endElement(_reader.getNamespaceURI(),
                                    localPart,
                                    (prefix.length() > 0) ?
                                        (prefix + ":" + localPart) : localPart);

                // Report end namespace events
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS:
                _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.COMMENT:
                _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                break;
            default:
                throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
            } // switch
        }
    }
    catch (XMLStreamException e) {
        _handler.endDocument();     // flush whatever we have
        throw e;
    }

    _handler.endDocument();
}
 
Example 16
protected void emptyElement(String name, AttributesImpl attrs, TransformerHandler handler) throws SAXException {
  startElement(name, attrs, handler);
  endElement(name, handler);
  attrs.clear();
}
 
Example 17
/**
 * Add the row information into the XML document
 * 
 * @param row
 * @param element
 * @throws IOException
 */
private void fillRowElement(PeakListRow row, TransformerHandler hd)
    throws SAXException, IOException {

  // <PEAK_IDENTITY>
  PeakIdentity preferredIdentity = row.getPreferredPeakIdentity();
  PeakIdentity[] identities = row.getPeakIdentities();
  AttributesImpl atts = new AttributesImpl();

  for (int i = 0; i < identities.length; i++) {

    if (canceled)
      return;

    atts.addAttribute("", "", PeakListElementName.ID.getElementName(), "CDATA",
        String.valueOf(i));
    atts.addAttribute("", "", PeakListElementName.PREFERRED.getElementName(), "CDATA",
        String.valueOf(identities[i] == preferredIdentity));
    hd.startElement("", "", PeakListElementName.PEAK_IDENTITY.getElementName(), atts);
    fillIdentityElement(identities[i], hd);
    hd.endElement("", "", PeakListElementName.PEAK_IDENTITY.getElementName());
  }

  // <PEAK_INFORMATION>

  // atts.clear();

  if (canceled)
    return;

  // atts.addAttribute("", "", PeakListElementName.ID.getElementName(),
  // "CDATA", "INFORMATION");
  hd.startElement("", "", PeakListElementName.PEAK_INFORMATION.getElementName(), atts);
  fillInformationElement(row.getPeakInformation(), hd);
  hd.endElement("", "", PeakListElementName.PEAK_INFORMATION.getElementName());

  // <PEAK>
  Feature[] peaks = row.getPeaks();
  for (Feature p : peaks) {
    if (canceled)
      return;

    atts.clear();
    String dataFileID = dataFilesIDMap.get(p.getDataFile());
    atts.addAttribute("", "", PeakListElementName.COLUMN.getElementName(), "CDATA", dataFileID);
    atts.addAttribute("", "", PeakListElementName.MZ.getElementName(), "CDATA",
        String.valueOf(p.getMZ()));
    // In the project file, retention time is represented in seconds,
    // for historical reasons
    double rt = p.getRT() * 60d;
    atts.addAttribute("", "", PeakListElementName.RT.getElementName(), "CDATA",
        String.valueOf(rt));
    atts.addAttribute("", "", PeakListElementName.HEIGHT.getElementName(), "CDATA",
        String.valueOf(p.getHeight()));
    atts.addAttribute("", "", PeakListElementName.AREA.getElementName(), "CDATA",
        String.valueOf(p.getArea()));
    atts.addAttribute("", "", PeakListElementName.STATUS.getElementName(), "CDATA",
        p.getFeatureStatus().toString());
    atts.addAttribute("", "", PeakListElementName.CHARGE.getElementName(), "CDATA",
        String.valueOf(p.getCharge()));
    atts.addAttribute("", "", PeakListElementName.PARENT_CHROMATOGRAM_ROW_ID.getElementName(), "CDATA",
        p.getParentChromatogramRowID() != null ? String.valueOf(p.getParentChromatogramRowID()) : "");
    hd.startElement("", "", PeakListElementName.PEAK.getElementName(), atts);

    fillPeakElement(p, hd);
    hd.endElement("", "", PeakListElementName.PEAK.getElementName());
  }

}
 
Example 18
Source Project: exificient   File: SAXDecoder.java    License: MIT License 4 votes vote down vote up
protected void handleDeferredStartElement(EXIBodyDecoder decoder,
		QNameContext deferredStartElement, final AttributesImpl attributes)
		throws SAXException, IOException, EXIException {

	if (namespaces) {
		List<NamespaceDeclaration> prefixes = decoder
				.getDeclaredPrefixDeclarations();
		if (prefixes != null) {
			for (int i = 0; i < prefixes.size(); i++) {
				NamespaceDeclaration ns = prefixes.get(i);
				contentHandler.startPrefixMapping(ns.prefix,
						ns.namespaceURI);
			}
		}
	}

	/*
	 * the qualified name is required when the namespace-prefixes property
	 * is true, and is optional when the namespace-prefixes property is
	 * false (the default).
	 */
	// String seQNameAsString = Constants.EMPTY_STRING;
	// if (namespacePrefixes) {
	// seQNameAsString = decoder.getElementQNameAsString();
	// }
	/*
	 * Note: it looks like widely used APIs (Xerces, Saxon, ..) provide the
	 * textual qname even when
	 * http://xml.org/sax/features/namespace-prefixes is set to false
	 * http:// sourceforge.net/projects/exificient/forums/forum/856596/topic
	 * /5839494
	 */
	String seQNameAsString = decoder.getElementQNameAsString();

	// start so far deferred start element
	contentHandler.startElement(deferredStartElement.getNamespaceUri(),
			deferredStartElement.getLocalName(), seQNameAsString,
			attributes);

	// clear AT information
	attributes.clear();
}
 
Example 19
/**
 * Create the part of the XML document related to the scans
 * 
 * @param scan
 * @param element
 */
private void fillParameterElement(UserParameter<?, ?> parameter, TransformerHandler hd)
    throws SAXException, IOException {

  AttributesImpl atts = new AttributesImpl();

  RawDataFile dataFiles[] = project.getDataFiles();

  if (parameter instanceof ComboParameter) {
    Object choices[] = ((ComboParameter<?>) parameter).getChoices();

    for (Object choice : choices) {
      hd.startElement("", "", UserParameterElementName.OPTION.getElementName(), atts);

      hd.characters(choice.toString().toCharArray(), 0, choice.toString().length());
      hd.endElement("", "", UserParameterElementName.OPTION.getElementName());
    }

  }

  for (RawDataFile dataFile : dataFiles) {

    Object value = project.getParameterValue(parameter, dataFile);

    if (value == null)
      continue;

    String valueString = String.valueOf(value);
    String dataFileID = dataFilesIDMap.get(dataFile);

    atts.addAttribute("", "", UserParameterElementName.DATA_FILE.getElementName(), "CDATA",
        dataFileID);

    hd.startElement("", "", UserParameterElementName.VALUE.getElementName(), atts);

    atts.clear();

    hd.characters(valueString.toCharArray(), 0, valueString.length());
    hd.endElement("", "", UserParameterElementName.VALUE.getElementName());

  }

}
 
Example 20
/**
 * Create the part of the XML document related to the scans
 *
 * @param scan
 * @param element
 */
private void fillParameterElement(UserParameter<?, ?> parameter, TransformerHandler hd)
    throws SAXException, IOException {

  AttributesImpl atts = new AttributesImpl();

  RawDataFile dataFiles[] = project.getDataFiles();

  if (parameter instanceof ComboParameter) {
    Object choices[] = ((ComboParameter<?>) parameter).getChoices().toArray();

    for (Object choice : choices) {
      hd.startElement("", "", UserParameterElementName.OPTION.getElementName(), atts);

      hd.characters(choice.toString().toCharArray(), 0, choice.toString().length());
      hd.endElement("", "", UserParameterElementName.OPTION.getElementName());
    }

  }

  for (RawDataFile dataFile : dataFiles) {

    Object value = project.getParameterValue(parameter, dataFile);

    if (value == null)
      continue;

    String valueString = String.valueOf(value);
    String dataFileID = dataFilesIDMap.get(dataFile);

    atts.addAttribute("", "", UserParameterElementName.DATA_FILE.getElementName(), "CDATA",
        dataFileID);

    hd.startElement("", "", UserParameterElementName.VALUE.getElementName(), atts);

    atts.clear();

    hd.characters(valueString.toCharArray(), 0, valueString.length());
    hd.endElement("", "", UserParameterElementName.VALUE.getElementName());

  }

}