Java tutorial
//package com.java2s; /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** The symbol used by XPath to query properties/attributes from a node. */ private static final String xpathPropertySymbol = "@"; /** * Prepends the at (@) symbol to the property name. This is XPath-specific. * <p> * Before you start to hate me, please be aware that I created this method * because of the way XPath can be used to query the JCR. For example, if you * have a query in which you want to evaluate a property of a child node, say * "jcr:content/@jcr:title," prepending the @ symbol to the property name * would only cause problems, i.e. "@jcr:content/@jcr:title." The issue being * that jcr:content is a child node, not a property, thus the query fails * miserably. <b>STOP DOING THAT AND I MAY CONSIDER REMOVING THIS METHOD</b> * <p> * As a measure to avoid the auto-symbol-prepending I'm going to check if the * symbol exists in the property name and prepend it if and only if it doesn't * exist. Thus, "published" would output "@published" and * "jcr:content/@jcr:title" would remain unaltered. * <p> * As a side-effect, if a property starts with the at (@) symbol, it is not * prepended again, but this wouldn't be the right way of using the API, i. e. * never prepend the at (@) symbol because it's XPath-specific. * * @param propertyName * The name of the property that @ will be prepended to * @return A new string representing a valid XPath property */ public static String prependSymbol(final String propertyName) { if (propertyName.contains(xpathPropertySymbol)) { return propertyName; } else { // Performance problems ensue return xpathPropertySymbol + propertyName; } } }