public boolean match(String search, Tag child, Tag mapping) { String target = child.getName(); String sn = null; String tn = null; if (search.equals("*")) return true; int s = search.indexOf(':'); if (s > 0) { sn = search.substring(0, s); search = search.substring(s + 1); } int t = target.indexOf(':'); if (t > 0) { tn = target.substring(0, t); target = target.substring(t + 1); } if (!search.equals(target)) // different tag names return false; if (mapping == null) { return tn == sn || (sn != null && sn.equals(tn)); } else { String suri = sn == null ? mapping.getAttribute("xmlns") : mapping.getAttribute("xmlns:" + sn); String turi = tn == null ? child.findRecursiveAttribute("xmlns") : child.findRecursiveAttribute("xmlns:" + tn); return turi == suri || (turi != null && suri != null && turi.equals(suri)); } }
/** Print the tag formatted to a PrintWriter. */ public void print(int indent, PrintWriter pw) { pw.print("\n"); spaces(pw, indent); pw.print('<'); pw.print(name); for (Map.Entry<String, String> e : attributes.entrySet()) { String key = e.getKey(); String value = escape(e.getValue()); pw.print(' '); pw.print(key); pw.print("="); String quote = "'"; if (value.indexOf(quote) >= 0) quote = "\""; pw.print(quote); pw.print(value); pw.print(quote); } if (content.size() == 0) pw.print('/'); else { pw.print('>'); for (Object content : this.content) { if (content instanceof String) { formatted(pw, indent + 2, 60, escape((String) content)); } else if (content instanceof Tag) { Tag tag = (Tag) content; tag.print(indent + 2, pw); } } pw.print("\n"); spaces(pw, indent); pw.print("</"); pw.print(name); } pw.print('>'); }
void select(String path, Vector<Object> results, Tag mapping) { if (path.startsWith("//")) { int i = path.indexOf('/', 2); String name = path.substring(2, i < 0 ? path.length() : i); for (Object o : content) { if (o instanceof Tag) { Tag child = (Tag) o; if (match(name, child, mapping)) results.add(child); child.select(path, results, mapping); } } return; } if (path.length() == 0) { results.addElement(this); return; } int i = path.indexOf("/"); String elementName = path; String remainder = ""; if (i > 0) { elementName = path.substring(0, i); remainder = path.substring(i + 1); } for (Object o : content) { if (o instanceof Tag) { Tag child = (Tag) o; if (child.getName().equals(elementName) || elementName.equals("*")) child.select(remainder, results, mapping); } } }
public static Tag toXML(Requirement requirement) { Tag req = new Tag("require"); req.addAttribute("name", requirement.getName()); req.addAttribute("filter", requirement.getFilter()); req.addAttribute("optional", requirement.isOptional() + ""); req.addAttribute("multiple", requirement.isMultiple() + ""); req.addAttribute("extend", requirement.isExtend() + ""); if (requirement.getComment() != null) req.addContent(requirement.getComment()); return req; }
/** Add a new content tag. */ public void addContent(Tag tag) { content.addElement(tag); tag.parent = this; }
public static void convert(Collection<Map<String, String>> c, String type, Tag parent) { for (Map<String, String> map : c) { parent.addContent(new Tag(type, map)); } }
public String findRecursiveAttribute(String name) { String value = getAttribute(name); if (value != null) return value; if (parent != null) return parent.findRecursiveAttribute(name); return null; }
/** convenient method to get the contents in a StringBuffer. */ public void getContentsAsString(StringBuffer sb) { for (Object o : content) { if (o instanceof Tag) ((Tag) o).getContentsAsString(sb); else sb.append(o.toString()); } }
public Tag toXML(String name) { Tag tag = toXML(this); tag.rename(name); return tag; }