Java Code Examples for org.jsoup.parser.Tag#valueOf()

The following examples show how to use org.jsoup.parser.Tag#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private ElementMeta createSafeElement(Element sourceEl) {
    String sourceTag = sourceEl.tagName();
    Attributes destAttrs = new Attributes();
    Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
    int numDiscarded = 0;

    Attributes sourceAttrs = sourceEl.attributes();
    for (Attribute sourceAttr : sourceAttrs) {
        if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr))
            destAttrs.put(sourceAttr);
        else
            numDiscarded++;
    }
    Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag);
    destAttrs.addAll(enforcedAttrs);

    return new ElementMeta(dest, numDiscarded);
}
 
Example 2
Source File: CifnewsPageHandler.java    From cetty with Apache License 2.0 6 votes vote down vote up
@Override
public Element appendBody(Elements tempBody) {
    final Element articleBody = new Element(Tag.valueOf("div"), "");
    String blockquote = tempBody.select("div.fetch-read>div.summary").text();
    buildBlockquote(blockquote, articleBody);
    Elements inner = tempBody.select("div.article-inner>*");
    for (Element pEl : inner) {
        if (pEl.select("div.fetch-present").size() != 0) {
            continue;
        }
        Element imgEl = pEl.select("p>img").first();
        if (imgEl != null) {
            Element figure = buildFigure(imgEl);
            if (imgEl.nextElementSibling() != null && imgEl.nextElementSibling().tagName().equals("p")) {
                Element figcaption = buildFigcaption(imgEl.nextElementSibling().text());
                figure.appendChild(figcaption);
                articleBody.appendChild(figure);
                continue;
            }
            articleBody.appendChild(figure);
            continue;
        }
        articleBody.appendChild(pEl);
    }
    return articleBody;
}
 
Example 3
Source File: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private ElementMeta createSafeElement(Element sourceEl) {
    String sourceTag = sourceEl.tagName();
    Attributes destAttrs = new Attributes();
    Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
    int numDiscarded = 0;

    Attributes sourceAttrs = sourceEl.attributes();
    for (Attribute sourceAttr : sourceAttrs) {
        if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr))
            destAttrs.put(sourceAttr);
        else
            numDiscarded++;
    }
    Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag);
    destAttrs.addAll(enforcedAttrs);

    return new ElementMeta(dest, numDiscarded);
}
 
Example 4
Source File: Waimaob2cPageHandler.java    From cetty with Apache License 2.0 6 votes vote down vote up
@Override
public Element appendBody(Elements tempBody) {
    final Element articleBody = new Element(Tag.valueOf("div"), "");
    for (Element body : tempBody) {
        if (body.tagName().equals("p")) {
            boolean skipRegister = body.select("p").text().contains("即刻注册SHOPIFY账户, 跟着我们精心准备的SHOPIFY教程开始外贸独立站之旅!");
            boolean skipCopyRight = body.classNames().contains("post-copyright");
            if (skipRegister || skipCopyRight) {
                continue;
            }
        }
        Element imgEl = body.select("img").first();
        if (imgEl != null) {
            articleBody.appendChild(buildFigure(imgEl));
            continue;
        }
        articleBody.appendChild(body);
    }
    return articleBody;
}
 
Example 5
Source File: StructuralAnnotationsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testArticle() throws UIMAException {
  final JCas jCas = JCasSingleton.getJCasInstance();
  final StructuralAnnotations sa = new StructuralAnnotations();

  final Map<String, Class<?>> expectedArticle = new HashMap<>();
  expectedArticle.put("Sheet", Sheet.class);
  expectedArticle.put("Slide", Slide.class);
  expectedArticle.put("Page", Page.class);
  expectedArticle.put("Another", Page.class);

  for (final Map.Entry<String, Class<?>> e : expectedArticle.entrySet()) {
    final Element anchor = new Element(Tag.valueOf("article"), "");
    anchor.attr("class", e.getKey());

    final AnnotationCollector collector = new AnnotationCollector();
    sa.map(jCas, anchor, collector);

    if (e.getValue() != null) {
      assertTrue(e.getValue().isInstance(collector.getAnnotations().get(0)));
    } else {
      assertNull(collector.getAnnotations());
    }
  }
}
 
Example 6
Source File: ElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testHashcodeIsStableWithContentChanges() {
    Element root = new Element(Tag.valueOf("root"), "");

    HashSet<Element> set = new HashSet<Element>();
    // Add root node:
    set.add(root);

    root.appendChild(new Element(Tag.valueOf("a"), ""));
    assertTrue(set.contains(root));
}
 
Example 7
Source File: ElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test 
   public void whiteSpaceClassElement(){
    Tag tag = Tag.valueOf("a");
    Attributes attribs = new Attributes();
    Element el = new Element(tag, "", attribs);
    
    attribs.put("class", "abc ");
    boolean hasClass = el.hasClass("ab");
    assertFalse(hasClass);
        
}
 
Example 8
Source File: NodeTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void orphanNodeReturnsNullForSiblingElements() {
    Node node = new Element(Tag.valueOf("p"), "");
    Element el = new Element(Tag.valueOf("p"), "");

    assertEquals(0, node.siblingIndex());
    assertEquals(0, node.siblingNodes().size());

    assertNull(node.previousSibling());
    assertNull(node.nextSibling());

    assertEquals(0, el.siblingElements().size());
    assertNull(el.previousElementSibling());
    assertNull(el.nextElementSibling());
}
 
