Java tutorial
//package com.java2s; public class Main { public static final String START_BRACKET = "<"; public static final String END_BRACKET = ">"; /** * Append a start tag to the string buffer. * * @param output * StringBuilder of the output. * @param propertyName */ public static void appendStartNode(StringBuilder output, String propertyName) { output.append(START_BRACKET); output.append(getCleanPropertyName(propertyName)); output.append(END_BRACKET); } /** * Checks that the first letter in the string is a letter and not a digit. * If this is true is returns the untouched properyName * <p/> * If it starts with something other then a letter then it prepends an 'n' * character to the front of the string. * * @param propertyName * @return a property name that started with 'n' is it starts with something * other then a letter. */ public static String getCleanPropertyName(String propertyName) { StringBuilder sb = new StringBuilder(); if (propertyName != null && propertyName.length() > 0) { char firstValue = propertyName.charAt(0); if (Character.isDigit(firstValue) || !Character.isLetter(firstValue)) { // propertyName = "n" + propertyName; sb.append('n'); } for (int i = 0; i < propertyName.length(); i++) { char iChar = propertyName.charAt(i); if (Character.isLetterOrDigit(iChar) || iChar == '.' || iChar == '-' || iChar == '_' || iChar == ':') { sb.append(iChar); } } } return sb.toString(); } }