001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.sun.validation.impl;
043:
044: import java.lang.reflect.Method;
045: import java.text.MessageFormat;
046: import java.util.ArrayList;
047: import java.util.Collection;
048:
049: import org.netbeans.modules.schema2beans.BaseBean;
050: import org.netbeans.modules.schema2beans.BaseProperty;
051:
052: import org.netbeans.modules.j2ee.sun.validation.Constants;
053: import org.netbeans.modules.j2ee.sun.validation.util.BundleReader;
054: import org.netbeans.modules.j2ee.sun.validation.util.Utils;
055: import org.netbeans.modules.j2ee.sun.validation.Validatee;
056:
057: /**
058: *
059: * @author Rajeshwar Patil
060: * @version %I%, %G%
061: */
062:
063: public class ValidateeImpl implements Validatee {
064: /* A class implementation comment can go here. */
065: private BaseBean baseBean = null;
066:
067: private Utils utils = null;
068:
069: /** Creates a new instance of ValidateeImpl */
070: public ValidateeImpl(Object baseBean) {
071: this .baseBean = (BaseBean) baseBean;
072: utils = new Utils();
073: }
074:
075: public ArrayList getElementNames() {
076: ArrayList elements = new ArrayList();
077: BaseProperty[] baseProperty = baseBean.listProperties();
078: int size = baseProperty.length;
079: for (int i = 0; i < size; i++) {
080: elements.add(baseProperty[i].getName());
081: ///String format =
082: /// BundleReader.getValue("Name_Value_Pair_Format"); //NOI18N
083: ///Object[] arguments = new Object[]{"Name", //NOI18N
084: /// baseProperty[i].getName()};
085: ///System.out.println(MessageFormat.format(format, arguments));
086: ///arguments =
087: /// new Object[]{"FullName", baseProperty[i].getFullName()};//NOI18N
088: ///System.out.println(MessageFormat.format(format, arguments));
089: }
090: return elements;
091: }
092:
093: public ArrayList getElementDtdNames() {
094: ArrayList elements = new ArrayList();
095: BaseProperty[] baseProperty = baseBean.listProperties();
096: int size = baseProperty.length;
097: for (int i = 0; i < size; i++) {
098: elements.add(baseProperty[i].getDtdName());
099: ///String format =
100: /// BundleReader.getValue("Name_Value_Pair_Format"); //NOI18N
101: ///Object[] arguments = new Object[]{"Dtd Name", //NOI18N
102: /// baseProperty[i].getDtdName()};
103: ///System.out.println(MessageFormat.format(format, arguments));
104: }
105: return elements;
106: }
107:
108: public boolean isIndexed(String elementName) {
109: BaseProperty baseProperty = baseBean.getProperty(elementName);
110: boolean returnValue = false;
111: if (null != baseProperty) {
112: returnValue = baseProperty.isIndexed();
113: } else {
114: String format = BundleReader
115: .getValue("Error_does_not_exists"); //NOI18N
116: Object[] arguments = new Object[] { "Property", elementName }; //NOI18N
117: String message = MessageFormat.format(format, arguments);
118: assert false : message;
119: }
120: return returnValue;
121: }
122:
123: public int getElementCardinal(String elementName) {
124: BaseProperty baseProperty = baseBean.getProperty(elementName);
125: int returnValue = -1;
126: if (null != baseProperty) {
127: returnValue = baseProperty.getInstanceType();
128: } else {
129: String format = BundleReader
130: .getValue("Error_does_not_exists"); //NOI18N
131: Object[] arguments = new Object[] { "Property", elementName }; //NOI18N
132: String message = MessageFormat.format(format, arguments);
133: assert false : message;
134: }
135: return returnValue;
136: }
137:
138: public int getCardinal() {
139: String name = baseBean.name();
140: BaseBean parent = baseBean.parent();
141: BaseProperty baseProperty = parent.getProperty(name);
142: return baseProperty.getInstanceType();
143: }
144:
145: public boolean isBeanElement(String elementName) {
146: BaseProperty baseProperty = baseBean.getProperty(elementName);
147: boolean returnValue = false;
148: if (null != baseProperty) {
149: returnValue = baseProperty.isBean();
150: } else {
151: String format = BundleReader
152: .getValue("Error_does_not_exists"); //NOI18N
153: Object[] arguments = new Object[] { "Property", elementName }; //NOI18N
154: String message = MessageFormat.format(format, arguments);
155: assert false : message;
156: }
157: return returnValue;
158: }
159:
160: public String getXPath() {
161: ///String format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
162: ///Object[] arguments = new Object[]{"Name", baseBean.name()}; //NOI18N
163: ///System.out.println(MessageFormat.format(format, arguments));
164: ///arguments = new Object[]{"FullName", baseBean.fullName()}; //NOI18N
165: ///System.out.println(MessageFormat.format(format, arguments));
166:
167: BaseBean bean = baseBean;
168: BaseBean parentBean = null;
169: String xpath = bean.dtdName();
170: ///boolean root = bean.isRoot();
171: boolean root = isRootElement(bean);
172: parentBean = bean.parent();
173: while (!root) {
174: xpath = parentBean.dtdName() + Constants.XPATH_DELIMITER
175: + xpath;
176: bean = parentBean;
177: parentBean = bean.parent();
178: ///root = bean.isRoot();
179: root = isRootElement(bean);
180: }
181: xpath = Constants.XPATH_DELIMITER + xpath;
182: return xpath;
183: }
184:
185: public String getIndexedXPath() {
186: BaseBean bean = baseBean;
187: BaseBean parentBean = null;
188: String xpath = bean.dtdName();
189: int index = getIndex(baseBean);
190: if (index != -1) {
191: xpath = utils.getIndexedName(xpath, index);
192: }
193:
194: boolean root = isRootElement(bean);
195: parentBean = bean.parent();
196:
197: String name = null;
198: while (!root) {
199: name = parentBean.dtdName();
200: index = getIndex(parentBean);
201: if (index != -1) {
202: name = utils.getIndexedName(name, index);
203: }
204: xpath = name + Constants.XPATH_DELIMITER + xpath;
205: bean = parentBean;
206: parentBean = bean.parent();
207: root = isRootElement(bean);
208: }
209: xpath = Constants.XPATH_DELIMITER + xpath;
210: return xpath;
211: }
212:
213: boolean isRootElement(BaseBean bean) {
214: BaseBean parent = bean.parent();
215: boolean root;
216: if (parent.name().equals(bean.name())) {
217: root = true;
218: } else {
219: root = false;
220: }
221: return root;
222: }
223:
224: int getIndex(BaseBean baseBean) {
225: int index = -1;
226: boolean root = isRootElement(baseBean);
227: if (!root) {
228: String name = baseBean.name();
229: BaseBean parent = baseBean.parent();
230: if (parent != null) {
231: index = parent.indexOf(name, baseBean);
232: }
233: }
234: return index;
235: }
236:
237: public Object getElement(String elementName, int index) {
238: return utils.getElement(elementName, index, baseBean);
239: }
240:
241: public Object getElement(String elementName) {
242: return utils.getElement(elementName, baseBean);
243: }
244:
245: public Object[] getElements(String elementName) {
246: return utils.getElements(elementName, baseBean);
247: }
248:
249: public Method getMethod(String methodName) {
250: return utils.getMethod(utils.getClass(baseBean), methodName);
251: }
252:
253: public Object invoke(Method method) {
254: return utils.invoke(baseBean, method);
255: }
256: }
|