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