| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.util; |
| 20 | |
| 21 | import java.io.UnsupportedEncodingException; |
| 22 | import java.net.URLEncoder; |
| 23 | import java.util.concurrent.ExecutionException; |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | import com.google.common.cache.Cache; |
| 27 | import com.google.common.cache.CacheBuilder; |
| 28 | import com.google.common.cache.CacheLoader; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class Patterns { |
| 35 | public static final Pattern TOKEN_PATTERN = Pattern.compile("\\{(.+?)\\}"); |
| 36 | public static final Pattern TWO_SPACE_PATTERN = Pattern.compile(" "); |
| 37 | public static final Pattern URL_ENCODED_PATTERN = Pattern.compile(".*%[a-fA-F0-9][a-fA-F0-9].*"); |
| 38 | public static final Pattern URI_PATTERN = Pattern.compile("([a-z0-9]+)://([^:]*):(.*)@(.*)"); |
| 39 | public static final Pattern PATTERN_THAT_BREAKS_URI = Pattern.compile("[a-z0-9]+://.*/.*@.*"); |
| 40 | public static final Pattern JSON_STRING_PATTERN = Pattern.compile("^[^\"\\{\\[].*[^\\{\\[\"]$"); |
| 41 | public static final Pattern JSON_NUMBER_PATTERN = Pattern.compile("^[0-9]*\\.?[0-9]*$"); |
| 42 | public static final Pattern PLUS_PATTERN = Pattern.compile("\\+"); |
| 43 | public static final Pattern STAR_PATTERN = Pattern.compile("\\*"); |
| 44 | public static final Pattern _7E_PATTERN = Pattern.compile("%7E"); |
| 45 | public static final Pattern NEWLINE_PATTERN = Pattern.compile("\r?\n"); |
| 46 | public static final Pattern SLASH_PATTERN = Pattern.compile("[/]"); |
| 47 | public static final Pattern IP_PATTERN = Pattern.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 Cache<Character, Pattern> CHAR_TO_ENCODED_PATTERN = CacheBuilder.newBuilder() |
| 54 | .<Character, Pattern> build(new CacheLoader<Character, Pattern>() { |
| 55 | @Override |
| 56 | public Pattern load(Character plain) throws ExecutionException { |
| 57 | try { |
| 58 | String encoded = URLEncoder.encode(plain + "", "UTF-8"); |
| 59 | return Pattern.compile(encoded); |
| 60 | } catch (UnsupportedEncodingException e) { |
| 61 | throw new ExecutionException("Bad encoding on input: " + plain, e); |
| 62 | } |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | public final static Cache<Character, Pattern> CHAR_TO_PATTERN = CacheBuilder.newBuilder() |
| 67 | .<Character, Pattern> build(new CacheLoader<Character, Pattern>() { |
| 68 | @Override |
| 69 | public Pattern load(Character plain) { |
| 70 | return Pattern.compile(plain + ""); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | public final static Cache<String, Pattern> TOKEN_TO_PATTERN = CacheBuilder.newBuilder() |
| 75 | .<String, Pattern> build(new CacheLoader<String, Pattern>() { |
| 76 | @Override |
| 77 | public Pattern load(String tokenValue) { |
| 78 | return Pattern.compile("\\{" + tokenValue + "\\}"); |
| 79 | } |
| 80 | }); |
| 81 | } |