本文整理汇总了Java中org.jsoup.parser.Tag类的典型用法代码示例。如果您正苦于以下问题:Java Tag类的具体用法?Java Tag怎么用?Java Tag使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Tag类属于org.jsoup.parser包,在下文中一共展示了Tag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replacePlaceholderWithNode
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test
public void replacePlaceholderWithNode() {
Map<String, Node> nodeIdMap = new HashMap<>();
String html = "<div>";
for (int i = 0; i < 5; i++) {
Attributes attrs = new Attributes();
String id = "id" + i;
attrs.put("id", id);
Element ele = new Element(Tag.valueOf("span"), "", attrs);
ele.append("The original node");
nodeIdMap.put(id, ele);
Element placeholder = ArticleUtil.generatePlaceholderNode(id);
html += placeholder.outerHtml();
}
html += "</div>";
String results = ArticleUtil.replacePlaceholderWithNode(nodeIdMap, html);
for (Node originalNode: nodeIdMap.values()) {
assertThat(results).contains(originalNode.outerHtml());
}
}
示例2: createSafeElement
import org.jsoup.parser.Tag; //导入依赖的package包/类
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);
}
示例3: handlesBaseUri
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test public void handlesBaseUri() {
Tag tag = Tag.valueOf("a");
Attributes attribs = new Attributes();
attribs.put("relHref", "/foo");
attribs.put("absHref", "http://bar/qux");
Element noBase = new Element(tag, "", attribs);
assertEquals("", noBase.absUrl("relHref")); // with no base, should NOT fallback to href attrib, whatever it is
assertEquals("http://bar/qux", noBase.absUrl("absHref")); // no base but valid attrib, return attrib
Element withBase = new Element(tag, "http://foo/", attribs);
assertEquals("http://foo/foo", withBase.absUrl("relHref")); // construct abs from base + rel
assertEquals("http://bar/qux", withBase.absUrl("absHref")); // href is abs, so returns that
assertEquals("", withBase.absUrl("noval"));
Element dodgyBase = new Element(tag, "wtf://no-such-protocol/", attribs);
assertEquals("http://bar/qux", dodgyBase.absUrl("absHref")); // base fails, but href good, so get that
assertEquals("", dodgyBase.absUrl("relHref")); // base fails, only rel href, so return nothing
}
示例4: testMain
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test
public void testMain() throws UIMAException {
JCas jCas = JCasSingleton.getJCasInstance();
SemanticHtml sa = new SemanticHtml();
Map<String, Class<?>> expectedMain = new HashMap<>();
expectedMain.put("time", Temporal.class);
expectedMain.put("meter", Quantity.class);
expectedMain.put("dfn", Buzzword.class);
expectedMain.put("address", Location.class);
expectedMain.put("abbr", Buzzword.class);
expectedMain.put("cite", DocumentReference.class);
for (Map.Entry<String, Class<?>> e : expectedMain.entrySet()) {
Element element = new Element(Tag.valueOf(e.getKey()), "");
AnnotationCollector collector = new AnnotationCollector();
sa.map(jCas, element, collector);
if (e.getValue() != null) {
assertTrue(e.getValue().isInstance(collector.getAnnotations().get(0)));
} else {
assertNull(collector.getAnnotations());
}
}
}
示例5: testHeadings
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test
public void testHeadings() throws UIMAException {
final JCas jCas = JCasSingleton.getJCasInstance();
final StructuralAnnotations sa = new StructuralAnnotations();
final Element h1 = new Element(Tag.valueOf("h1"), "");
final Element h2 = new Element(Tag.valueOf("h2"), "");
final Element h3 = new Element(Tag.valueOf("h3"), "");
final Element h4 = new Element(Tag.valueOf("h4"), "");
final AnnotationCollector collector = new AnnotationCollector();
sa.map(jCas, h1, collector);
sa.map(jCas, h2, collector);
sa.map(jCas, h3, collector);
sa.map(jCas, h4, collector);
Heading heading1 = (Heading) collector.getAnnotations().get(0);
Heading heading2 = (Heading) collector.getAnnotations().get(1);
Heading heading3 = (Heading) collector.getAnnotations().get(2);
Heading heading4 = (Heading) collector.getAnnotations().get(3);
assertEquals(1, heading1.getLevel());
assertEquals(2, heading2.getLevel());
assertEquals(3, heading3.getLevel());
assertEquals(4, heading4.getLevel());
}
示例6: testLink
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test
public void testLink() throws UIMAException {
final JCas jCas = JCasSingleton.getJCasInstance();
final StructuralAnnotations sa = new StructuralAnnotations();
final Element a1 = new Element(Tag.valueOf("a"), "");
a1.attr("href", "http://example.com");
final Element a2 = new Element(Tag.valueOf("a"), "");
a2.attr("href", "/example.com");
final AnnotationCollector collector = new AnnotationCollector();
sa.map(jCas, a1, collector);
sa.map(jCas, a2, collector);
Annotation link = collector.getAnnotations().get(0);
assertTrue(link instanceof Link);
assertEquals("http://example.com", ((Link) link).getTarget());
Annotation link2 = collector.getAnnotations().get(1);
assertTrue(link2 instanceof Link);
assertEquals("/example.com", ((Link) link2).getTarget());
}
示例7: testMain
import org.jsoup.parser.Tag; //导入依赖的package包/类
@Test
public void testMain() throws UIMAException {
final JCas jCas = JCasSingleton.getJCasInstance();
final StructuralAnnotations sa = new StructuralAnnotations();
final Map<String, Class<?>> expectedMain = new HashMap<>();
expectedMain.put("Document", Document.class);
expectedMain.put("SlideShow", SlideShow.class);
expectedMain.put("SpreadSheet", SpreadSheet.class);
expectedMain.put("Another", Document.class);
for (final Map.Entry<String, Class<?>> e : expectedMain.entrySet()) {
final Element anchor = new Element(Tag.valueOf("main"), "");
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());
}
}
}
示例8: testArticle
import org.jsoup.parser.Tag; //导入依赖的package包/类
@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());
}
}
}
示例9: testNameContent
import org.jsoup.parser.Tag; //导入依赖的package包/类
@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());
}
示例10: head
import org.jsoup.parser.Tag; //导入依赖的package包/类
public void head(Node source, int depth) {
if (skipChildren) {
return;
}
if (source instanceof Element) {
Element sourceElement = (Element) source;
if (isSafeTag(sourceElement)) {
String sourceTag = sourceElement.tagName();
Attributes destinationAttributes = sourceElement.attributes().clone();
Element destinationChild = new Element(Tag.valueOf(sourceTag), sourceElement.baseUri(), destinationAttributes);
destination.appendChild(destinationChild);
destination = destinationChild;
} else if (source != root) {
skipChildren = true;
}
} else if (source instanceof TextNode) {
TextNode sourceText = (TextNode) source;
TextNode destinationText = new TextNode(sourceText.getWholeText(), source.baseUri());
destination.appendChild(destinationText);
} else if (source instanceof DataNode && isSafeTag(source.parent())) {
DataNode sourceData = (DataNode) source;
DataNode destinationData = new DataNode(sourceData.getWholeData(), source.baseUri());
destination.appendChild(destinationData);
}
}
示例11: addHiddenInputTag
import org.jsoup.parser.Tag; //导入依赖的package包/类
private void addHiddenInputTag(Element form, String formIdAttrName, String formIdAttrValue) {
Attributes attributes = Stream.of(
new Attribute("type", "hidden"),
new Attribute("name", formIdAttrName),
new Attribute("value", formIdAttrValue))
.collect(Attributes::new, Attributes::put, Attributes::addAll);
form.prependChild(new Element(Tag.valueOf("input"), "/", attributes));
}
示例12: getImageActionElement
import org.jsoup.parser.Tag; //导入依赖的package包/类
/**
* Returns an {@link org.jsoup.nodes.Element} consisting of an {@code <a>} tag wrapped around
* the {@code <img>} tag, both of which share the {@link ImageAction} link representation provided.
*/
public static Element getImageActionElement(String imageActionStringRepresentation) {
return new Element(Tag.valueOf("a"), "")
.attr("href", imageActionStringRepresentation)
.appendChild(
new Element(Tag.valueOf("img"), "")
.attr("src", imageActionStringRepresentation));
}
示例13: getResourceWithExternalGif
import org.jsoup.parser.Tag; //导入依赖的package包/类
private String getResourceWithExternalGif() {
String source = loader.readTestResourceFile("SampleImageCommentAJAXCallResponse.html");
Document doc = Jsoup.parse(source);
Element postBody = doc.select(".communication__body__text").first();
postBody.select("div.image-show-container").first().remove();
postBody.appendChild(new Element(Tag.valueOf("img"), "").attr("src", expectedImageSource + ".gif"));
return doc.outerHtml();
}
示例14: createSafeElement
import org.jsoup.parser.Tag; //导入依赖的package包/类
/**
* 按原Element重建一个新的Element
* @param sourceEl
* @return
*/
private static Element createSafeElement(Element sourceEl) {
String sourceTag = sourceEl.tagName();
Attributes destAttrs = new Attributes();
Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
Attributes sourceAttrs = sourceEl.attributes();
for (Attribute sourceAttr : sourceAttrs) {
destAttrs.put(sourceAttr);
}
return dest;
}
示例15: findHeadingAncestor
import org.jsoup.parser.Tag; //导入依赖的package包/类
/**
* return the Tag of the first heading (h1, h2..) ancestor otherwise return
* null if no ancestor is heading
*/
public static Tag findHeadingAncestor(Node node) {
Node ancestor = node;
while (ancestor != null) {
Tag t = getHeadingTag(ancestor);
if (t != null) {
return t;
}
ancestor = ancestor.parent();
}
return null;
}