KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > xml > QuoteMetaCharacters


1 /*
2  * XML input/output support for FindBugs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.xml;
21
22 import java.io.IOException JavaDoc;
23
24 import edu.umd.cs.findbugs.annotations.NonNull;
25
26 /**
27  * Quote metacharacters in a String.
28  *
29  * @see MetaCharacterMap
30  * @author David Hovemeyer
31  */

32 public abstract class QuoteMetaCharacters {
33     private String JavaDoc text;
34     private MetaCharacterMap map;
35
36     /**
37      * Constructor.
38      *
39      * @param text the text in which we want to quote metacharacters
40      * @param map the MetaCharacterMap
41      */

42     public QuoteMetaCharacters(@NonNull String JavaDoc text, @NonNull MetaCharacterMap map) {
43         if (text == null) throw new NullPointerException JavaDoc("text must be nonnull");
44         if (map == null) throw new NullPointerException JavaDoc("map must be nonnull");
45         this.text = text;
46         this.map = map;
47     }
48
49     /**
50      * Quote metacharacters in the text.
51      */

52     public void process() throws IOException JavaDoc {
53         int pos = 0;
54         do {
55             int meta = findNextMeta(text, pos);
56             if (meta >= 0) {
57                 emitLiteral(text.substring(pos, meta));
58                 emitLiteral(map.getReplacement(text.substring(meta, meta + 1)));
59                 pos = meta + 1;
60             } else {
61                 emitLiteral(text.substring(pos, text.length()));
62                 pos = text.length();
63             }
64         } while (pos < text.length());
65     }
66
67     /**
68      * Downcall method to emit literal text,
69      * in which any occurrences of the metacharacters
70      * are quoted.
71      *
72      * @param s the literal text to emit
73      */

74     public abstract void emitLiteral(String JavaDoc s) throws IOException JavaDoc;
75
76     private int findNextMeta(String JavaDoc s, int start) {
77         for (int i = start; i < s.length(); ++i) {
78             char c = s.charAt(i);
79             if (map.isMeta(c))
80                 return i;
81         }
82         return -1;
83     }
84 }
85
86 // vim:ts=4
87
Popular Tags