KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > changelog > ChangeLogUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Ralph Krueger.
17  */

18
19 package org.netbeans.modules.changelog;
20
21
22 import java.util.*;
23 import org.openide.*;
24 import java.io.*;
25
26
27 /**
28  * A utility class.
29  * @author ralph
30  */

31 public class ChangeLogUtils {
32
33     /** method that converts the string value of the revision into an array of
34      * integers for further processing
35      */

36     public static int[] convertRevisionToIntArray(String JavaDoc revision) {
37        StringTokenizer token = new StringTokenizer(revision, ".");
38        int[] array = new int[token.countTokens()];
39        int index = 0;
40        while (token.hasMoreTokens()) {
41            String JavaDoc item = token.nextToken();
42            try {
43               int parsedNumber = Integer.parseInt(item);
44               array[index] = parsedNumber;
45            } catch (NumberFormatException JavaDoc exc) {
46               array[index] = 0;
47            }
48            index = index + 1;
49        }
50        return array;
51     }
52
53     /**
54      * converts array of integers to a String conforming to the format of the cvs revisions
55      * zero values are ignored. Can be used to shorten the resulting revision.
56      */

57     public static String JavaDoc convertIntArrayToRevision(int[] arr) {
58         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59         for (int i = 0; i < arr.length; i++) {
60             if (arr[i] == 0) {
61                 continue;
62             }
63             if (i != 0) {
64                 buffer.append('.');
65             }
66             buffer.append(arr[i]);
67         }
68         return buffer.toString();
69     }
70     
71
72     /**
73      * @param replMap - expects patterns as keys and replacement values as values
74      * aka ("filePath", "javacvs/build.xml")
75      */

76     public static String JavaDoc replaceArguments(String JavaDoc original, HashMap replMap) {
77         Iterator it = replMap.keySet().iterator();
78         while (it.hasNext()) {
79             String JavaDoc pattern = (String JavaDoc)it.next();
80             String JavaDoc value = (String JavaDoc)replMap.get(pattern);
81             pattern = "{" + pattern + "}";
82             int index = original.indexOf(pattern);
83             if (index >= 0) {
84                 original = original.substring(0, index) +
85                           value + original.substring(index + pattern.length());
86             }
87         }
88         return original;
89     }
90     
91     private static final char[] charArray = new char[] {'>', '<', '&'};
92     private static final String JavaDoc[] stringArray =
93         new String JavaDoc[] { "&gt;", "&lt;", "&amp;"};
94     
95     public static String JavaDoc escapeString(String JavaDoc original) {
96         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(original);
97         int index = 0;
98         while (index < buffer.length()) {
99             char character = buffer.charAt(index);
100             for (int i = 0; i < charArray.length; i++) {
101                 if (character == charArray[i]) {
102                     buffer.deleteCharAt(index);
103                     buffer.insert(index, stringArray[i]);
104                     index = index + stringArray[i].length();
105                     continue;
106                 }
107             }
108             index = index + 1;
109         }
110         return buffer.toString();
111     }
112     
113     private static final char[] xmlcharArray = new char[] {'>', '<', '&', '"', '\''};
114     private static final String JavaDoc[] xmlstringArray =
115         new String JavaDoc[] { "&gt;", "&lt;", "&amp;", "&quot;", "&apos;" };
116     
117     public static String JavaDoc xmlescapeString(String JavaDoc original) {
118         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(original);
119         int index = 0;
120         while (index < buffer.length()) {
121             char character = buffer.charAt(index);
122             for (int i = 0; i < xmlcharArray.length; i++) {
123                 if (character == xmlcharArray[i]) {
124                     buffer.deleteCharAt(index);
125                     buffer.insert(index, xmlstringArray[i]);
126                     index = index + xmlstringArray[i].length();
127                     continue;
128                 }
129             }
130             index = index + 1;
131         }
132         return buffer.toString();
133     }
134
135 }
136
Popular Tags