/** Encode a URI and local name to an exploded QName (also known as a "Clark name") String. */ public static String buildExplodedQName(String uri, String localname) { if ("".equals(uri)) return localname; else { final StringBuilder sb = new StringBuilder(uri.length() + localname.length() + 2); sb.append('{'); sb.append(uri); sb.append('}'); sb.append(localname); return sb.toString(); } }
private static void mapPrefixIfNeeded( Set<String> declaredPrefixes, String uri, String qName, StringBuilder sb) { final String prefix = XMLUtils.prefixFromQName(qName); if (prefix.length() > 0 && !declaredPrefixes.contains(prefix)) { sb.append(" xmlns:"); sb.append(prefix); sb.append("=\""); sb.append(uri); sb.append("\""); declaredPrefixes.add(prefix); } }
/** * Make an NCName out of a non-blank string. Any characters that do not belong in an NCName are * converted to '_'. * * @param name source * @return NCName */ public static String makeNCName(String name) { if (StringUtils.isBlank(name)) throw new IllegalArgumentException("Name must not be blank or empty"); final Name10Checker name10Checker = Name10Checker.getInstance(); if (name10Checker.isValidNCName(name)) { return name; } else { final StringBuilder sb = new StringBuilder(); final char start = name.charAt(0); sb.append(name10Checker.isNCNameStartChar(start) ? start : '_'); for (int i = 1; i < name.length(); i++) { final char ch = name.charAt(i); sb.append(name10Checker.isNCNameChar(ch) ? ch : '_'); } return sb.toString(); } }
private static String nZeros(int n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) sb.append('0'); return sb.toString(); }
public static String saxElementToDebugString(String uri, String qName, Attributes attributes) { // Open start tag final StringBuilder sb = new StringBuilder("<"); sb.append(qName); final Set<String> declaredPrefixes = new HashSet<String>(); mapPrefixIfNeeded(declaredPrefixes, uri, qName, sb); // Attributes if any for (int i = 0; i < attributes.getLength(); i++) { mapPrefixIfNeeded(declaredPrefixes, attributes.getURI(i), attributes.getQName(i), sb); sb.append(' '); sb.append(attributes.getQName(i)); sb.append("=\""); sb.append(attributes.getValue(i)); sb.append('\"'); } // Close start tag sb.append('>'); // Content sb.append("[...]"); // Close element with end tag sb.append("</"); sb.append(qName); sb.append('>'); return sb.toString(); }