コード例 #1
0
  public InputStream addAtomInlinecount(final InputStream feed, final int count) throws Exception {
    final XMLEventReader reader = getEventReader(feed);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    try {

      final XMLElement feedElement =
          extractElement(reader, writer, Collections.<String>singletonList("feed"), 0, 1, 1)
              .getValue();

      writer.add(feedElement.getStart());
      addAtomElement(
          IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count), Constants.ENCODING),
          writer);
      writer.add(feedElement.getContentReader());
      writer.add(feedElement.getEnd());

      while (reader.hasNext()) {
        writer.add(reader.nextEvent());
      }

    } finally {
      writer.flush();
      writer.close();
      reader.close();
      IOUtils.closeQuietly(feed);
    }

    return new ByteArrayInputStream(bos.toByteArray());
  }
コード例 #2
0
  @Override
  public InputStream addEditLink(final InputStream content, final String title, final String href)
      throws Exception {

    final ByteArrayOutputStream copy = new ByteArrayOutputStream();
    IOUtils.copy(content, copy);
    IOUtils.closeQuietly(content);

    XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEventWriter writer = getEventWriter(bos);

    final String editLinkElement =
        String.format("<link rel=\"edit\" title=\"%s\" href=\"%s\" />", title, href);

    try {
      // check edit link existence
      extractElement(
          reader,
          writer,
          Collections.<String>singletonList(Constants.get(ConstantKey.LINK)),
          Collections.<Map.Entry<String, String>>singletonList(
              new AbstractMap.SimpleEntry<String, String>("rel", "edit")),
          false,
          0,
          -1,
          -1);

      addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer);
      writer.add(reader);

    } catch (Exception e) {
      reader.close();
      reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

      bos = new ByteArrayOutputStream();
      writer = getEventWriter(bos);

      final XMLElement entryElement =
          extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1)
              .getValue();

      writer.add(entryElement.getStart());

      addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer);

      writer.add(entryElement.getContentReader());
      writer.add(entryElement.getEnd());

      writer.add(reader);

      writer.flush();
      writer.close();
    } finally {
      reader.close();
    }

    return new ByteArrayInputStream(bos.toByteArray());
  }
コード例 #3
0
  private InputStream addAtomContent(
      final InputStream content, final String title, final String href) throws Exception {

    final ByteArrayOutputStream copy = new ByteArrayOutputStream();
    IOUtils.copy(content, copy);

    IOUtils.closeQuietly(content);

    XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEventWriter writer = getEventWriter(bos);

    try {
      // check edit link existence
      XMLElement contentElement =
          extractElement(reader, writer, Collections.<String>singletonList("content"), 0, 2, 2)
              .getValue();
      writer.add(contentElement.getStart());
      writer.add(contentElement.getContentReader());
      writer.add(contentElement.getEnd());
      writer.add(reader);
    } catch (Exception e) {
      reader.close();
      reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

      bos = new ByteArrayOutputStream();
      writer = getEventWriter(bos);

      if (isMediaContent(title)) {
        final XMLElement entryElement =
            extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1)
                .getValue();

        writer.add(entryElement.getStart());
        writer.add(entryElement.getContentReader());

        addAtomElement(
            IOUtils.toInputStream(
                String.format("<content type=\"*/*\" src=\"%s/$value\" />", href)),
            writer);

        writer.add(entryElement.getEnd());
      } else {
        try {
          final XMLElement entryElement =
              extractElement(
                      reader,
                      writer,
                      Collections.<String>singletonList(Constants.get(ConstantKey.PROPERTIES)),
                      0,
                      2,
                      3)
                  .getValue();

          addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\">"), writer);

          writer.add(entryElement.getStart());
          writer.add(entryElement.getContentReader());
          writer.add(entryElement.getEnd());

          addAtomElement(IOUtils.toInputStream("</content>"), writer);
        } catch (Exception nf) {
          reader.close();
          reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

          bos = new ByteArrayOutputStream();
          writer = getEventWriter(bos);

          final XMLElement entryElement =
              extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1)
                  .getValue();
          writer.add(entryElement.getStart());
          writer.add(entryElement.getContentReader());

          addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\"/>"), writer);

          writer.add(entryElement.getEnd());
        }
      }

      writer.add(reader);

      writer.flush();
      writer.close();
    } finally {
      reader.close();
    }

    return new ByteArrayInputStream(bos.toByteArray());
  }
コード例 #4
0
  /** {@inheritDoc } */
  @Override
  protected NavigationLinks retrieveNavigationInfo(final String entitySetName, final InputStream is)
      throws Exception {

    final NavigationLinks links = new NavigationLinks();

    final XMLEventReader reader = getEventReader(is);

    try {
      final List<Map.Entry<String, String>> filter = new ArrayList<Map.Entry<String, String>>();
      filter.add(
          new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=entry"));
      filter.add(
          new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=feed"));

      int startDepth = 0;

      while (true) {
        // a. search for link with type attribute equals to "application/atom+xml;type=entry/feed"
        final Map.Entry<Integer, XMLElement> linkInfo =
            extractElement(
                reader,
                null,
                Collections.<String>singletonList(Constants.get(ConstantKey.LINK)),
                filter,
                true,
                startDepth,
                2,
                2);
        final XMLElement link = linkInfo.getValue();
        startDepth = linkInfo.getKey();

        final String title = link.getStart().getAttributeByName(new QName("title")).getValue();

        final Attribute hrefAttr = link.getStart().getAttributeByName(new QName("href"));
        final String href = hrefAttr == null ? null : hrefAttr.getValue();

        try {
          final XMLElement inlineElement =
              extractElement(
                      link.getContentReader(),
                      null,
                      Collections.<String>singletonList(Constants.get(ConstantKey.INLINE)),
                      0,
                      -1,
                      -1)
                  .getValue();
          final XMLEventReader inlineReader = inlineElement.getContentReader();

          try {
            while (true) {
              final XMLElement entry =
                  extractElement(
                          inlineReader, null, Collections.<String>singletonList("entry"), 0, -1, -1)
                      .getValue();
              links.addInlines(title, entry.toStream());
            }
          } catch (Exception e) {
            // Reached the end of document
          }

          inlineReader.close();
        } catch (Exception ignore) {
          // inline element not found (inlines are not mondatory).
          if (StringUtils.isNotBlank(href) && ENTITY_URI_PATTERN.matcher(href).matches()) {
            links.addLinks(title, href.substring(href.lastIndexOf('/') + 1));
          }
        }
      }
    } catch (Exception ignore) {
      // ignore
    } finally {
      reader.close();
    }

    return links;
  }