|
12 | 12 | import java.nio.file.SimpleFileVisitor;
|
13 | 13 | import java.nio.file.attribute.BasicFileAttributes;
|
14 | 14 | import java.util.ArrayList;
|
15 |
| -import java.util.Collections; |
16 | 15 | import java.util.List;
|
17 | 16 |
|
18 | 17 | import org.springframework.web.multipart.MultipartFile;
|
19 | 18 |
|
| 19 | +import com.fathzer.soft.javaluator.DoubleEvaluator; |
| 20 | + |
20 | 21 | import io.github.pixee.security.HostValidator;
|
21 | 22 | import io.github.pixee.security.Urls;
|
22 | 23 |
|
@@ -115,91 +116,114 @@ public static Long convertSizeToBytes(String sizeStr) {
|
115 | 116 | return null;
|
116 | 117 | }
|
117 | 118 |
|
118 |
| - public static List<Integer> parsePageString(String pageOrder, int totalPages) { |
119 |
| - return parsePageString(pageOrder, totalPages, false); |
120 |
| - } |
121 |
| - |
122 |
| - public static List<Integer> parsePageString( |
123 |
| - String pageOrder, int totalPages, boolean isOneBased) { |
124 |
| - if (pageOrder == null || pageOrder.isEmpty()) { |
125 |
| - return Collections.singletonList(1); |
| 119 | + public static List<Integer> parsePageList(String pages, int totalPages, boolean oneBased) { |
| 120 | + if (pages == null) { |
| 121 | + return List.of(1); // Default to first page if input is null |
126 | 122 | }
|
127 |
| - if (pageOrder.matches("\\d+")) { |
128 |
| - // Convert the single number string to an integer and return it in a list |
129 |
| - return Collections.singletonList(Integer.parseInt(pageOrder)); |
| 123 | + try { |
| 124 | + return parsePageList(pages.split(","), totalPages, oneBased); |
| 125 | + } catch (NumberFormatException e) { |
| 126 | + return List.of(1); // Default to first page if input is invalid |
130 | 127 | }
|
131 |
| - return parsePageList(pageOrder.split(","), totalPages, isOneBased); |
132 | 128 | }
|
133 | 129 |
|
134 |
| - public static List<Integer> parsePageList(String[] pageOrderArr, int totalPages) { |
135 |
| - return parsePageList(pageOrderArr, totalPages, false); |
| 130 | + public static List<Integer> parsePageList(String[] pages, int totalPages) { |
| 131 | + return parsePageList(pages, totalPages, false); |
136 | 132 | }
|
137 | 133 |
|
138 |
| - public static List<Integer> parsePageList( |
139 |
| - String[] pageOrderArr, int totalPages, boolean isOneBased) { |
140 |
| - List<Integer> newPageOrder = new ArrayList<>(); |
141 |
| - |
142 |
| - int adjustmentFactor = isOneBased ? 1 : 0; |
143 |
| - |
144 |
| - // loop through the page order array |
145 |
| - for (String element : pageOrderArr) { |
146 |
| - if ("all".equalsIgnoreCase(element)) { |
| 134 | + public static List<Integer> parsePageList(String[] pages, int totalPages, boolean oneBased) { |
| 135 | + List<Integer> result = new ArrayList<>(); |
| 136 | + int offset = oneBased ? 1 : 0; |
| 137 | + for (String page : pages) { |
| 138 | + if ("all".equalsIgnoreCase(page)) { |
147 | 139 | for (int i = 0; i < totalPages; i++) {
|
148 |
| - newPageOrder.add(i + adjustmentFactor); |
| 140 | + result.add(i + offset); |
149 | 141 | }
|
150 |
| - // As all pages are already added, no need to check further |
151 |
| - break; |
152 |
| - } else if (element.matches("\\d*n\\+?-?\\d*|\\d*\\+?n")) { |
153 |
| - // Handle page order as a function |
154 |
| - int coefficient = 0; |
155 |
| - int constant = 0; |
156 |
| - boolean coefficientExists = false; |
157 |
| - boolean constantExists = false; |
158 |
| - |
159 |
| - if (element.contains("n")) { |
160 |
| - String[] parts = element.split("n"); |
161 |
| - if (!"".equals(parts[0]) && parts[0] != null) { |
162 |
| - coefficient = Integer.parseInt(parts[0]); |
163 |
| - coefficientExists = true; |
164 |
| - } |
165 |
| - if (parts.length > 1 && !"".equals(parts[1]) && parts[1] != null) { |
166 |
| - constant = Integer.parseInt(parts[1]); |
167 |
| - constantExists = true; |
168 |
| - } |
169 |
| - } else if (element.contains("+")) { |
170 |
| - constant = Integer.parseInt(element.replace("+", "")); |
171 |
| - constantExists = true; |
| 142 | + } else if (page.contains(",")) { |
| 143 | + // Split the string into parts, could be single pages or ranges |
| 144 | + String[] parts = page.split(","); |
| 145 | + for (String part : parts) { |
| 146 | + result.addAll(handlePart(part, totalPages, offset)); |
172 | 147 | }
|
| 148 | + } else { |
| 149 | + result.addAll(handlePart(page, totalPages, offset)); |
| 150 | + } |
| 151 | + } |
| 152 | + return new ArrayList<>( |
| 153 | + new java.util.LinkedHashSet<>(result)); // Remove duplicates and maintain order |
| 154 | + } |
| 155 | + |
| 156 | + public static List<Integer> evaluateNFunc(String expression, int maxValue) { |
| 157 | + List<Integer> results = new ArrayList<>(); |
| 158 | + DoubleEvaluator evaluator = new DoubleEvaluator(); |
| 159 | + |
| 160 | + // Validate the expression |
| 161 | + if (!expression.matches("[0-9n+\\-*/() ]+")) { |
| 162 | + throw new IllegalArgumentException("Invalid expression"); |
| 163 | + } |
173 | 164 |
|
174 |
| - for (int i = 1; i <= totalPages; i++) { |
175 |
| - int pageNum = coefficientExists ? coefficient * i : i; |
176 |
| - pageNum += constantExists ? constant : 0; |
| 165 | + int n = 0; |
| 166 | + while (true) { |
| 167 | + // Replace 'n' with the current value of n, correctly handling numbers before 'n' |
| 168 | + String sanitizedExpression = insertMultiplicationBeforeN(expression, n); |
| 169 | + Double result = evaluator.evaluate(sanitizedExpression); |
177 | 170 |
|
178 |
| - if (pageNum <= totalPages && pageNum > 0) { |
179 |
| - newPageOrder.add(pageNum - adjustmentFactor); |
| 171 | + // Check if the result is null or not within bounds |
| 172 | + if (result == null || result <= 0 || result.intValue() > maxValue) { |
| 173 | + if (n != 0) break; |
| 174 | + } else { |
| 175 | + results.add(result.intValue()); |
| 176 | + } |
| 177 | + n++; |
| 178 | + } |
| 179 | + |
| 180 | + return results; |
| 181 | + } |
| 182 | + |
| 183 | + private static String insertMultiplicationBeforeN(String expression, int nValue) { |
| 184 | + // Insert multiplication between a number and 'n' (e.g., "4n" becomes "4*n") |
| 185 | + String withMultiplication = expression.replaceAll("(\\d)n", "$1*n"); |
| 186 | + // Now replace 'n' with its current value |
| 187 | + return withMultiplication.replaceAll("n", String.valueOf(nValue)); |
| 188 | + } |
| 189 | + |
| 190 | + private static List<Integer> handlePart(String part, int totalPages, int offset) { |
| 191 | + List<Integer> partResult = new ArrayList<>(); |
| 192 | + |
| 193 | + // First check for n-syntax because it should not be processed as a range |
| 194 | + if (part.contains("n")) { |
| 195 | + partResult = evaluateNFunc(part, totalPages); |
| 196 | + // Adjust the results according to the offset |
| 197 | + for (int i = 0; i < partResult.size(); i++) { |
| 198 | + int adjustedValue = partResult.get(i) - 1 + offset; |
| 199 | + partResult.set(i, adjustedValue); |
| 200 | + } |
| 201 | + } else if (part.contains("-")) { |
| 202 | + // Process ranges only if it's not n-syntax |
| 203 | + String[] rangeParts = part.split("-"); |
| 204 | + try { |
| 205 | + int start = Integer.parseInt(rangeParts[0]); |
| 206 | + int end = Integer.parseInt(rangeParts[1]); |
| 207 | + for (int i = start; i <= end; i++) { |
| 208 | + if (i >= 1 && i <= totalPages) { |
| 209 | + partResult.add(i - 1 + offset); |
180 | 210 | }
|
181 | 211 | }
|
182 |
| - } else if (element.contains("-")) { |
183 |
| - // split the range into start and end page |
184 |
| - String[] range = element.split("-"); |
185 |
| - int start = Integer.parseInt(range[0]); |
186 |
| - int end = Integer.parseInt(range[1]); |
187 |
| - // check if the end page is greater than total pages |
188 |
| - if (end > totalPages) { |
189 |
| - end = totalPages; |
190 |
| - } |
191 |
| - // loop through the range of pages |
192 |
| - for (int j = start; j <= end; j++) { |
193 |
| - // print the current index |
194 |
| - newPageOrder.add(j - adjustmentFactor); |
| 212 | + } catch (NumberFormatException e) { |
| 213 | + // Range is invalid, ignore this part |
| 214 | + } |
| 215 | + } else { |
| 216 | + // This is a single page number |
| 217 | + try { |
| 218 | + int pageNum = Integer.parseInt(part.trim()); |
| 219 | + if (pageNum >= 1 && pageNum <= totalPages) { |
| 220 | + partResult.add(pageNum - 1 + offset); |
195 | 221 | }
|
196 |
| - } else { |
197 |
| - // if the element is a single page |
198 |
| - newPageOrder.add(Integer.parseInt(element) - adjustmentFactor); |
| 222 | + } catch (NumberFormatException ignored) { |
| 223 | + // Ignore invalid numbers |
199 | 224 | }
|
200 | 225 | }
|
201 |
| - |
202 |
| - return newPageOrder; |
| 226 | + return partResult; |
203 | 227 | }
|
204 | 228 |
|
205 | 229 | public static boolean createDir(String path) {
|
|
0 commit comments