Exemplo n.º 1
0
  @Override
  public void endPrefixMapping(String prefix) throws SAXException {
    this.prefixMappings.remove(prefix);

    // Send the event to the right extra handler.
    super.endPrefixMapping(prefix);
  }
Exemplo n.º 2
0
  @Override
  public void endDocument() throws SAXException {
    this.state = State.NONE;
    this.currentEntry = null;
    this.contentBuffer = null;

    super.endDocument();
  }
Exemplo n.º 3
0
 @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
   if (this.contentDepth >= 0) {
     // The content might embed XML elements from various namespaces
     if (this.currentContentWriter != null) {
       this.currentContentWriter.characters(ch, start, length);
     }
   } else {
     this.contentBuffer.append(ch, start, length);
   }
   super.characters(ch, start, length);
 }
Exemplo n.º 4
0
  @Override
  public void startPrefixMapping(String prefix, String uri) throws SAXException {
    this.prefixMappings.put(prefix, uri);

    super.startPrefixMapping(prefix, uri);
  }
Exemplo n.º 5
0
  @Override
  public void startElement(String uri, String localName, String qName, Attributes attrs)
      throws SAXException {
    this.contentBuffer.delete(0, this.contentBuffer.length() + 1);

    if (this.contentDepth >= 0) {
      // The content might embed XML elements from various namespaces
      if (this.currentContentWriter != null) {
        this.currentContentWriter.startElement(uri, localName, qName, attrs);
      }
      this.contentDepth++;
    } else if (uri.equalsIgnoreCase(Feed.ATOM_NAMESPACE)) {
      if (localName.equals("feed")) {
        String attr = attrs.getValue("xml:base");
        if (attr != null) {
          this.currentFeed.setBaseReference(new Reference(attr));
        }
        this.state = State.FEED;
        startFeed(this.currentFeed);
      } else if (localName.equals("title")) {
        startTextElement(attrs);

        if (this.state == State.FEED) {
          this.state = State.FEED_TITLE;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_TITLE;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_TITLE;
        }
      } else if (localName.equals("summary")) {
        startTextElement(attrs);

        if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_SUMMARY;
        }
      } else if (localName.equals("updated")) {
        this.currentDate = new Date();

        if (this.state == State.FEED) {
          this.state = State.FEED_UPDATED;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_UPDATED;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_UPDATED;
        }
      } else if (localName.equals("published")) {
        this.currentDate = new Date();

        if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_PUBLISHED;
        }
      } else if (localName.equals("author")) {
        this.currentPerson = new Person();

        if (this.state == State.FEED) {
          this.state = State.FEED_AUTHOR;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_AUTHOR;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_AUTHOR;
        }
      } else if (localName.equals("name")) {
        if (this.state == State.FEED_AUTHOR) {
          this.state = State.FEED_AUTHOR_NAME;
        } else if (this.state == State.FEED_ENTRY_AUTHOR) {
          this.state = State.FEED_ENTRY_AUTHOR_NAME;
        } else if (this.state == State.FEED_ENTRY_SOURCE_AUTHOR) {
          this.state = State.FEED_ENTRY_SOURCE_AUTHOR_NAME;
        }
      } else if (localName.equals("id")) {
        if (this.state == State.FEED) {
          this.state = State.FEED_ID;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_ID;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_ID;
        }
      } else if (localName.equals("link")) {
        this.currentLink = new Link();
        this.currentLink.setHref(new Reference(attrs.getValue("", "href")));
        this.currentLink.setRel(Relation.valueOf(attrs.getValue("", "rel")));
        String type = attrs.getValue("", "type");
        if (type != null && type.length() > 0) {
          this.currentLink.setType(new MediaType(type));
        }

        this.currentLink.setHrefLang(new Language(attrs.getValue("", "hreflang")));
        this.currentLink.setTitle(attrs.getValue("", "title"));
        final String attr = attrs.getValue("", "length");
        this.currentLink.setLength((attr == null) ? -1L : Long.parseLong(attr));

        if (this.state == State.FEED) {
          this.state = State.FEED_LINK;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_LINK;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_LINK;
        }
        // Glean the content
        this.currentContent = new Content();
        // Content available inline
        initiateInlineMixedContent();
        this.currentLink.setContent(currentContent);
        startLink(this.currentLink);
      } else if (localName.equalsIgnoreCase("entry")) {
        if (this.state == State.FEED) {
          this.currentEntry = new Entry();
          this.state = State.FEED_ENTRY;
        }
        startEntry(this.currentEntry);
      } else if (localName.equals("category")) {
        this.currentCategory = new Category();
        this.currentCategory.setTerm(attrs.getValue("", "term"));
        this.currentCategory.setScheme(new Reference(attrs.getValue("", "scheme")));
        this.currentCategory.setLabel(attrs.getValue("", "label"));

        if (this.state == State.FEED) {
          this.state = State.FEED_CATEGORY;
        } else if (this.state == State.FEED_ENTRY) {
          this.state = State.FEED_ENTRY_CATEGORY;
        } else if (this.state == State.FEED_ENTRY_SOURCE) {
          this.state = State.FEED_ENTRY_SOURCE_CATEGORY;
        }
      } else if (localName.equalsIgnoreCase("content")) {
        if (this.state == State.FEED_ENTRY) {
          contentType = getMediaType(attrs.getValue("", "type"));
          String srcAttr = attrs.getValue("", "src");
          this.currentContent = new Content();

          if (srcAttr == null) {
            // Content available inline
            initiateInlineMixedContent();
          } else {
            // Content available externally
            this.currentContent.setExternalRef(new Reference(srcAttr));
            this.currentContent.setExternalType(contentType);
          }

          this.currentEntry.setContent(currentContent);
          this.state = State.FEED_ENTRY_CONTENT;
        }
        startContent(this.currentContent);
      }
    }

    super.startElement(uri, localName, qName, attrs);
  }
