Package org.codehaus.stax2

Examples of org.codehaus.stax2.XMLStreamWriter2


                break;
            }

            // Invalid case: leaving the required attr out:
            StringWriter strw = new StringWriter();
            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
            sw.writeStartElement("root");
            try {
                sw.writeEndElement();
                fail(modeDesc+" Expected a validation exception when omitting a #REQUIRED attribute");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // Should not close, since stream is invalid now...
View Full Code Here


            StringWriter strw = new StringWriter();

            /* Ok, as long as we use the right ns prefix... better also
             * output namespace declaration, in non-repairing mode.
             */
            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
            sw.writeStartElement("root");
            if (!repairing) {
                sw.writeNamespace(NS_PREFIX, NS_URI);
            }
            // prefix, uri, localname (for attrs!)
            sw.writeAttribute(NS_PREFIX, NS_URI, "attr", "value");
            sw.writeEndElement();
            sw.writeEndDocument();
            sw.close();
        }
    }
View Full Code Here

            }

            // Invalid case, trying to use "wrong" prefix:

            StringWriter strw = new StringWriter();
            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
            sw.writeStartElement("root");
            if (!repairing) {
                sw.writeNamespace(NS_PREFIX, NS_URI);
            }
            // prefix, uri, localname (for attrs!)
            try {
                sw.writeAttribute(NS_PREFIX2, NS_URI, "attr", "value");
                fail(modeDesc+" Expected a validation exception when trying to add an attribute with wrong ns prefix");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // Should not close, since stream is invalid now...
View Full Code Here

    System.setProperty("javax.xml.stream.XMLEventFactory", "com.ctc.wstx.stax.WstxEventFactory");
   
    initializePackageParsers();

    SMOutputFactory smFactory = new SMOutputFactory(XMLOutputFactory.newInstance());
    XMLStreamWriter2 streamWriter = smFactory.createStax2Writer(stream);
   
    // For this to work, the elements need to be completely empty (no whitespace or line return)
    streamWriter.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS, Boolean.TRUE);

    SMOutputDocument outputDocument = SMOutputFactory.createOutputDocument(
        streamWriter, "1.0", "UTF-8", false);
    // to have the automatic indentation working, we should probably only be using StaxMate classes and not directly StAX
    // outputDocument.setIndentation("\n  ", 1, 1);

    String SBMLNamespace = JSBML.getNamespaceFrom(sbmlDocument.getLevel(),
        sbmlDocument.getVersion());
    SMOutputContext context = outputDocument.getContext();
    context.setIndentation("\n" + createIndentationString(indentCount), 1, 2);
    SMNamespace namespace = context.getNamespace(SBMLNamespace);
    namespace.setPreferredPrefix("");
    outputDocument.addCharacters("\n");

    /*
     * Write a comment to track which program created this SBML file and
     * which version of JSBML was used for this purpose.
     */
    if ((programName != null) && (programName.length() > 0)) {
      String date = String.format("%1$tY-%1$tm-%1$td %1$tR", Calendar.getInstance().getTime());
      String msg = " Created by {0} version {1} on {2} with JSBML version {3}. ";
      outputDocument.addComment(
        MessageFormat.format(msg, programName, (programVersion != null)
              && (programVersion.length() > 0) ? programVersion
              : "?", date, JSBML.getJSBMLDottedVersion()));
      outputDocument.addCharacters("\n");
    }

    SMOutputElement smOutputElement = outputDocument.addElement(namespace,
        sbmlDocument.getElementName());

    SBMLObjectForXML xmlObject = new SBMLObjectForXML();
    xmlObject.setName(sbmlDocument.getElementName());
    xmlObject.setNamespace(SBMLNamespace);
    xmlObject.addAttributes(sbmlDocument.writeXMLAttributes());

    // register all the name spaces of the SBMLDocument to the writer
    Iterator<Map.Entry<String, String>> it = sbmlDocument
        .getSBMLDocumentNamespaces().entrySet().iterator();

    logger.debug(" SBML name spaces size = "
        + sbmlDocument.getSBMLDocumentNamespaces().size());

    while (it.hasNext()) {
      Map.Entry<String, String> entry = it.next();
      if (!entry.getKey().equals("xmlns")) {

        logger.debug(" SBML name spaces: " + entry.getKey() + " = "
            + entry.getValue());

        String namespacePrefix = entry.getKey().substring(
            entry.getKey().indexOf(":") + 1);
        streamWriter.setPrefix(namespacePrefix, entry.getValue());

        logger.debug(" SBML namespaces: " + namespacePrefix + " = "
            + entry.getValue());

      }
    }

    if (sbmlDocument.getDeclaredNamespaces().size() > 0) {
         
      logger.debug(" SBML declared namespaces size = "
          + sbmlDocument.getDeclaredNamespaces().size());

      xmlObject.addAttributes(sbmlDocument.getDeclaredNamespaces());
     
      for (String prefix : sbmlDocument.getDeclaredNamespaces().keySet()) {

        if (!prefix.equals("xmlns")) {

          String namespaceURI = sbmlDocument.getDeclaredNamespaces().get(prefix);

          logger.debug(" SBML name spaces: " + prefix + " = " + namespaceURI);

          String namespacePrefix = prefix.substring(prefix.indexOf(":") + 1);

          streamWriter.setPrefix(namespacePrefix, namespaceURI);

          logger.debug(" SBML namespaces: " + namespacePrefix + " = " + namespaceURI);
        }
      }
    }
