##### Coverage Report - org.apache.any23.util.StringUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
StringUtils |
0% | 0/49
0% | 0/40
| 4.0;4

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 |  |
18 |  |
     package org.apache.any23.util;
19 |  |
20 |  |
     /**
21 |  |
      * This class provides a set of string utility methods.
22 |  |
      *
23 |  |
      * @author Michele Mostarda (mostarda@fbk.eu)
24 |  |
      */
25 |  |
     public class StringUtils {
26 |  |
27 |  |
         /**
28 |  |
          * Joins the given input sting <code>data</code> list
29 |  |
          * using the specified <code>delimiter</code>.
30 |  |
          *
31 |  |
          * @param delimiter string delimiter.
32 |  |
          * @param data list of data to be joined.
33 |  |
          * @return the joined string.
34 |  |
          */
35 |  |
         public static String join(String delimiter, String... data) {
36 | 0 |
             final StringBuilder sb = new StringBuilder();
37 | 0 |
             for (int i = 0; i < data.length; i++) {
38 | 0 |
                 sb.append(data[i]);
39 | 0 |
                 if (i >= data.length - 1) {
40 | 0 |
                     break;
41 |  |
                 }
42 | 0 |
                 sb.append(delimiter);
43 |  |
             }
44 | 0 |
             return sb.toString();
45 |  |
         }
46 |  |
47 |  |
         /**
48 |  |
          * Counts how many times <code>content</code> appears within <code>container</code>
49 |  |
          * without string overlapping.
50 |  |
          *
51 |  |
          * @param container container string.
52 |  |
          * @param content content string.
53 |  |
          * @return occurrences count.
54 |  |
          */
55 |  |
         public static int countOccurrences(String container, String content) {
56 | 0 |
             int lastIndex, currIndex = 0, occurrences = 0;
57 |  |
             while(true) {
58 | 0 |
                 lastIndex = container.indexOf(content, currIndex);
59 | 0 |
                 if(lastIndex == -1) {
60 | 0 |
                     break;
61 |  |
                 }
62 | 0 |
                 currIndex = lastIndex + content.length();
63 | 0 |
                 occurrences++;
64 |  |
             }
65 | 0 |
             return occurrences;
66 |  |
         }
67 |  |
68 |  |
         /**
69 |  |
          * Counts the number of <code>NL</code> in the given <i>in</i> string.
70 |  |
          *
71 |  |
          * @param in input string.
72 |  |
          * @return the number of new line chars.
73 |  |
          */
74 |  |
         public static int countNL(String in) {
75 | 0 |
             return countOccurrences(in, "\n");
76 |  |
         }
77 |  |
78 |  |
79 |  |
         /**
80 |  |
          * Check whether string <code>candidatePrefix</code> is prefix of string <code>container</code>.
81 |  |
          *
82 |  |
          * @param candidatePrefix
83 |  |
          * @param container
84 |  |
          * @return
85 |  |
          */
86 |  |
         public static boolean isPrefix(String candidatePrefix, String container) {
87 | 0 |
             if(candidatePrefix == null || container == null) {
88 | 0 |
                 throw new NullPointerException("Arguments must be not null.");
89 |  |
             }
90 | 0 |
             if(candidatePrefix.length() > container.length()) {
91 | 0 |
                 return false;
92 |  |
             }
93 | 0 |
             for(int i = 0; i < candidatePrefix.length(); i++) {
94 | 0 |
                 if(candidatePrefix.charAt(i) != container.charAt(i)) {
95 | 0 |
                     return false;
96 |  |
                 }
97 |  |
             }
98 | 0 |
             return true;
99 |  |
         }
100 |  |
101 |  |
         /**
102 |  |
          * Check whether string <code>candidateSuffix</code> is suffix of string <code>container</code>.
103 |  |
          *
104 |  |
          * @param candidateSuffix
105 |  |
          * @param container
106 |  |
          * @return
107 |  |
          */
108 |  |
         public static boolean isSuffix(String candidateSuffix, String container) {
109 | 0 |
             if(candidateSuffix == null || container == null) {
110 | 0 |
                 throw new NullPointerException("Arguments must be not null.");
111 |  |
             }
112 | 0 |
             if(candidateSuffix.length() > container.length()) {
113 | 0 |
                 return false;
114 |  |
             }
115 | 0 |
             final int lenDiff = container.length() - candidateSuffix.length();
116 | 0 |
             for(int i = candidateSuffix.length() - 1; i >= 0; i--) {
117 | 0 |
                 if(candidateSuffix.charAt(i) != container.charAt(i + lenDiff)) {
118 | 0 |
                     return false;
119 |  |
                 }
120 |  |
             }
121 | 0 |
             return true;
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * Escapes all the unescaped double quotes when needed.
126 |  |
          *
127 |  |
          * @param in input string.
128 |  |
          * @return unescaped output.
129 |  |
          */
130 |  |
         public static String escapeDoubleQuotes(String in) {
131 | 0 |
             final StringBuilder out = new StringBuilder();
132 | 0 |
             boolean escaped = false;
133 |  |
             char current;
134 | 0 |
             for(int i = 0; i < in.length(); i++) {
135 | 0 |
                 current = in.charAt(i);
136 | 0 |
                 if(current == '\\') {
137 | 0 |
                     escaped = !escaped;
138 | 0 |
                 } else if(current == '"' && !escaped) {
139 | 0 |
                     out.append('\\');
140 |  |
                 }
141 | 0 |
                 out.append(current);
142 |  |
             }
143 | 0 |
             return out.toString();
144 |  |
         }
145 |  |
146 |  |
         /**
147 |  |
          * Escapes the <code>in</code> string as <b>JSON</b> string
148 |  |
          * to let it being embeddable within a string field.
149 |  |
          *
150 |  |
          * @param in string to be escaped.
151 |  |
          * @return escaped string.
152 |  |
          */
153 |  |
         public static String escapeAsJSONString(String in) {
154 | 0 |
             return escapeDoubleQuotes( in.replaceAll("\n", "\\\\n") );
155 |  |
         }
156 |  |
157 |  |
         /**
158 |  |
          * Builds a string composed of the given char <code>c<code/> <code>n</code> times.
159 |  |
          *
160 |  |
          * @param c char to be multiplied.
161 |  |
          * @param times number of times.
162 |  |
          * @return the string containing the multiplied char.
163 |  |
          */
164 |  |
         public static String multiply(char c, int times) {
165 | 0 |
             if(times <= 0) throw new IllegalArgumentException("Invalid number of times, must be > 0 .");
166 | 0 |
             final char[] buffer = new char[times];
167 | 0 |
             for(int i = 0; i < times; i++) {
168 | 0 |
                 buffer[i] = c;
169 |  |
             }
170 | 0 |
             return new String(buffer);
171 |  |
         }
172 |  |
173 | 0 |
         private StringUtils() {}
174 |  |
     }