Example 9
Source File: StructuralAnnotationsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testFigure() throws UIMAException {
  final JCas jCas = JCasSingleton.getJCasInstance();
  final StructuralAnnotations sa = new StructuralAnnotations();

  final Element anchor = new Element(Tag.valueOf("img"), "");
  anchor.attr("src", "test");

  final AnnotationCollector collector = new AnnotationCollector();
  sa.map(jCas, anchor, collector);

  Annotation fig = collector.getAnnotations().get(0);
  assertTrue(fig instanceof Figure);
  assertEquals("test", ((Figure) fig).getTarget());
}
 
Example 10
Source File: MetaTagTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testNameContent() throws UIMAException {
  JCas jCas = JCasSingleton.getJCasInstance();
  MetaTags mt = new MetaTags();

  Element element = new Element(Tag.valueOf("meta"), "");
  element.attr("name", "key");
  element.attr("content", "value");

  AnnotationCollector collector = new AnnotationCollector();
  mt.map(jCas, element, collector);
  Metadata annotation = (Metadata) collector.getAnnotations().get(0);
  assertEquals("key", annotation.getKey());
  assertEquals("value", annotation.getValue());
}
 
Example 11
Source File: NodeTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void after() {
    Document doc = Jsoup.parse("<p>One <b>two</b> three</p>");
    Element newNode = new Element(Tag.valueOf("em"), "");
    newNode.appendText("four");

    doc.select("b").first().after(newNode);
    assertEquals("<p>One <b>two</b><em>four</em> three</p>", doc.body().html());

    doc.select("b").first().after("<i>five</i>");
    assertEquals("<p>One <b>two</b><i>five</i><em>four</em> three</p>", doc.body().html());
}
 
Example 12
Source File: TextNotIdenticalToAttributeCheckerTest.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    mockTextElementBuilder = createMock(TextElementBuilder.class);
    mockSSPHandler = createMock(SSPHandler.class);
    elements = new Elements();
    element = new Element(Tag.valueOf("div"), "");
    element.attr(AttributeStore.ALT_ATTR, "test");
    mockTestSolutionHandler = createMock(TestSolutionHandler.class);
    mockProcessRemarkService = createMock(ProcessRemarkService.class);
}
 
Example 13
Source File: StructuralAnnotationsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnchor() throws UIMAException {
  final JCas jCas = JCasSingleton.getJCasInstance();
  final StructuralAnnotations sa = new StructuralAnnotations();

  final Element anchor = new Element(Tag.valueOf("a"), "");

  final AnnotationCollector collector = new AnnotationCollector();
  sa.map(jCas, anchor, collector);

  assertTrue(collector.getAnnotations().get(0) instanceof Anchor);
}
 
Example 14
Source File: NodeTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void after() {
    Document doc = Jsoup.parse("<p>One <b>two</b> three</p>");
    Element newNode = new Element(Tag.valueOf("em"), "");
    newNode.appendText("four");

    doc.select("b").first().after(newNode);
    assertEquals("<p>One <b>two</b><em>four</em> three</p>", doc.body().html());

    doc.select("b").first().after("<i>five</i>");
    assertEquals("<p>One <b>two</b><i>five</i><em>four</em> three</p>", doc.body().html());
}
 
Example 15
Source File: ElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testHashcodeIsStableWithContentChanges() {
    Element root = new Element(Tag.valueOf("root"), "");

    HashSet<Element> set = new HashSet<Element>();
    // Add root node:
    set.add(root);

    root.appendChild(new Element(Tag.valueOf("a"), ""));
    assertTrue(set.contains(root));
}
 
Example 16
Source File: NodeTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void orphanNodeReturnsNullForSiblingElements() {
    Node node = new Element(Tag.valueOf("p"), "");
    Element el = new Element(Tag.valueOf("p"), "");

    assertEquals(0, node.siblingIndex());
    assertEquals(0, node.siblingNodes().size());

    assertNull(node.previousSibling());
    assertNull(node.nextSibling());

    assertEquals(0, el.siblingElements().size());
    assertNull(el.previousElementSibling());
    assertNull(el.nextElementSibling());
}
 
Example 17
Source File: ElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testHashcodeIsStableWithContentChanges() {
    Element root = new Element(Tag.valueOf("root"), "");

    HashSet<Element> set = new HashSet<Element>();
    // Add root node:
    set.add(root);

    root.appendChild(new Element(Tag.valueOf("a"), ""));
    assertTrue(set.contains(root));
}
 
Example 18
Source File: Element.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new, standalone element.
 * @param tag tag name
 */
public Element(String tag) {
    this(Tag.valueOf(tag), "", new Attributes());
}
 
Example 19
Source File: Document.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 Create a new Element, with this document's base uri. Does not make the new element a child of this document.
 @param tagName element tag name (e.g. {@code a})
 @return new element
 */
public Element createElement(String tagName) {
    return new Element(Tag.valueOf(tagName, ParseSettings.preserveCase), this.baseUri());
}
 
Example 20
Source File: Element.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new element by tag name, and add it as the last child.
 * 
 * @param tagName the name of the tag (e.g. {@code div}).
 * @return the new element, to allow you to add content to it, e.g.:
 *  {@code parent.appendElement("h1").attr("id", "header").text("Welcome");}
 */
public Element appendElement(String tagName) {
    Element child = new Element(Tag.valueOf(tagName), baseUri());
    appendChild(child);
    return child;
}