View Full Code Here

    StringWriter stream = new StringWriter();
   
    SMOutputFactory smFactory = new SMOutputFactory(WstxOutputFactory.newInstance());

    try {
      XMLStreamWriter2 writer = smFactory.createStax2Writer(stream);

      // For this to work, the elements need to be completely empty (no whitespace or line return)
      writer.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS, Boolean.TRUE);

      // Create an xml fragment to avoid having the xml declaration
      SMRootFragment outputDocument = SMOutputFactory.createOutputFragment(writer);

      // all the sbml element namespaces are registered to the writer in the writeAnnotation method

      // call the writeAnnotation, indicating that we are building an xml fragment
      writeAnnotation(sbase, outputDocument, writer, 0, true);

      writer.writeEndDocument();
      writer.close();

      annotationStr = stream.toString();
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } catch (SBMLException e) {
View Full Code Here

        final AsyncInputFeeder feeder = asyncReader.getInputFeeder();
       
        final byte[] buf = new byte[BLOCK_SIZE];

        // Let's just copy input as is
        XMLStreamWriter2 sw = (XMLStreamWriter2) new OutputFactoryImpl().createXMLStreamWriter(System.out, "UTF-8");
       
        main_loop:
        while (true) {
            int type;

            // May need to feed multiple segments:
            while ((type = asyncReader.next()) == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
                if (!feeder.needMoreInput()) { // sanity check for this test (not needed for real code)
                    throw new IllegalStateException("Got EVENT_INCOMPLETE but not expecting more input");
                }
//                System.out.println("READ-MORE: reader == "+asyncReader.toString());
                int len = in.read(buf);
                if (len < 0) {
                    System.err.println("Error: Unexpected EOF");
                    break main_loop;
                }
                bytes += len;
                feeder.feedInput(buf, 0, len);
            }
            sw.copyEventFromReader(asyncReader, false);
            if (type == END_DOCUMENT) {
                break;
            }
        }
        feeder.endOfInput();
        sw.close();
        return bytes;
    }
View Full Code Here

        final AsyncInputFeeder feeder = asyncReader.getInputFeeder();
       
        final byte[] buf = new byte[BLOCK_SIZE];

        // Let's just copy input as is
        XMLStreamWriter2 sw = (XMLStreamWriter2) new OutputFactoryImpl().createXMLStreamWriter(System.out, "UTF-8");
       
        main_loop:
        while (true) {
            int type;

            // May need to feed multiple segments:
            while ((type = asyncReader.next()) == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
                if (!feeder.needMoreInput()) { // sanity check for this test (not needed for real code)
                    throw new IllegalStateException("Got EVENT_INCOMPLETE but not expecting more input");
                }
//                System.out.println("READ-MORE: reader == "+asyncReader.toString());
                int len = in.read(buf);
                if (len < 0) {
                    System.err.println("Error: Unexpected EOF");
                    break main_loop;
                }
                bytes += len;
                feeder.feedInput(buf, 0, len);
            }
            sw.copyEventFromReader(asyncReader, false);
            if (type == END_DOCUMENT) {
                break;
            }
        }
        feeder.endOfInput();
        sw.close();
        return bytes;
    }
View Full Code Here

    }

    public XMLEventWriter createXMLEventWriter(XMLStreamWriter sw)
        throws XMLStreamException
    {
        XMLStreamWriter2 sw2 = Stax2WriterAdapter.wrapIfNecessary(sw);
        return new Stax2EventWriterImpl(sw2);
    }
View Full Code Here

    }

    public XMLEventWriter createXMLEventWriter(XMLStreamWriter sw)
        throws XMLStreamException
    {
        XMLStreamWriter2 sw2 = Stax2WriterAdapter.wrapIfNecessary(sw);
        return new Stax2EventWriterImpl(sw2);
    }
View Full Code Here

        XMLValidationSchema vs = getValidator(serviceInfo);
        reader2.validateAgainst(vs);
    }

    public void setupValidation(XMLStreamWriter writer, ServiceInfo serviceInfo) throws XMLStreamException {
        XMLStreamWriter2 writer2 = (XMLStreamWriter2)writer;
        XMLValidationSchema vs = getValidator(serviceInfo);
        writer2.validateAgainst(vs);
    }
View Full Code Here

TOP

Related Classes of org.codehaus.stax2.XMLStreamWriter2

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.