001: /*
002: * License
003: *
004: *
005: * Copyright (c) 2003 Essl Christian. All rights
006: * reserved.
007: *
008: * This Licence is based on the Apache Software Licence Version 1.1.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by Christian Essl
025: * and others for project Jucas (http://www.jucas.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Jucas" and "Christian Essl" must
030: * not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact essl_christian@jucas.org.
033: *
034: * 5. Products derived from this software may not be called "Jucas"
035: * or "Christian Essl", nor may "Jucas" or "Christian Essl"
036: * appear in their name, without prior written permission
037: * of Christian Essl (jucas@esslchristian.de).
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL CHRISTIAN ESSL OR
043: * OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE.
051: * ====================================================================
052: *
053: *
054: */
055: package org.jucas.xml;
056:
057: import java.lang.reflect.InvocationTargetException;
058: import java.util.Set;
059:
060: import org.apache.commons.beanutils.BeanUtils;
061:
062: import org.jucas.IJucasBean;
063: import org.jucas.Scopes;
064:
065: import org.jaul.JaulAttributes;
066: import org.jaul.JaulContext;
067: import org.jaul.TagScript;
068:
069: /**
070: * @author chris
071: *
072: * To change this generated comment edit the template variable "typecomment":
073: * Window>Preferences>Java>Templates.
074: * To enable and disable the creation of type comments go to
075: * Window>Preferences>Java>Code Generation.
076: */
077: final public class JaulUtils {
078:
079: /**
080: * Constructor for JaulUtils.
081: */
082: private JaulUtils() {
083: super ();
084: }
085:
086: public static int getScope(String string) {
087: if (string == null)
088: return Scopes.DEFAULT;
089: if ("request".equals(string))
090: return Scopes.REQUEST;
091: else if ("session".equals(string))
092: return Scopes.SESSION;
093: else
094: return Scopes.DEFAULT;
095: }
096:
097: public static int getWantedScope(String string) {
098: if (string == null)
099: return IJucasBean.NO_REQUESTED_SCOPE;
100: if ("request".equals(string))
101: return Scopes.REQUEST;
102: else if ("session".equals(string))
103: return Scopes.SESSION;
104: else
105: return IJucasBean.NO_REQUESTED_SCOPE;
106: }
107:
108: public static void setAttributes(Object bean, TagScript script,
109: JaulContext ctxt, Set exclude)
110: throws IllegalAccessException, InvocationTargetException {
111: JaulAttributes atts = script.getAttributes();
112: for (int i = 0; i < atts.getLength(); i++) {
113: String name = atts.getLocalName(i);
114: if (exclude != null && exclude.contains(name))
115: continue;
116: Object value = atts.getExpression(i).evaluate(ctxt);
117: BeanUtils.setProperty(bean, name, value);
118: }
119: }
120:
121: //////////////////////////
122: //Script things
123: public static boolean isScriptEventAttribute(String attrName) {
124: if (attrName.length() > 11
125: && attrName.startsWith("add")
126: && (attrName.endsWith("Listener") || attrName.indexOf(
127: "Listener-", 4) != -1)) {
128: return true;
129:
130: } else {
131: return false;
132: }
133:
134: }
135:
136: static private String escapeAmper(String in) {
137: if (in.indexOf('\'') == -1)
138: return in;
139: StringBuffer stB = new StringBuffer(in.length());
140: char[] cA = in.toCharArray();
141: int length = cA.length;
142: int i = 0;
143: while (i < length) {
144: char c = cA[i];
145: if (c == '\'') {
146: int nextI = i + 1;
147: if (nextI < length && cA[nextI] == '\'') {
148: stB.append('\'');
149: i = nextI + 1;
150: continue;
151: } else {
152: stB.append('"');
153: }
154: } else {
155: stB.append(c);
156: }
157: i++;
158: }
159:
160: return stB.toString();
161: }
162:
163: public static void main(String[] arg) {
164: System.out.println(escapeAmper("'normal string'"));
165: System.out.println(escapeAmper("\\'\\'\\'"));
166: System.out.println(escapeAmper("\\'"));
167: System.out.println(escapeAmper("\\\'"));
168: System.out.println(escapeAmper("\\\\'\\\\\'"));
169: }
170:
171: }
|