1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.util; |
20 | |
21 | import java.io.UnsupportedEncodingException; |
22 | import java.net.URLEncoder; |
23 | import java.util.Map; |
24 | import java.util.regex.Pattern; |
25 | |
26 | import com.google.common.base.Function; |
27 | import com.google.common.collect.MapMaker; |
28 | |
29 | /** |
30 | * |
31 | * @author Adrian Cole |
32 | */ |
33 | public class Patterns { |
34 | public static final Pattern TOKEN_PATTERN = Pattern.compile("\\{(.+?)\\}"); |
35 | public static final Pattern TWO_SPACE_PATTERN = Pattern.compile(" "); |
36 | public static final Pattern URL_ENCODED_PATTERN = Pattern.compile(".*%[a-fA-F0-9][a-fA-F0-9].*"); |
37 | public static final Pattern URI_PATTERN = Pattern.compile("([a-z0-9]+)://([^:]*):(.*)@(.*)"); |
38 | public static final Pattern PATTERN_THAT_BREAKS_URI = Pattern.compile("[a-z0-9]+://.*/.*@.*"); |
39 | public static final Pattern JSON_STRING_PATTERN = Pattern.compile("^[^\"\\{\\[].*[^\\{\\[\"]$"); |
40 | public static final Pattern JSON_NUMBER_PATTERN = Pattern.compile("^[0-9]*\\.?[0-9]*$"); |
41 | public static final Pattern PLUS_PATTERN = Pattern.compile("\\+"); |
42 | public static final Pattern STAR_PATTERN = Pattern.compile("\\*"); |
43 | public static final Pattern _7E_PATTERN = Pattern.compile("%7E"); |
44 | public static final Pattern NEWLINE_PATTERN = Pattern.compile("\r?\n"); |
45 | public static final Pattern SLASH_PATTERN = Pattern.compile("[/]"); |
46 | public static final Pattern IP_PATTERN = Pattern |
47 | .compile("b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).)" |
48 | + "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)b"); |
49 | public static final Pattern LEADING_SLASHES = Pattern.compile("^[/]+"); |
50 | public static final Pattern TRAILING_SLASHES = Pattern.compile("[/]*$"); |
51 | public static final Pattern REST_CONTEXT_BUILDER = Pattern.compile("(.*ContextBuilder)<([^,]+), ?([^>]+)>"); |
52 | |
53 | public final static Map<Character, Pattern> CHAR_TO_ENCODED_PATTERN = new MapMaker() |
54 | .makeComputingMap(new Function<Character, Pattern>() { |
55 | public Pattern apply(Character plain) { |
56 | try { |
57 | String encoded = URLEncoder.encode(plain + "", "UTF-8"); |
58 | return Pattern.compile(encoded); |
59 | } catch (UnsupportedEncodingException e) { |
60 | throw new IllegalStateException("Bad encoding on input: " + plain, e); |
61 | } |
62 | } |
63 | }); |
64 | |
65 | public final static Map<Character, Pattern> CHAR_TO_PATTERN = new MapMaker() |
66 | .makeComputingMap(new Function<Character, Pattern>() { |
67 | public Pattern apply(Character plain) { |
68 | return Pattern.compile(plain + ""); |
69 | } |
70 | }); |
71 | |
72 | public final static Map<String, Pattern> TOKEN_TO_PATTERN = new MapMaker() |
73 | .makeComputingMap(new Function<String, Pattern>() { |
74 | public Pattern apply(String tokenValue) { |
75 | return Pattern.compile("\\{" + tokenValue + "\\}"); |
76 | } |
77 | }); |
78 | } |