001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/model/wizards/PropertyUpdaterHelper.java,v 1.1.1.1 2004/03/25 12:08:40 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:40 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.model.wizards;
042:
043: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
044:
045: /**
046: * Helper class for propery updaters. It provides methods primarily related to
047: * the <tt>propertyStructureName</tt> parameter of the wizard events.
048: *
049: * @author Daniel Fernandez
050: * @since 1.0
051: */
052: public class PropertyUpdaterHelper {
053:
054: /**
055: * Avoids the instantiation of this class.
056: */
057: private PropertyUpdaterHelper() {
058: }
059:
060: /**
061: * This method checks that a property structure name is correctly formed.
062: * This is, that is in the form <tt>0</tt>, <tt>0.a.1</tt>,
063: * <tt>0.a.1.b.2</tt>... designating correctly the position of a
064: * property structure in the Property tree.
065: *
066: * @param structureName the property structure name string to check.
067: * @return true if the name is well formed, false if not.
068: */
069: static boolean isStructureNameWellFormed(String structureName) {
070:
071: int strLength = structureName.length();
072: if (strLength > 0) {
073:
074: try {
075:
076: /*
077: * Check if it is in the form "0", "1"...
078: */
079: Integer.parseInt(structureName);
080: return true;
081:
082: } catch (NumberFormatException e) {
083:
084: /*
085: * This means the name is not just a number, so, we'll extract
086: * its components, which must be:
087: *
088: * 1. A number of value.
089: * 2. The name of a property.
090: * 3. A string conforming another property structure name,
091: * which can be checked recursively.
092: */
093: int firstDot = structureName.indexOf(".");
094: if ((firstDot < 0) || (firstDot >= (strLength - 1))) {
095: return false;
096: }
097: int secondDot = structureName
098: .indexOf(".", firstDot + 1);
099: if ((secondDot < 0) || (secondDot >= (strLength - 1))) {
100: return false;
101: }
102:
103: try {
104:
105: Integer.parseInt(structureName.substring(0,
106: firstDot));
107:
108: String propName = structureName.substring(
109: firstDot + 1, secondDot);
110: if (propName.indexOf(" ") >= 0) {
111: return false;
112: }
113:
114: String restOfStructureName = structureName
115: .substring(secondDot + 1);
116:
117: /*
118: * We make a recursive call with the rest of the name.
119: */
120: return isStructureNameWellFormed(restOfStructureName);
121:
122: } catch (NumberFormatException exc) {
123: return false;
124: }
125:
126: }
127:
128: } else {
129:
130: return false;
131:
132: }
133:
134: }
135:
136: /**
137: * Extracts the property structure number from a given property structure
138: * name. This is the number which identifies the number of value appliable
139: * to the property tree so as to find the adequate property. For example,
140: * if the property structure name were <tt>0.a.1</tt>, this method would
141: * give as a result <tt>0</tt>.
142: *
143: * @param structureName the property structure name.
144: * @throws InternalErrorException if any fatal errors occur.
145: * @return an int being the number of value in the property.
146: */
147: static int getPropertyStructureNumber(String structureName)
148: throws InternalErrorException {
149:
150: if (isStructureNameWellFormed(structureName)) {
151:
152: try {
153:
154: /*
155: * Check if the name is only a number. If it is, it's already
156: * what we need.
157: */
158: int number = Integer.parseInt(structureName);
159: return number;
160:
161: } catch (NumberFormatException e) {
162:
163: /*
164: * The name is longar than just a number, so we must extract
165: * the first number, the ciphers before the first dot.
166: */
167: int lastDot = structureName.lastIndexOf(".");
168: String numberStr = structureName.substring(lastDot + 1);
169:
170: try {
171: int number = Integer.parseInt(numberStr);
172: return number;
173: } catch (NumberFormatException exc) {
174: // As the string is well formed, this is impossible
175: throw new InternalErrorException(e);
176: }
177:
178: }
179:
180: } else {
181: throw new InternalErrorException(
182: "Structure name badly formed: " + structureName);
183: }
184:
185: }
186:
187: /**
188: * This number takes the property name from a property structure name. This
189: * is, removes the last number which selects a value inside a property
190: * (selects a property structure). So, from <tt>0.a.1</tt> would return
191: * <tt>0.a</tt>.<p>
192: *
193: * If the property structure name is only a number, for example <tt>0</tt>,
194: * it returns an emtpy string.
195: *
196: * @param structureName the property structure name
197: * @throws InternalErrorException if any fatal error occurs.
198: * @return a String being the property selector from the property structure
199: * name.
200: */
201: static String getPropertyStructurePropertyName(String structureName)
202: throws InternalErrorException {
203:
204: if (isStructureNameWellFormed(structureName)) {
205:
206: int lastDot = structureName.lastIndexOf(".");
207:
208: if (lastDot >= 0) {
209:
210: /* We take everything but the last number */
211: return structureName.substring(0, lastDot);
212:
213: } else {
214:
215: /*
216: * The property structure name was simply a number,
217: * and so we return an empty string.
218: */
219: return "";
220:
221: }
222:
223: } else {
224: throw new InternalErrorException(
225: "Structure name badly formed: " + structureName);
226: }
227:
228: }
229:
230: }
|