本文整理汇总了Java中org.jsoup.nodes.Attribute类的典型用法代码示例。如果您正苦于以下问题:Java Attribute类的具体用法?Java Attribute怎么用?Java Attribute使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Attribute类属于org.jsoup.nodes包,在下文中一共展示了Attribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isIllegalStringInTag
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
private boolean isIllegalStringInTag(final Element tag){
final String[] illegalWords = {"advert", "werbung", "anzeige", "adsense"};
if (tag == null){
return false;
}
for (final String word : illegalWords) {
final Attributes tagAttrs = tag.attributes();
if (tagAttrs != null){
for(final Attribute attr : tagAttrs){
if(attr.toString().toLowerCase().contains(word.toLowerCase())){
return true;
}
}
}else{
return false;
}
}
return false;
}
示例2: setQuestion
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
public void setQuestion(ExamSectionQuestion esq) {
Document doc = Jsoup.parse(esq.getQuestion().getQuestion());
Elements blanks = doc.select(CLOZE_SELECTOR);
blanks.forEach(b -> {
boolean isNumeric = isNumeric(b);
Iterator<Attribute> it = b.attributes().iterator();
while (it.hasNext()) {
Attribute a = it.next();
if (!a.getKey().equals("id")) {
it.remove();
}
}
b.tagName("input");
b.text("");
b.attr("type", isNumeric ? "number" : "text");
b.attr("class", "cloze-input");
if (isNumeric) {
b.attr("step", "any");
// Should allow for using both comma and period as decimal separator
b.attr( "lang", "en-150");
}
});
this.question = doc.body().children().toString();
}
示例3: getExpressionReturnTypeForAttribute
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Guess the type of the expression based on where it is used.
* The guessed type can be overridden by adding a Cast to the desired type at the
* beginning of the expression.
* @param attribute The attribute the expression is in
* @return
*/
private String getExpressionReturnTypeForAttribute(Attribute attribute)
{
String attributeName = attribute.getKey().toLowerCase();
if (attributeName.indexOf("@") == 0 || attributeName.indexOf("v-on:") == 0)
return "void";
if ("v-if".equals(attributeName) || "v-show".equals(attributeName))
return "boolean";
if (currentProp != null)
return currentProp.getType().toString();
return Any.class.getCanonicalName();
}
示例4: isSafeAttribute
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Test if the supplied attribute is allowed by this whitelist for this tag
* @param tagName tag to consider allowing the attribute in
* @param el element under test, to confirm protocol
* @param attr attribute under test
* @return true if allowed
*/
protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
TagName tag = TagName.valueOf(tagName);
AttributeKey key = AttributeKey.valueOf(attr.getKey());
if (attributes.containsKey(tag)) {
if (attributes.get(tag).contains(key)) {
if (protocols.containsKey(tag)) {
Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
// ok if not defined protocol; otherwise test
return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
} else { // attribute found, no protocols defined, so OK
return true;
}
}
}
// no attributes defined for tag, try :all tag
return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}
示例5: updateNamespaces
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Finds any namespaces defined in this element. Returns any tag prefix.
*/
private String updateNamespaces(org.jsoup.nodes.Element el) {
// scan the element for namespace declarations
// like: xmlns="blah" or xmlns:prefix="blah"
Attributes attributes = el.attributes();
for (Attribute attr : attributes) {
String key = attr.getKey();
String prefix;
if (key.equals(xmlnsKey)) {
prefix = "";
} else if (key.startsWith(xmlnsPrefix)) {
prefix = key.substring(xmlnsPrefix.length());
} else {
continue;
}
namespaces.put(prefix, attr.getValue());
}
// get the element prefix if any
int pos = el.tagName().indexOf(":");
return pos > 0 ? el.tagName().substring(0, pos) : "";
}
示例6: parsesBooleanAttributes
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
@Test public void parsesBooleanAttributes() {
String html = "<a normal=\"123\" boolean empty=\"\"></a>";
Element el = Jsoup.parse(html).select("a").first();
assertEquals("123", el.attr("normal"));
assertEquals("", el.attr("boolean"));
assertEquals("", el.attr("empty"));
List<Attribute> attributes = el.attributes().asList();
assertEquals("There should be 3 attribute present", 3, attributes.size());
// Assuming the list order always follows the parsed html
assertFalse("'normal' attribute should not be boolean", attributes.get(0) instanceof BooleanAttribute);
assertTrue("'boolean' attribute should be boolean", attributes.get(1) instanceof BooleanAttribute);
assertFalse("'empty' attribute should not be boolean", attributes.get(2) instanceof BooleanAttribute);
assertEquals(html, el.outerHtml());
}
示例7: renameAllAttributeKeys
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
private static void renameAllAttributeKeys(
ImmutableMap<String, String> renameMap, Element element) {
Attributes attributes = element.attributes();
for (Attribute attribute : attributes) {
String key = attribute.getKey();
// Polymer events are referenced as strings. As a result they do not participate in renaming.
// Additionally, it is not valid to have a Polymer property start with "on".
if (!key.startsWith("on-")) {
String renamedProperty = renameMap.get(
CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key));
if (renamedProperty != null) {
attribute.setKey(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, renamedProperty));
}
}
}
}
示例8: createSafeElement
import org.jsoup.nodes.Attribute; //导入依赖的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);
}
示例9: testValidProtocol
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
private boolean testValidProtocol(Element el, Attribute attr, Set<Protocol> protocols) {
// try to resolve relative urls to abs, and optionally update the attribute so output html has abs.
// rels without a baseuri get removed
String value = el.absUrl(attr.getKey());
if (value.length() == 0)
value = attr.getValue(); // if it could not be made abs, run as-is to allow custom unknown protocols
if (!preserveRelativeLinks)
attr.setValue(value);
for (Protocol protocol : protocols) {
String prot = protocol.toString() + ":";
if (value.toLowerCase().startsWith(prot)) {
return true;
}
}
return false;
}
示例10: searchDirectParentWithAttribute
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* this method gets the parent node of the node in param
* with attribute Class not null
* @param n
* @return
*/
public Node searchDirectParentWithAttribute(Node n){
if (n!=null) {
Attributes attributes =n.attributes();
List <Attribute> list_attributes= attributes.asList();
if (list_attributes.size()>0){
for (int i=0; i<list_attributes.size(); i++){
String attributeHtml =list_attributes.get(i).html();
if(attributeHtml.toLowerCase().contains("class=")) {
if(list_attributes.get(i).getValue().length()>0) {
return n;
}
}
}
return searchDirectParentWithAttribute( n.parent());
} else {
return searchDirectParentWithAttribute( n.parent());
}
} else {
return n;
}
}
示例11: isSafeAttribute
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
TagName tag = TagName.valueOf(tagName);
AttributeKey key = AttributeKey.valueOf(attr.getKey());
if (attributes.containsKey(tag)) {
if (attributes.get(tag).contains(key)) {
if (protocols.containsKey(tag)) {
Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
// ok if not defined protocol; otherwise test
return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
} else { // attribute found, no protocols defined, so OK
return true;
}
}
} else { // no attributes defined for tag, try :all tag
return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}
return false;
}
示例12: elementToHtml
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Produce predictable html (attributes in alphabetical order), always
* include close tags
*/
private String elementToHtml(Element producedElem, StringBuilder sb) {
ArrayList<String> names = new ArrayList<String>();
for (Attribute a : producedElem.attributes().asList()) {
names.add(a.getKey());
}
Collections.sort(names);
sb.append("<" + producedElem.tagName() + "");
for (String attrName : names) {
sb.append(" ").append(attrName).append("=").append("\'")
.append(producedElem.attr(attrName)).append("\'");
}
sb.append(">");
for (Node child : producedElem.childNodes()) {
if (child instanceof Element) {
elementToHtml((Element) child, sb);
} else if (child instanceof TextNode) {
String text = ((TextNode) child).text();
sb.append(text.trim());
}
}
sb.append("</").append(producedElem.tagName()).append(">");
return sb.toString();
}
示例13: isSafeAttribute
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Test if the supplied attribute is allowed by this whitelist for this tag
* @param tagName tag to consider allowing the attribute in
* @param el element under test, to confirm protocol
* @param attr attribute under test
* @return true if allowed
*/
boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
TagName tag = TagName.valueOf(tagName);
AttributeKey key = AttributeKey.valueOf(attr.getKey());
if (attributes.containsKey(tag)) {
if (attributes.get(tag).contains(key)) {
if (protocols.containsKey(tag)) {
Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
// ok if not defined protocol; otherwise test
return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
} else { // attribute found, no protocols defined, so OK
return true;
}
}
}
// no attributes defined for tag, try :all tag
return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}
示例14: removeMalformedAttributes
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
* Remove the comments of the page
*
* @param node
*/
private void removeMalformedAttributes(Node node) {
// as we are removing child nodes while iterating, we cannot use a normal foreach over children,
// or will get a concurrent list modification error.
int i = 0;
while (i < node.childNodes().size()) {
Node child = node.childNode(i);
for (Attribute attr : child.attributes()) {
if (attr.getKey().startsWith("\"") && attr.getKey().endsWith("\"")) {
child.removeAttr(attr.getKey());
}
}
removeMalformedAttributes(child);
i++;
}
}
示例15: parseAttributeToExtractCaptcha
import org.jsoup.nodes.Attribute; //导入依赖的package包/类
/**
*
* @param element
* @return wheter either one attribute of the current element, either its
* text, either one attribute of one of its parent or the text of one of
* its parents contains the "captcha" keyword
*/
private boolean parseAttributeToExtractCaptcha(Element element) {
if (element.nodeName().equalsIgnoreCase(HTML_ELEMENT) ||
element.nodeName().equalsIgnoreCase(BODY_ELEMENT)) {
return false;
}
if (StringUtils.containsIgnoreCase(element.ownText(), CAPTCHA_KEY)) {
return true;
} else {
for (Attribute attr : element.attributes()) {
if (StringUtils.containsIgnoreCase(attr.getValue(), CAPTCHA_KEY)) {
return true;
}
}
}
return false;
}