| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of the GNU
|
|---|
| 7 | * General Public License Version 3 only ("GPL").
|
|---|
| 8 | * You may not use this file except in compliance with the License.
|
|---|
| 9 | * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
|
|---|
| 10 | * See the License for the specific language governing permissions and limitations under the License.
|
|---|
| 11 | *
|
|---|
| 12 | * When distributing the software, include this License Header Notice in each file.
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | /*
|
|---|
| 17 | * Créé le 3 mars 2005
|
|---|
| 18 | */
|
|---|
| 19 | package org.jopendocument.util;
|
|---|
| 20 |
|
|---|
| 21 | import java.util.HashMap;
|
|---|
| 22 | import java.util.LinkedHashMap;
|
|---|
| 23 | import java.util.Map;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @author Sylvain CUAZ
|
|---|
| 27 | */
|
|---|
| 28 | public class StringUtils {
|
|---|
| 29 |
|
|---|
| 30 | public static final class Escaper {
|
|---|
| 31 |
|
|---|
| 32 | // eg '
|
|---|
| 33 | private final char esc;
|
|---|
| 34 |
|
|---|
| 35 | // eg { '=> S, " => D}
|
|---|
| 36 | private final Map<Character, Character> substitution;
|
|---|
| 37 | private final Map<Character, Character> inv;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * A new escaper that will have <code>esc</code> as escape character.
|
|---|
| 41 | *
|
|---|
| 42 | * @param esc the escape character, eg '
|
|---|
| 43 | * @param name the character that will be appended to <code>esc</code>, eg with S all
|
|---|
| 44 | * occurrences of ' will be replaced by 'S
|
|---|
| 45 | */
|
|---|
| 46 | public Escaper(char esc, char name) {
|
|---|
| 47 | super();
|
|---|
| 48 | this.esc = esc;
|
|---|
| 49 | this.substitution = new LinkedHashMap<>();
|
|---|
| 50 | this.inv = new HashMap<>();
|
|---|
| 51 | this.add(esc, name);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public Escaper add(char toRemove, char escapedName) {
|
|---|
| 55 | if (this.inv.containsKey(escapedName))
|
|---|
| 56 | throw new IllegalArgumentException(escapedName + " already replaces " + this.inv.get(escapedName));
|
|---|
| 57 | this.substitution.put(toRemove, escapedName);
|
|---|
| 58 | this.inv.put(escapedName, toRemove);
|
|---|
| 59 | return this;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Override
|
|---|
| 63 | public boolean equals(Object obj) {
|
|---|
| 64 | if (obj instanceof Escaper) {
|
|---|
| 65 | final Escaper o = (Escaper) obj;
|
|---|
| 66 | return this.esc == o.esc && this.substitution.equals(o.substitution);
|
|---|
| 67 | } else
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | @Override
|
|---|
| 72 | public int hashCode() {
|
|---|
| 73 | return this.esc + this.substitution.hashCode();
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | }
|
|---|