1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.betwixt.strategy;
  17. import org.apache.commons.betwixt.XMLUtils;
  18. /**
  19. * <code>NameMapper</code> implementation that processes a name by replacing or stripping
  20. * illegal characters before passing result down the chain.
  21. *
  22. * @author Robert Burrell Donkin
  23. * @since 0.5
  24. */
  25. public class BadCharacterReplacingNMapper implements NameMapper {
  26. /** Next mapper in chain, possibly null */
  27. private NameMapper chainedMapper;
  28. /** Replacement character, possibly null */
  29. private Character replacement = null;
  30. /**
  31. * Constructs a replacing mapper which delegates to given mapper.
  32. * @param chainedMapper next link in processing chain, possibly null
  33. */
  34. public BadCharacterReplacingNMapper(NameMapper chainedMapper) {
  35. this.chainedMapper = chainedMapper;
  36. }
  37. /**
  38. * Gets the character that should be used to replace bad characters
  39. * if null then bad characters will be deleted.
  40. * @return the replacement Character possibly null
  41. */
  42. public Character getReplacement() {
  43. return replacement;
  44. }
  45. /**
  46. * Sets the character that should be used to replace bad characters.
  47. * @param replacement the Charcter to be used for replacement if not null.
  48. * Otherwise, indicates that illegal characters should be deleted.
  49. */
  50. public void setReplacement( Character replacement ) {
  51. this.replacement = replacement;
  52. }
  53. /**
  54. * This implementation processes characters which are not allowed in xml
  55. * element names and then returns the result from the next link in the chain.
  56. * This processing consists of deleting them if no replacement character
  57. * has been set.
  58. * Otherwise, the character will be replaced.
  59. *
  60. * @param typeName the string to convert
  61. * @return the processed input
  62. */
  63. public String mapTypeToElementName(String typeName) {
  64. StringBuffer buffer = new StringBuffer( typeName );
  65. for (int i=0, size = buffer.length(); i< size; i++) {
  66. char nextChar = buffer.charAt( i );
  67. boolean bad = false;
  68. if ( i==0 ) {
  69. bad = !XMLUtils.isNameStartChar( nextChar );
  70. } else {
  71. bad = !XMLUtils.isNameChar( nextChar );
  72. }
  73. if (bad) {
  74. if ( replacement != null ) {
  75. buffer.setCharAt( i, replacement.charValue() );
  76. } else {
  77. // delete
  78. buffer.deleteCharAt( i );
  79. i--;
  80. size--;
  81. }
  82. }
  83. }
  84. if ( buffer.length() == 0 ) {
  85. throw new IllegalArgumentException(
  86. "Element name contains no legal characters and no replacements have been set.");
  87. }
  88. typeName = buffer.toString();
  89. if ( chainedMapper == null ) {
  90. return typeName;
  91. }
  92. return chainedMapper.mapTypeToElementName( typeName );
  93. }
  94. }