1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF 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.apache.maven.model.interpolation; 20 21 /* 22 * Copyright 2007 The Codehaus Foundation. 23 * 24 * Licensed under the Apache License, Version 2.0 (the "License"); 25 * you may not use this file except in compliance with the License. 26 * You may obtain a copy of the License at 27 * 28 * http://www.apache.org/licenses/LICENSE-2.0 29 * 30 * Unless required by applicable law or agreed to in writing, software 31 * distributed under the License is distributed on an "AS IS" BASIS, 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 * See the License for the specific language governing permissions and 34 * limitations under the License. 35 */ 36 37 import org.apache.maven.model.interpolation.reflection.ReflectionValueExtractor; 38 import org.codehaus.plexus.interpolation.AbstractValueSource; 39 40 /** 41 * Wraps an object, providing reflective access to the object graph of which the 42 * supplied object is the root. Expressions like 'child.name' will translate into 43 * 'rootObject.getChild().getName()' for non-boolean properties, and 44 * 'rootObject.getChild().isName()' for boolean properties. 45 */ 46 public class ObjectBasedValueSource extends AbstractValueSource { 47 48 private final Object root; 49 50 /** 51 * Construct a new value source, using the supplied object as the root from 52 * which to start, and using expressions split at the dot ('.') to navigate 53 * the object graph beneath this root. 54 * @param root the root of the graph. 55 */ 56 public ObjectBasedValueSource(Object root) { 57 super(true); 58 this.root = root; 59 } 60 61 /** 62 * <p>Split the expression into parts, tokenized on the dot ('.') character. Then, 63 * starting at the root object contained in this value source, apply each part 64 * to the object graph below this root, using either 'getXXX()' or 'isXXX()' 65 * accessor types to resolve the value for each successive expression part. 66 * Finally, return the result of the last expression part's resolution.</p> 67 * 68 * <p><b>NOTE:</b> The object-graph nagivation actually takes place via the 69 * {@link ReflectionValueExtractor} class.</p> 70 */ 71 public Object getValue(String expression) { 72 if (expression == null || expression.trim().isEmpty()) { 73 return null; 74 } 75 76 try { 77 return ReflectionValueExtractor.evaluate(expression, root, false); 78 } catch (Exception e) { 79 addFeedback("Failed to extract \'" + expression + "\' from: " + root, e); 80 } 81 82 return null; 83 } 84 }