1 16 package org.apache.commons.betwixt.strategy; 17 18 import org.apache.commons.betwixt.XMLUtils; 19 20 27 public class BadCharacterReplacingNMapper implements NameMapper { 28 29 private NameMapper chainedMapper; 30 31 private Character replacement = null; 32 33 37 public BadCharacterReplacingNMapper(NameMapper chainedMapper) { 38 this.chainedMapper = chainedMapper; 39 } 40 41 46 public Character getReplacement() { 47 return replacement; 48 } 49 50 55 public void setReplacement( Character replacement ) { 56 this.replacement = replacement; 57 } 58 59 69 public String mapTypeToElementName(String typeName) { 70 71 StringBuffer buffer = new StringBuffer ( typeName ); 72 for (int i=0, size = buffer.length(); i< size; i++) { 73 char nextChar = buffer.charAt( i ); 74 boolean bad = false; 75 if ( i==0 ) { 76 bad = !XMLUtils.isNameStartChar( nextChar ); 77 } else { 78 bad = !XMLUtils.isNameChar( nextChar ); 79 } 80 81 if (bad) { 82 if ( replacement != null ) { 83 buffer.setCharAt( i, replacement.charValue() ); 84 } else { 85 buffer.deleteCharAt( i ); 87 i--; 88 size--; 89 } 90 } 91 } 92 93 if ( buffer.length() == 0 ) { 94 throw new IllegalArgumentException ( 95 "Element name contains no legal characters and no replacements have been set."); 96 } 97 98 typeName = buffer.toString(); 99 100 if ( chainedMapper == null ) { 101 return typeName; 102 } 103 return chainedMapper.mapTypeToElementName( typeName ); 104 } 105 } 106 | Popular Tags |