private String formatTag(Tag tag, int indent) { StringBuilder sb = new StringBuilder(); sb.append(createIndent(indent)); sb.append(indent); sb.append("<"); sb.append(formatAttribute(tag)); sb.append(">"); if (tag.getParent() != null) { sb.append(" (" + tag.getParent().getName().getLocalPart() + ")"); } if (text != null) { sb.append(text); } sb.append("\n"); if (tag.getTags().size() > 0) { indent++; for (Tag subTag : tag.getTags()) { sb.append(formatTag(subTag, indent)); } } return sb.toString(); }
public boolean equals(Object object) { if (object == null) { return false; } if (!(object instanceof Tag)) { return false; } if (object == this) { return true; } Tag tag = (Tag) object; if (!getName().equals(tag.getName())) { return false; } for (QName attr : getAttributes().keySet()) { if (getIgnoreAttr().contains(attr.getLocalPart()) || getIgnoreAttr().contains(getName().getLocalPart() + "@" + attr.getLocalPart())) { continue; } if (!tag.getAttributes().containsKey(attr)) { return false; } if (!tag.getAttributes().get(attr).equals(getAttributes().get(attr))) { return false; } } return true; } }
public int hashCode() { return getName().hashCode() + getAttributes().hashCode(); }
public static Tag getTagTree(final InputStream is, final List<String> ignoreAttr, Map<QName, Set<String>> types) throws Exception { Tag root = new Tag(); root.setName(new QName("root", "root")); newTag = new Tag(); newTag.setName(reader.getName()); if (!ignoreAttr.isEmpty()) { newTag.getIgnoreAttr().addAll(ignoreAttr); newTag.getAttributes().put(reader.getAttributeName(i), tp); newTag.setParent(currentTag); currentTag.getTags().add(newTag); currentTag = newTag; newTag.setText(reader.getText()); currentTag = currentTag.getParent();
protected void assertTagEquals(Tag expected, Tag source, final List<String> ignoreAttr, final List<String> ignoreTag) { if (!expected.getName().equals(source.getName())) { throw new ComparisonFailure("Tags not equal: ", expected.getName().toString(), source.getName().toString()); assertAttributesEquals(expected.getName(), expected.getAttributes(), source.getAttributes(), ignoreAttr); assertAttributesEquals(expected.getName(), source.getAttributes(), expected.getAttributes(), ignoreAttr); if (!StringUtils.isEmpty(expected.getText()) && !expected.getText().equals(source.getText())) { throw new ComparisonFailure("Text not equal: ", expected.getText(), source.getText()); if (!expected.getTags().isEmpty()) { for (Tag expectedTag : expected.getTags()) { if (ignoreTag.contains(expectedTag.getName().getLocalPart()) && expectedTag.getTags().isEmpty()) { continue; throw new AssertionError("\n" + expectedTag.toString() + " is missing in the source file:" + "\n" + source.toString());
if (!expectedTag.getName().equals(sourceTag.getName())) { throw new ComparisonFailure("Tags not equal: ", expectedTag.getName().toString(), sourceTag.getName().toString()); for (Map.Entry<QName, String> attr : expectedTag.getAttributes().entrySet()) { if (ignoreAttr.contains(attr.getKey().getLocalPart())) { continue; if (sourceTag.getAttributes().containsKey(attr.getKey())) { if (!sourceTag.getAttributes().get(attr.getKey()).equals(attr.getValue())) { throw new ComparisonFailure("Attributes not equal: ", attr.getKey() + ":" + attr.getValue(), attr.getKey() + ":" + sourceTag.getAttributes().get(attr.getKey())); if (!StringUtils.isEmpty(expectedTag.getText()) && !expectedTag.getText().equals(sourceTag.getText())) { throw new ComparisonFailure("Text not equal: ", expectedTag.getText(), sourceTag.getText());
newTag = new Tag(); newTag.setName(reader.getName()); newTag.getAttributes().put(reader.getAttributeName(i), reader.getAttributeValue(i)); newTag.setText(reader.getText());
NodeList nl = parser.parse(null); // you can also filter here NodeList divs = nl.extractAllNodesThatMatch( new AndFilter(new TagNameFilter("DIV"), new HasAttributeFilter("id", "OBJ123"))); if( divs.size() > 0 ) { Tag div = divs.elementAt(0); String text = div.getText(); // this is the text of the div }
public String toString() { StringBuilder sb = new StringBuilder(); sb.append(formatTag(this, 0)); return sb.toString(); }
public static Tag getTagTree(final InputStream is, final List<String> ignoreAttr, Map<QName, Set<String>> types) throws Exception { Tag root = new Tag(); root.setName(new QName("root", "root")); newTag = new Tag(); newTag.setName(reader.getName()); if (!ignoreAttr.isEmpty()) { newTag.getIgnoreAttr().addAll(ignoreAttr); newTag.getAttributes().put(reader.getAttributeName(i), tp); newTag.setParent(currentTag); currentTag.getTags().add(newTag); currentTag = newTag; newTag.setText(reader.getText()); currentTag = currentTag.getParent();
protected void assertTagEquals(Tag expected, Tag source, final List<String> ignoreAttr, final List<String> ignoreTag) { if (!expected.getName().equals(source.getName())) { throw new ComparisonFailure("Tags not equal: ", expected.getName().toString(), source.getName().toString()); assertAttributesEquals(expected.getName(), expected.getAttributes(), source.getAttributes(), ignoreAttr); assertAttributesEquals(expected.getName(), source.getAttributes(), expected.getAttributes(), ignoreAttr); if (!StringUtils.isEmpty(expected.getText()) && !expected.getText().equals(source.getText())) { throw new ComparisonFailure("Text not equal: ", expected.getText(), source.getText()); if (!expected.getTags().isEmpty()) { for (Tag expectedTag : expected.getTags()) { if (ignoreTag.contains(expectedTag.getName().getLocalPart()) && expectedTag.getTags().isEmpty()) { continue; throw new AssertionError("\n" + expectedTag.toString() + " is missing in the source file:" + "\n" + source.toString());
if (!expectedTag.getName().equals(sourceTag.getName())) { throw new ComparisonFailure("Tags not equal: ", expectedTag.getName().toString(), sourceTag.getName().toString()); for (Map.Entry<QName, String> attr : expectedTag.getAttributes().entrySet()) { if (ignoreAttr.contains(attr.getKey().getLocalPart())) { continue; if (sourceTag.getAttributes().containsKey(attr.getKey())) { if (!sourceTag.getAttributes().get(attr.getKey()).equals(attr.getValue())) { throw new ComparisonFailure("Attributes not equal: ", attr.getKey() + ":" + attr.getValue(), attr.getKey() + ":" + sourceTag.getAttributes().get(attr.getKey())); if (!StringUtils.isEmpty(expectedTag.getText()) && !expectedTag.getText().equals(sourceTag.getText())) { throw new ComparisonFailure("Text not equal: ", expectedTag.getText(), sourceTag.getText());
newTag = new Tag(); newTag.setName(reader.getName()); newTag.getAttributes().put(reader.getAttributeName(i), reader.getAttributeValue(i)); newTag.setText(reader.getText());
public int hashCode() { return getName().hashCode() + getAttributes().hashCode(); }
NodeList nl = parser.parse(null); // you can also filter here NodeList divs = nl.extractAllNodesThatMatch( new AndFilter(new TagNameFilter("DIV"), new HasAttributeFilter("class", "txt"))); if( divs.size() > 0 ) { Tag div = divs.elementAt(0); String text = div.getText(); // this is the text of the div }
public String toString() { StringBuilder sb = new StringBuilder(); sb.append(formatTag(this, 0)); return sb.toString(); }
private String formatTag(Tag tag, int indent) { StringBuilder sb = new StringBuilder(); sb.append(createIndent(indent)); sb.append(indent); sb.append("<"); sb.append(formatAttribute(tag)); sb.append(">"); if (tag.getParent() != null) { sb.append(" (" + tag.getParent().getName().getLocalPart() + ")"); } if (text != null) { sb.append(text); } sb.append("\n"); if (tag.getTags().size() > 0) { indent++; for (Tag subTag : tag.getTags()) { sb.append(formatTag(subTag, indent)); } } return sb.toString(); }
public boolean equals(Object object) { if (object == null) { return false; } if (!(object instanceof Tag)) { return false; } if (object == this) { return true; } Tag tag = (Tag) object; if (!getName().equals(tag.getName())) { return false; } for (QName attr : getAttributes().keySet()) { if (getIgnoreAttr().contains(attr.getLocalPart()) || getIgnoreAttr().contains(getName().getLocalPart() + "@" + attr.getLocalPart())) { continue; } if (!tag.getAttributes().containsKey(attr)) { return false; } if (!tag.getAttributes().get(attr).equals(getAttributes().get(attr))) { return false; } } return true; } }
private String formatAttribute(final Tag tag) { StringBuilder sb = new StringBuilder(); sb.append(tag.getName().getLocalPart()); sb.append(" "); for (Map.Entry<QName, String> attr : tag.getAttributes().entrySet()) { sb.append(attr.getKey()); sb.append("=\""); sb.append(attr.getValue()); sb.append("\" "); } return sb.toString().trim(); }