Package org.apache.abdera.writer

Examples of org.apache.abdera.writer.StreamWriter


    static Calendar cal_now = Calendar.getInstance();

    @Test
    public void testSimple() throws Exception {
        Abdera abdera = Abdera.getInstance();
        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();

        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        Document<Entry> doc = abdera.getParser().parse(in);
        Entry entry = doc.getRoot();
        assertEquals("tag:example.org,2008:foo", entry.getId().toString());
View Full Code Here


    }

    @Test
    public void testAnnotated() throws Exception {
        Abdera abdera = Abdera.getInstance();
        StreamWriter sw = abdera.newStreamWriter();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sw.setOutputStream(out).setAutoIndent(true);
        ConventionSerializationContext c = new ConventionSerializationContext(sw);
        sw.startDocument();
        c.serialize(new MyAnnotatedEntry());
        sw.endDocument();

        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        Document<Entry> doc = abdera.getParser().parse(in);
        Entry entry = doc.getRoot();
        assertEquals("tag:example.org,2008:foo", entry.getId().toString());
View Full Code Here

    protected void process(Object source,
                           ObjectContext objectContext,
                           SerializationContext context,
                           Conventions conventions) {

        StreamWriter sw = context.getStreamWriter();
        Link _link = objectContext.getAnnotation(Link.class);

        String rel = null;
        AccessibleObject accessor = objectContext.getAccessor(Rel.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                rel = toString(value);
        }
        if (rel == null) {
            Rel _rel = objectContext.getAnnotation(Rel.class);
            if (_rel != null && !_rel.value().equals(DEFAULT)) {
                rel = _rel.value();
            }
        }
        if (rel == null && _link != null && !_link.rel().equals(DEFAULT)) {
            rel = _link.rel();
        }
        if (rel != null)
            sw.writeAttribute("rel", rel);

        String type = null;
        accessor = objectContext.getAccessor(MediaType.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                type = toString(value);
        }
        if (type == null) {
            MediaType _type = objectContext.getAnnotation(MediaType.class);
            if (_type != null && !_type.value().equals(DEFAULT)) {
                type = _type.value();
            }
        }
        if (type == null && _link != null && !_link.type().equals(DEFAULT)) {
            type = _link.type();
        }
        if (type != null)
            sw.writeAttribute("type", type);

        String title = null;
        accessor = objectContext.getAccessor(Title.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                title = toString(value);
        }
        if (title == null && _link != null && !_link.title().equals(DEFAULT)) {
            title = _link.title();
        }
        if (title != null)
            sw.writeAttribute("title", title);

        String hreflang = null;
        accessor = objectContext.getAccessor(HrefLanguage.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                hreflang = toString(value);
        }
        if (hreflang == null) {
            HrefLanguage _hreflang = objectContext.getAnnotation(HrefLanguage.class);
            if (_hreflang != null && !_hreflang.value().equals(DEFAULT)) {
                hreflang = _hreflang.value();
            }
        }
        if (hreflang == null && _link != null && !_link.hreflang().equals(DEFAULT)) {
            hreflang = _link.hreflang();
        }
        if (hreflang != null)
            sw.writeAttribute("hreflang", hreflang);

        String href = null;
        accessor = objectContext.getAccessor(Value.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                href = toString(value);
        }
        if (href == null)
            href = toString(source);
        if (href != null)
            sw.writeAttribute("href", href);

        writeAttributes(source, objectContext, context, conventions);
        writeExtensions(source, objectContext, context, conventions);
    }
View Full Code Here

    protected void init(Object source,
                        ObjectContext objectContext,
                        SerializationContext context,
                        Conventions conventions) {
        QName qname = this.qname != null ? this.qname : getQName(objectContext.getAccessor());
        StreamWriter sw = context.getStreamWriter();
        sw.startElement(qname);
    }
View Full Code Here

    protected void init(Object source,
                        ObjectContext objectContext,
                        SerializationContext context,
                        Conventions conventions) {
        QName qname = this.qname != null ? this.qname : getQName(objectContext.getAccessor());
        StreamWriter sw = context.getStreamWriter();
        sw.startElement(qname);
        writeCommon(source, objectContext, context, conventions);

    }
View Full Code Here

    protected void process(Object source,
                           ObjectContext objectContext,
                           SerializationContext context,
                           Conventions conventions) {
        writeAttributes(source, objectContext, context, conventions);
        StreamWriter sw = context.getStreamWriter();
        Generator _generator = objectContext.getAnnotation(Generator.class);

        String uri = null;
        AccessibleObject accessor = objectContext.getAccessor(URI.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                uri = toString(value);
        }
        if (uri == null) {
            URI _uri = objectContext.getAnnotation(URI.class);
            if (_uri != null && !_uri.value().equals(DEFAULT)) {
                uri = _uri.value();
            }
        }
        if (uri == null && _generator != null && !_generator.uri().equals(DEFAULT)) {
            uri = _generator.uri();
        }
        if (uri != null)
            sw.writeAttribute("uri", uri);

        String version = null;
        accessor = objectContext.getAccessor(URI.class, conventions);
        if (accessor != null) {
            Object value = eval(accessor, source);
            if (value != null)
                version = toString(value);
        }
        if (version == null) {
            URI _version = objectContext.getAnnotation(URI.class);
            if (_version != null && !_version.value().equals(DEFAULT)) {
                version = _version.value();
            }
        }
        if (version == null && _generator != null && !_generator.version().equals(DEFAULT)) {
            version = _generator.version();
        }
        if (version != null)
            sw.writeAttribute("version", version);

        writeTextValue(source, objectContext, context, conventions);
    }
View Full Code Here

  }

  public Object transformTuple(Object[] tuple, String[] aliases) {
    try {
      if (tuple.length == 1) {       
        StreamWriter sw = abdera.newStreamWriter();
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          sw.setOutputStream(out).setAutoIndent(true);
          ConventionSerializationContext c =
            new ConventionSerializationContext(sw);
          c.setSerializer(tuple[0].getClass(), new EntrySerializer());
          sw.startDocument();
          c.serialize(tuple[0]);
          sw.endDocument();
         
          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
          Document<Entry> doc = abdera.getParser().parse(in);
          Entry entry = doc.getRoot();
          if (ProviderHelper.getEditUriFromEntry(entry) == null) {
View Full Code Here

    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

    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

    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

TOP

Related Classes of org.apache.abdera.writer.StreamWriter

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.