Examples of StreamWriter


Examples of org.apache.abdera.writer.StreamWriter

    protected void process(Object source,
                           ObjectContext objectContext,
                           SerializationContext context,
                           Conventions conventions) {
        StreamWriter sw = context.getStreamWriter();
        if (!(source instanceof Element))
            return;
        Element element = (Element)source;
        sw.startElement(element.getQName());
        for (QName attr : element.getAttributes())
            sw.writeAttribute(attr, element.getAttributeValue(attr));
        XPath xpath = context.getAbdera().getXPath();
        List<?> children = xpath.selectNodes("node()", element);
        for (Object child : children) {
            if (child instanceof Element) {
                process(child, new ObjectContext(child), context, conventions);
            } else if (child instanceof Comment) {
                Comment comment = (Comment)child;
                sw.writeComment(comment.getText());
            } else if (child instanceof ProcessingInstruction) {
                ProcessingInstruction pi = (ProcessingInstruction)child;
                sw.writePI(pi.getText(), pi.getTarget());
            } else if (child instanceof TextValue) {
                TextValue tv = (TextValue)child;
                sw.writeElementText(tv.getText());
            }
        }
        sw.endElement();
    }
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

    public static void main(String... args) {

        Abdera abdera = Abdera.getInstance();

        StreamWriter out =
            abdera.newStreamWriter().setOutputStream(System.out, "UTF-8").setAutoflush(false).setAutoIndent(true)
                .startDocument().startFeed().writeBase("http://example.org").writeLanguage("en-US")
                .writeId("http://example.org").writeTitle("<Testing 123>").writeSubtitle("Foo").writeAuthor("James",
                                                                                                            null,
                                                                                                            null)
                .writeUpdated(new Date()).writeLink("http://example.org/foo").writeLink("http://example.org/bar",
                                                                                        "self").writeCategory("foo")
                .writeCategory("bar").writeLogo("logo").writeIcon("icon").writeGenerator("1.0",
                                                                                         "http://example.org",
                                                                                         "foo").flush();

        for (int n = 0; n < 100; n++) {
            out.startEntry().writeId("http://example.org/" + n).writeTitle("Entry #" + n).writeUpdated(new Date())
                .writePublished(new Date()).writeEdited(new Date()).writeSummary("This is text summary")
                .writeAuthor("James", null, null).writeContributor("Joe", null, null).startContent("application/xml")
                .startElement("a", "b", "c").startElement("x", "y", "z").writeElementText("This is a test")
                .startElement("a").writeElementText("foo").endElement().startElement("b", "bar")
                .writeAttribute("foo", new Date()).writeAttribute("bar", 123L).writeElementText(123.123).endElement()
                .endElement().endElement().endContent().endEntry().flush();
        }

        out.endFeed().endDocument().flush();

    }
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

    public static void main(String... args) throws Exception {

        Abdera abdera = Abdera.getInstance();

        // demonstrate serialization of a non-annotated java object
        StreamWriter sw = abdera.newStreamWriter();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sw.setOutputStream(out).setAutoIndent(true);
        ConventionSerializationContext c = new ConventionSerializationContext(sw);
        c.setSerializer(MyEntry.class, new EntrySerializer());
        sw.startDocument();
        c.serialize(new MyEntry());
        sw.endDocument();

        // once the object has been serialized, we can see that it's a parseable Atom document
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        Document<Entry> doc = abdera.getParser().parse(in);
        Entry entry = doc.getRoot();
        entry.writeTo(System.out);

        System.out.println();

        // demonstrate serialization using an annotated java object
        // annotations allow the developer to customize the way the
        // object is serialized
        sw = abdera.newStreamWriter();
        out = new ByteArrayOutputStream();
        sw.setOutputStream(out).setAutoIndent(true);
        c = new ConventionSerializationContext(sw);
        sw.startDocument();
        c.serialize(new MyAnnotatedEntry());
        sw.endDocument();

        in = new ByteArrayInputStream(out.toByteArray());
        doc = abdera.getParser().parse(in);
        entry = doc.getRoot();
        entry.writeTo(System.out);
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

*/
public class EntityProviderExample {

    public static void main(String... args) throws Exception {
        Abdera abdera = Abdera.getInstance();
        StreamWriter sw = abdera.newStreamWriter().setOutputStream(System.out).setAutoIndent(true);
        Foo foo = new Foo();
        foo.writeTo(sw);
        sw.close();
    }
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

        return (T)getAbdera().newStreamWriter();
    }

    public <T extends StreamWriter> T newStreamWriter(String name) {
        Class<? extends StreamWriter> _class = getStreamWriters().get(name);
        StreamWriter sw = null;
        if (_class != null) {
            try {
                sw = _class.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(Localizer.sprintf("IMPLEMENTATION.NOT.AVAILABLE", "StreamWriter"), e);
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

        if (response.isCommitted()) {
            log.error("Could not write an error message as the headers & HTTP status were already committed!");
        } else {
            response.setStatus(500);
            StreamWriter sw = getAbdera().newStreamWriter().setOutputStream(response.getOutputStream(), "UTF-8");
            Error.create(sw, 500, message, t);
            sw.close();
        }
    }
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

    return (T)getAbdera().newStreamWriter();
  }

  public <T extends StreamWriter> T newStreamWriter(String name) {
    Class<? extends StreamWriter> _class = getStreamWriters().get(name);
    StreamWriter sw = null;
    if (_class != null) {
      try {
        sw = _class.newInstance();
      } catch (Exception e) {
        throw new RuntimeException(
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

            } else {
                // private post
                entry.setRights(Common.RIGHTS_RESERVED);
                try {
                    StringWriter stringWriter = new StringWriter();
                    StreamWriter writer = Abdera.getInstance()
                            .getWriterFactory().newStreamWriter();
                    writer.setWriter(stringWriter);
                    writer.startEntry();
                    writer.writeId(entry.getId());
                    writer.writeUpdated(entry.getUpdated());
                    writer.writePublished(entry.getPublished());
                    if (predecessor != null) {
                        writer.startElement(Common.PREDECESSOR, Common.NS_URI);
                        writer.writeElementText(predecessor);
                        writer.endElement();
                    }
                    if (options.publicOptions != null) {
                        // these are options that will be publicly visible
                        if (options.publicOptions.status != null) {
                            writer.writeTitle(options.publicOptions.status);
                        } else {
                            writer.writeTitle(""); // empty title
                        }
                        if (options.publicOptions.body != null) {
                            writer.writeSummary(options.publicOptions.body);
                        }
                        if (options.publicOptions.verb != null) {
                            writer.startElement("verb",
                                    "http://activitystrea.ms/spec/1.0/");
                            writer.writeElementText(options.publicOptions.verb);
                            writer.endElement();
                        }
                        if (options.publicOptions.tags != null) {
                            for (String s : options.publicOptions.tags) {
                                writer.writeCategory(s);
                            }
                        }
                        if (options.publicOptions.mentions != null) {
                            for (String s : options.publicOptions.mentions) {
                                writer.startElement("mention", Common.NS_URI,
                                        "trsst");
                                writer.writeElementText(s);
                                writer.endElement();
                            }
                        }
                    } else {
                        writer.writeTitle(""); // empty title
                    }

                    writer.startContent("application/xenc+xml");

                    List<PublicKey> keys = new LinkedList<PublicKey>();
                    for (String id : options.recipientIds) {
                        // for each recipient
                        Feed recipientFeed = pull(id);
                        if (recipientFeed != null) {
                            // fetch encryption key
                            Element e = recipientFeed.getExtension(new QName(
                                    Common.NS_URI, Common.ENCRYPT));
                            if (e == null) {
                                // fall back to signing key
                                e = recipientFeed.getExtension(new QName(
                                        Common.NS_URI, Common.SIGN));
                            }
                            keys.add(Common.toPublicKeyFromX509(e.getText()));
                        }
                    }

                    // enforce the convention:
                    keys.remove(encryptionKeys.getPublic());
                    // move to end if exists;
                    // last encrypted key is for ourself
                    keys.add(encryptionKeys.getPublic());

                    // encrypt content key separately for each recipient
                    for (PublicKey recipient : keys) {
                        byte[] bytes = Crypto.encryptKeyWithIES(contentKey,
                                feed.getUpdated().getTime(), recipient,
                                encryptionKeys.getPrivate());
                        String encoded = new Base64(0, null, true)
                                .encodeToString(bytes);
                        writer.startElement("EncryptedData",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.startElement("CipherData",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.startElement("CipherValue",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.writeElementText(encoded);
                        writer.endElement();
                        writer.endElement();
                        writer.endElement();
                    }

                    // now: encrypt the payload with content key
                    byte[] bytes = encryptElementAES(entry, contentKey);
                    String encoded = new Base64(0, null, true)
                            .encodeToString(bytes);
                    writer.startElement("EncryptedData",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.startElement("CipherData",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.startElement("CipherValue",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.writeElementText(encoded);
                    writer.endElement();
                    writer.endElement();
                    writer.endElement();

                    // done with encrypted elements
                    writer.endContent();
                    writer.endEntry();
                    writer.flush();
                    // this constructed entry now replaces the encrypted
                    // entry
                    entry = (Entry) Abdera.getInstance().getParserFactory()
                            .getParser()
                            .parse(new StringReader(stringWriter.toString()))
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

  protected void process(
    Object source,
    ObjectContext objectContext,
    SerializationContext context,
    Conventions conventions) {
      StreamWriter sw = context.getStreamWriter();
      if (!(source instanceof Element)) return;
      Element element = (Element) source;
      sw.startElement(element.getQName());
      for (QName attr : element.getAttributes())
        sw.writeAttribute(
          attr,
          element.getAttributeValue(attr));
      XPath xpath = context.getAbdera().getXPath();
      List<?> children = xpath.selectNodes("node()", element);
      for (Object child : children) {
        if (child instanceof Element) {
          process(child, new ObjectContext(child), context, conventions);
        } else if (child instanceof Comment) {
          Comment comment = (Comment) child;
          sw.writeComment(comment.getText());
        } else if (child instanceof ProcessingInstruction) {
          ProcessingInstruction pi = (ProcessingInstruction) child;
          sw.writePI(pi.getText(), pi.getTarget());
        } else if (child instanceof TextValue) {
          TextValue tv = (TextValue) child;
          sw.writeElementText(tv.getText());
        }
      }     
      sw.endElement();
  }
View Full Code Here

Examples of org.apache.abdera.writer.StreamWriter

      type = _text != null ? _text.type() : type;
      contentValue = source;
      valueContext = objectContext;
    }
    QName qname = this.qname != null ? this.qname : getQName(objectContext.getAccessor());
    StreamWriter sw = context.getStreamWriter();
    sw.startText(qname,type);
    writeAttributes(
        source,
        objectContext,
        context,
        conventions);

    switch(type) {
      case TEXT:
      case HTML:
        sw.writeElementText(toString(contentValue));
        break;
      case XHTML:
        Div div = null;
        if (contentValue instanceof Div)
          div = (Div) contentValue;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.