1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.api.config.domain; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 /** 22 * Helper to parse dependencies from a property value. A dependency is a place 23 * holder that is to be resolved to resolve the property's value. 24 */ 25 final class DependenciesParser 26 { 27 // ********************************* Fields ********************************* 28 29 // --- constants ------------------------------------------------------------ 30 31 // --- members -------------------------------------------------------------- 32 33 // ****************************** Initializer ******************************* 34 35 // ****************************** Constructors ****************************** 36 37 // ****************************** Inner Classes ***************************** 38 39 // ********************************* Methods ******************************** 40 41 // --- init ----------------------------------------------------------------- 42 43 // --- get&set -------------------------------------------------------------- 44 45 // --- business ------------------------------------------------------------- 46 47 /** 48 * Returns the names of properties this property depends on. May be 49 * <code>null</code>, if this property does not refer to other properties. 50 * 51 * @param value the value to parse, may be <code>null</code>. 52 * @return the names of properties this property depends on. 53 */ 54 List<String> getDependencies(final String value) 55 { 56 final List<String> dependencies = new ArrayList<String>(); 57 58 if (value != null) 59 { 60 final int length = value.length(); 61 for (int i = 0; i < length; i++) 62 { 63 final char c = value.charAt(i); 64 if (c == '$' && i < length - 1 && value.charAt(i + 1) == '{') 65 { 66 // CHECKSTYLE:OFF 67 i = readKey(dependencies, value, i + 1); 68 // CHECKSTYLE:ON 69 } 70 } 71 } 72 73 return dependencies; 74 } 75 76 private static int readKey(final List<String> dependencies, 77 final String value, final int index) 78 { 79 final int length = value.length() - 1; 80 final StringBuilder keyBuffer = new StringBuilder(64); 81 int i = index; 82 char n = value.charAt(++i); 83 while (n != '}' && i < length) 84 { 85 keyBuffer.append(n); 86 n = value.charAt(++i); 87 } 88 89 final String key = keyBuffer.toString(); 90 dependencies.add(key); 91 92 return i; 93 } 94 95 // --- object basics -------------------------------------------------------- 96 97 }