Exemplo n.º 6
0
 @Override
 public void startDocument() throws SAXException {
   this.contentBuffer = new StringBuilder();
   super.startDocument();
 }
Exemplo n.º 7
0
  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    if (this.currentText != null) {
      this.currentText.setContent(this.contentBuffer.toString());
    }

    if (this.currentDate != null) {
      final String formattedDate = this.contentBuffer.toString();
      final Date parsedDate = DateUtils.parse(formattedDate.trim(), DateUtils.FORMAT_RFC_3339);

      if (parsedDate != null) {
        this.currentDate.setTime(parsedDate.getTime());
      } else {
        this.currentDate = null;
      }
    }

    if (contentDepth > 0) {
      // The content might embed XML elements from various namespaces
      if (this.currentContentWriter != null) {
        this.currentContentWriter.endElement(uri, localName, qName);
      }

      contentDepth--;
    } else if (uri.equalsIgnoreCase(Feed.ATOM_NAMESPACE)) {
      if (localName.equals("feed")) {
        this.state = State.NONE;
        endFeed(this.currentFeed);
      } else if (localName.equals("title")) {
        if (this.state == State.FEED_TITLE) {
          this.currentFeed.setTitle(this.currentText);
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_TITLE) {
          this.currentEntry.setTitle(this.currentText);
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_TITLE) {
          this.currentEntry.getSource().setTitle(this.currentText);
          this.state = State.FEED_ENTRY_SOURCE;
        }
      } else if (localName.equals("summary")) {
        if (this.state == State.FEED_ENTRY_SUMMARY) {
          if (this.currentText != null) {
            this.currentEntry.setSummary(this.currentText.getContent());
          }

          this.state = State.FEED_ENTRY;
        }
      } else if (localName.equals("updated")) {
        if (this.state == State.FEED_UPDATED) {
          this.currentFeed.setUpdated(this.currentDate);
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_UPDATED) {
          this.currentEntry.setUpdated(this.currentDate);
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_UPDATED) {
          this.currentEntry.getSource().setUpdated(this.currentDate);
          this.state = State.FEED_ENTRY_SOURCE;
        }
      } else if (localName.equals("published")) {
        if (this.state == State.FEED_ENTRY_PUBLISHED) {
          this.currentEntry.setPublished(this.currentDate);
          this.state = State.FEED_ENTRY;
        }
      } else if (localName.equals("author")) {
        if (this.state == State.FEED_AUTHOR) {
          this.currentFeed.getAuthors().add(this.currentPerson);
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_AUTHOR) {
          this.currentEntry.getAuthors().add(this.currentPerson);
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_AUTHOR) {
          this.currentEntry.getSource().getAuthors().add(this.currentPerson);
          this.state = State.FEED_ENTRY_SOURCE;
        }
      } else if (localName.equals("name")) {
        this.currentPerson.setName(this.contentBuffer.toString());

        if (this.state == State.FEED_AUTHOR_NAME) {
          this.state = State.FEED_AUTHOR;
        } else if (this.state == State.FEED_ENTRY_AUTHOR_NAME) {
          this.state = State.FEED_ENTRY_AUTHOR;
        } else if (this.state == State.FEED_ENTRY_SOURCE_AUTHOR_NAME) {
          this.state = State.FEED_ENTRY_SOURCE_AUTHOR;
        }
      } else if (localName.equals("id")) {
        if (this.state == State.FEED_ID) {
          this.currentFeed.setId(this.contentBuffer.toString());
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_ID) {
          this.currentEntry.setId(this.contentBuffer.toString());
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_ID) {
          this.currentEntry.getSource().setId(this.contentBuffer.toString());
          this.state = State.FEED_ENTRY_SOURCE;
        }
      } else if (localName.equals("link")) {
        if (this.state == State.FEED_LINK) {
          this.currentFeed.getLinks().add(this.currentLink);
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_LINK) {
          this.currentEntry.getLinks().add(this.currentLink);
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_LINK) {
          this.currentEntry.getSource().getLinks().add(this.currentLink);
          this.state = State.FEED_ENTRY_SOURCE;
        }

        // Set the inline content, if any
        if (this.currentContentWriter != null) {
          String content = this.currentContentWriter.getWriter().toString().trim();
          contentDepth = -1;
          if ("".equals(content)) {
            this.currentLink.setContent(null);
          } else {
            if (this.currentLink.getType() != null) {
              currentContent.setInlineContent(
                  new StringRepresentation(content, this.currentLink.getType()));
            } else {
              currentContent.setInlineContent(new StringRepresentation(content));
            }
          }
          this.currentContentWriter = null;
        }

        endLink(this.currentLink);
      } else if (localName.equalsIgnoreCase("entry")) {
        if (this.state == State.FEED_ENTRY) {
          this.currentFeed.getEntries().add(this.currentEntry);
          this.state = State.FEED;
        }

        endEntry(this.currentEntry);
      } else if (localName.equals("category")) {
        if (this.state == State.FEED_CATEGORY) {
          this.currentFeed.getCategories().add(this.currentCategory);
          this.state = State.FEED;
        } else if (this.state == State.FEED_ENTRY_CATEGORY) {
          this.currentEntry.getCategories().add(this.currentCategory);
          this.state = State.FEED_ENTRY;
        } else if (this.state == State.FEED_ENTRY_SOURCE_CATEGORY) {
          this.currentEntry.getSource().getCategories().add(this.currentCategory);
          this.state = State.FEED_ENTRY_SOURCE;
        }
      } else if (localName.equalsIgnoreCase("content")) {
        if (this.state == State.FEED_ENTRY_CONTENT) {
          if (!this.currentEntry.getContent().isExternal()) {
            String content = this.currentContentWriter.getWriter().toString().trim();
            contentDepth = -1;
            if ("".equals(content)) {
              this.currentEntry.setContent(null);
            } else {
              currentContent.setInlineContent(new StringRepresentation(content, contentType));
            }
          }

          this.state = State.FEED_ENTRY;
        }
        this.currentContentWriter = null;
        endContent(this.currentContent);
      }
    }

    this.currentText = null;
    this.currentDate = null;
    super.endElement(uri, localName, qName);
  }