Author: nbelaevski
Date: 2010-02-11 15:51:58 -0500 (Thu, 11 Feb 2010)
New Revision: 16444
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/QNameComparator.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/ComparatorUtils.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/JavaUtils.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/util/
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/util/ComparatorUtilsTest.java
Removed:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/StringUtils.java
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstLiteralTreeNode.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstStringTreeNode.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkChooseElement.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkOtherwiseElement.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkWhenElement.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/ModelFragment.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java
root/cdk/trunk/plugins/maven-cdk-plugin/pom.xml
Log:
https://jira.jboss.org/jira/browse/RF-7732
Refactoring & optimizations
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/ELVisitor.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -21,6 +21,8 @@
package org.richfaces.cdk.parser.el;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedString;
+
import java.util.Map;
import org.jboss.el.parser.AstCompositeExpression;
@@ -103,7 +105,7 @@
if (ret != null && ret.jjtGetNumChildren() > 0) {
parsedExpression = this.visit(ret, contextMap);
} else {
- parsedExpression = StringUtils.getEscapedString("");
+ parsedExpression = getEscapedString("");
variableType = TypesFactory.getType(String.class);
}
}
Deleted:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/StringUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/StringUtils.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/StringUtils.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -1,144 +0,0 @@
-/**
- * License Agreement.
- *
- * Rich Faces - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.cdk.parser.el;
-
-import java.util.Locale;
-
-public final class StringUtils {
- private StringUtils() {
- }
-
- private static String toCharExpression(char c) {
- String prependingZeroesString;
- String hexString = Integer.toHexString(c);
-
- switch (hexString.length()) {
- case 1:
- prependingZeroesString = "000";
- break;
- case 2:
- prependingZeroesString = "00";
- break;
- case 3:
- prependingZeroesString = "0";
- break;
- case 4:
- prependingZeroesString = "";
- break;
- default:
- throw new IllegalArgumentException();
- }
-
- return "\\u" + prependingZeroesString +
hexString.toUpperCase(Locale.US);
- }
-
- public static String getEscapedString(String s) {
- StringBuilder result = new StringBuilder();
- result.append('"');
-
- char[] chars = s.toCharArray();
-
- for (char c : chars) {
- if (c == '\n') {
- result.append("\\n");
- } else if (c == '\r') {
- result.append("\\r");
- } else if (c == '\t') {
- result.append("\\t");
- } else if (c == '\f') {
- result.append("\\f");
- } else if (c == '\b') {
- result.append("\\b");
- } else if (c == '\\') {
- result.append("\\\\");
- } else if (c == '"') {
- result.append("\\\"");
- } else {
- if (c < 0x20 || c > 0x7F) {
- result.append(toCharExpression(c));
- } else {
- result.append(c);
- }
- }
- }
-
- result.append('"');
-
- return result.toString();
- }
-
- public static String getEscapedStringsArray(Iterable<String> strings) {
- StringBuilder sb = new StringBuilder();
-
- if (strings != null) {
- for (String string : strings) {
- if (sb.length() > 0) {
- sb.append(", ");
- }
-
- sb.append(getEscapedString(string));
- }
- }
-
- return sb.toString();
- }
-
- public static boolean isEmpty(String s) {
- return s == null || s.length() == 0;
- }
-
- /**
- * Returns true if the char isalpha() or isdigit().
- */
- public static boolean isalnum(char c) {
- return isalpha(c) || isdigit(c);
- }
-
- /**
- * Returns true if the char isupper() or islower().
- */
- public static boolean isalpha(char c) {
- return isupper(c) || islower(c);
- }
-
- /**
- * Returns true if the char is from 'A' to 'Z' inclusive.
- */
- public static boolean isupper(char c) {
- return (c >= 'A') && (c <= 'Z');
- }
-
- /**
- * Returns true if the char is from 'a' to 'z' inclusive.
- */
- public static boolean islower(char c) {
- return (c >= 'a') && (c <= 'z');
- }
-
- /**
- * Returns true if the char is from '0' to '9' inclusive.
- */
- public static boolean isdigit(char c) {
- return (c >= '0') && (c <= '9');
- }
-
-}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstLiteralTreeNode.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstLiteralTreeNode.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstLiteralTreeNode.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -28,7 +28,7 @@
import org.jboss.el.parser.Node;
import org.richfaces.cdk.parser.el.ELVisitor;
import org.richfaces.cdk.parser.el.ParsingException;
-import org.richfaces.cdk.parser.el.StringUtils;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedString;
import org.richfaces.cdk.parser.el.Type;
import org.richfaces.cdk.parser.el.types.TypesFactory;
@@ -46,7 +46,7 @@
@Override
public void visit(StringBuilder sb, Map<String, Type> context, ELVisitor
visitor) throws ParsingException {
if (getNode().getImage() != null) {
- sb.append(StringUtils.getEscapedString(getNode().getImage()));
+ sb.append(getEscapedString(getNode().getImage()));
visitor.setVariableType(TypesFactory.getType(String.class));
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstStringTreeNode.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstStringTreeNode.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/AstStringTreeNode.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -29,7 +29,7 @@
import org.jboss.el.parser.Node;
import org.richfaces.cdk.parser.el.ELVisitor;
import org.richfaces.cdk.parser.el.ParsingException;
-import org.richfaces.cdk.parser.el.StringUtils;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedString;
import org.richfaces.cdk.parser.el.Type;
import org.richfaces.cdk.parser.el.types.TypesFactory;
@@ -46,7 +46,7 @@
@Override
public void visit(StringBuilder sb, Map<String, Type> context, ELVisitor
visitor) throws ParsingException {
- sb.append(StringUtils.getEscapedString(((AstString) getNode()).getString()));
+ sb.append(getEscapedString(((AstString) getNode()).getString()));
visitor.setVariableType(TypesFactory.getType(String.class));
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/node/EqualityTestTreeNode.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -51,6 +51,26 @@
this.negateValue = negateValue;
}
+ private boolean isPrimitive(Type type) {
+ return type.getRawType().isPrimitive();
+ }
+
+ private boolean useIsEqualsMethod(Type firstType, Type secondType) {
+ if (firstType.isNullType() && !isPrimitive(secondType)) {
+ return false;
+ }
+
+ if (secondType.isNullType() && !isPrimitive(firstType)) {
+ return false;
+ }
+
+ if (isPrimitive(firstType) && isPrimitive(secondType)) {
+ return false;
+ }
+
+ return true;
+ }
+
/* (non-Javadoc)
* @see
org.richfaces.cdk.parser.el.node.AbstractTreeNode#visit(java.lang.StringBuilder,
java.util.Map, org.richfaces.cdk.parser.el.ELVisitor)
*/
@@ -61,8 +81,7 @@
String secondChildOutput = getChildOutput(1, context, visitor);
Type secondChildType = visitor.getVariableType();
- if (!firstChildType.getRawType().isPrimitive() ||
- !secondChildType.getRawType().isPrimitive()) {
+ if (useIsEqualsMethod(firstChildType, secondChildType)) {
if (negateValue) {
sb.append(ELNodeConstants.EXCLAMATION_MARK);
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/QNameComparator.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/QNameComparator.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/QNameComparator.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.templatecompiler;
+
+import static org.richfaces.cdk.util.ComparatorUtils.nullSafeCompare;
+
+import java.util.Comparator;
+
+import javax.xml.namespace.QName;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public final class QNameComparator implements Comparator<QName> {
+
+ public static final Comparator<QName> QNAME_COMPARATOR = new
QNameComparator();
+
+ private QNameComparator() {
+ //private constructor
+ }
+
+ @Override
+ public int compare(QName o1, QName o2) {
+ int result;
+
+ result = nullSafeCompare(o1.getNamespaceURI(), o2.getNamespaceURI());
+
+ if (result == 0) {
+ result = nullSafeCompare(o1.getLocalPart(), o2.getLocalPart());
+ }
+
+ return result;
+ }
+
+}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -23,6 +23,10 @@
package org.richfaces.cdk.templatecompiler;
+import static org.richfaces.cdk.templatecompiler.QNameComparator.QNAME_COMPARATOR;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedString;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedStringsArray;
+
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
@@ -59,7 +63,6 @@
import org.richfaces.cdk.parser.el.ELParserUtils;
import org.richfaces.cdk.parser.el.ELVisitor;
import org.richfaces.cdk.parser.el.ParsingException;
-import org.richfaces.cdk.parser.el.StringUtils;
import org.richfaces.cdk.parser.el.Type;
import org.richfaces.cdk.parser.el.types.TypesFactory;
import org.richfaces.cdk.templatecompiler.model.AnyElement;
@@ -185,16 +188,17 @@
abstract boolean isServiceMethodRequired(ELVisitor visitor);
void createMethod(JavaClass generatedClass) {
- JavaMethod conversionMethod = new JavaMethod(name, returnType, arguments);
+ JavaMethod serviceMethod = new JavaMethod(name, returnType, arguments);
- conversionMethod.addModifier(JavaModifier.PRIVATE);
- conversionMethod.addModifier(JavaModifier.FINAL);
+ serviceMethod.addModifier(JavaModifier.PRIVATE);
+ serviceMethod.addModifier(JavaModifier.STATIC);
+ serviceMethod.addModifier(JavaModifier.FINAL);
- MethodBody conversionMethodBody = new MethodBody(conversionMethod);
- conversionMethod.setMethodBody(conversionMethodBody);
- conversionMethodBody.addStatement(createMethodBodyStatement());
+ MethodBody serviceMethodBody = new MethodBody(serviceMethod);
+ serviceMethod.setMethodBody(serviceMethodBody);
+ serviceMethodBody.addStatement(createMethodBodyStatement());
- generatedClass.addMethod(conversionMethod);
+ generatedClass.addMethod(serviceMethod);
}
}
@@ -317,16 +321,16 @@
StringBuilder sb = new StringBuilder();
sb.append("new ComponentAttribute(");
- sb.append(StringUtils.getEscapedString(htmlAttributeName));
+ sb.append(getEscapedString(htmlAttributeName));
sb.append(")");
String attributeName;
- if (!StringUtils.isEmpty(componentAttributeName)) {
+ if (!Strings.isEmpty(componentAttributeName)) {
attributeName = componentAttributeName;
sb.append(".setComponentAttributeName(");
- sb.append(StringUtils.getEscapedString(componentAttributeName));
+ sb.append(getEscapedString(componentAttributeName));
sb.append(")");
} else {
attributeName = htmlAttributeName;
@@ -348,7 +352,7 @@
}
);
- sb.append(StringUtils.getEscapedStringsArray(eventNamesStrings));
+ sb.append(getEscapedStringsArray(eventNamesStrings));
sb.append("})");
}
@@ -517,7 +521,10 @@
boolean shouldEncodePassThrough = false;
String[] passThroughExclusions = null;
- for (Map.Entry<QName, Object> attribute : elementAttributes.entrySet())
{
+ Map<QName, Object> sortedElementAttributes = new TreeMap<QName,
Object>(QNAME_COMPARATOR);
+ sortedElementAttributes.putAll(elementAttributes);
+
+ for (Map.Entry<QName, Object> attribute :
sortedElementAttributes.entrySet()) {
QName attributeName = attribute.getKey();
Object attributeValue = attribute.getValue();
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkChooseElement.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkChooseElement.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkChooseElement.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -21,10 +21,6 @@
*/
package org.richfaces.cdk.templatecompiler.model;
-import java.util.List;
-
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import org.richfaces.cdk.CdkException;
@@ -36,20 +32,6 @@
@XmlRootElement(name = "choose", namespace = Template.JSTL_CORE_NAMESPACE)
public class CdkChooseElement extends ModelFragment {
- @XmlElements({
- @XmlElement(name = "when", namespace = Template.JSTL_CORE_NAMESPACE,
type = CdkWhenElement.class),
- @XmlElement(name = "otherwise", namespace =
Template.JSTL_CORE_NAMESPACE, type = CdkOtherwiseElement.class)
- })
- @Override
- public List<Object> getChildren() {
- return super.getChildren();
- }
-
- @Override
- public void setChildren(List<Object> body) {
- super.setChildren(body);
- }
-
/* (non-Javadoc)
* @see
org.richfaces.cdk.templatecompiler.model.ModelFragment#visit(org.richfaces.cdk.templatecompiler.model.TemplateVisitor)
*/
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkOtherwiseElement.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkOtherwiseElement.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkOtherwiseElement.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -21,12 +21,15 @@
*/
package org.richfaces.cdk.templatecompiler.model;
+import javax.xml.bind.annotation.XmlRootElement;
+
import org.richfaces.cdk.CdkException;
/**
* @author Nick Belaevski
* @since 4.0
*/
+@XmlRootElement(name = "otherwise", namespace = Template.JSTL_CORE_NAMESPACE)
public class CdkOtherwiseElement extends ModelFragment {
@Override
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkWhenElement.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkWhenElement.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/CdkWhenElement.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -21,12 +21,15 @@
*/
package org.richfaces.cdk.templatecompiler.model;
+import javax.xml.bind.annotation.XmlRootElement;
+
import org.richfaces.cdk.CdkException;
/**
* @author Nick Belaevski
* @since 4.0
*/
+@XmlRootElement(name = "when", namespace = Template.JSTL_CORE_NAMESPACE)
public class CdkWhenElement extends CdkConditionalJstlElementBase {
@Override
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/ModelFragment.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/ModelFragment.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/model/ModelFragment.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -20,6 +20,8 @@
CdkIfElement.class,
CdkObjectElement.class,
CdkChooseElement.class,
+ CdkWhenElement.class,
+ CdkOtherwiseElement.class,
CdkForEachElement.class
})
public class ModelFragment implements LeafModelElement {
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/ComparatorUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/ComparatorUtils.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/ComparatorUtils.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.util;
+
+/**
+ * Helper methods for comparable types
+ *
+ * @author Nick Belaevski
+ */
+public final class ComparatorUtils {
+
+ private ComparatorUtils() {
+ //private constructor
+ }
+
+ /**
+ * <p>Does null-safe comparison of mutually comparable objects.</p>
+ *
+ * <p>Obeys common rules of {@link Comparable#compareTo(Object)} methods if
both objects to compare
+ * are non-null.</p>
+ *
+ * <p>If one of the objects is <code>null</code>, but the other is
not, then <code>null</code> object
+ * is considered to be less than non-null.</p>
+ *
+ * <p>Two <code>null</code> objects are considered as
equal.</p>
+ *
+ * @param <T> the type of objects that this object may be compared to
+ * @param t1 the first object
+ * @param t2 the second object
+ * @return
+ */
+ public static <T extends Comparable<T>> int nullSafeCompare(T t1, T t2)
{
+ if (t1 == null) {
+ return t2 == null ? 0 : -1;
+ } else if (t2 == null) {
+ return 1;
+ } else {
+ return t1.compareTo(t2);
+ }
+ }
+
+}
Added:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/JavaUtils.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/JavaUtils.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/JavaUtils.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -0,0 +1,169 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.util;
+
+import java.util.Locale;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public final class JavaUtils {
+
+ private JavaUtils() {
+ // private constructor
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Transform character to Java code expression
+ * </p>
+ *
+ * @param c
+ * @return
+ */
+ private static String toCharExpression(char c) {
+ String prependingZeroesString;
+ String hexString = Integer.toHexString(c);
+
+ switch (hexString.length()) {
+ case 1:
+ prependingZeroesString = "000";
+ break;
+ case 2:
+ prependingZeroesString = "00";
+ break;
+ case 3:
+ prependingZeroesString = "0";
+ break;
+ case 4:
+ prependingZeroesString = "";
+ break;
+ default:
+ throw new IllegalArgumentException();
+ }
+
+ return "\\u" + prependingZeroesString +
hexString.toUpperCase(Locale.US);
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Escapes string into Java language expression
+ * </p>
+ *
+ * @param s
+ * @return
+ */
+ public static String getEscapedString(String s) {
+ StringBuilder result = new StringBuilder();
+ result.append('"');
+
+ char[] chars = s.toCharArray();
+
+ for (char c : chars) {
+ if (c == '\n') {
+ result.append("\\n");
+ } else if (c == '\r') {
+ result.append("\\r");
+ } else if (c == '\t') {
+ result.append("\\t");
+ } else if (c == '\f') {
+ result.append("\\f");
+ } else if (c == '\b') {
+ result.append("\\b");
+ } else if (c == '\\') {
+ result.append("\\\\");
+ } else if (c == '"') {
+ result.append("\\\"");
+ } else {
+ if (c < 0x20 || c > 0x7F) {
+ result.append(toCharExpression(c));
+ } else {
+ result.append(c);
+ }
+ }
+ }
+
+ result.append('"');
+
+ return result.toString();
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Escapes sequence of strings into comma-separated sequence of Java language
expressions
+ * </p>
+ *
+ * @param strings
+ * @return
+ */
+ public static String getEscapedStringsArray(Iterable<String> strings) {
+ StringBuilder sb = new StringBuilder();
+
+ if (strings != null) {
+ for (String string : strings) {
+ if (sb.length() > 0) {
+ sb.append(", ");
+ }
+
+ sb.append(getEscapedString(string));
+ }
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Returns true if the char isalpha() or isdigit().
+ */
+ public static boolean isalnum(char c) {
+ return isalpha(c) || isdigit(c);
+ }
+
+ /**
+ * Returns true if the char isupper() or islower().
+ */
+ public static boolean isalpha(char c) {
+ return isupper(c) || islower(c);
+ }
+
+ /**
+ * Returns true if the char is from 'A' to 'Z' inclusive.
+ */
+ public static boolean isupper(char c) {
+ return (c >= 'A') && (c <= 'Z');
+ }
+
+ /**
+ * Returns true if the char is from 'a' to 'z' inclusive.
+ */
+ public static boolean islower(char c) {
+ return (c >= 'a') && (c <= 'z');
+ }
+
+ /**
+ * Returns true if the char is from '0' to '9' inclusive.
+ */
+ public static boolean isdigit(char c) {
+ return (c >= '0') && (c <= '9');
+ }
+}
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java 2010-02-11
17:35:12 UTC (rev 16443)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/parser/el/test/ELParserTest.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -21,7 +21,10 @@
package org.richfaces.cdk.parser.el.test;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.List;
@@ -100,11 +103,23 @@
parseExpression("#{1 ne 3}");
assertEquals("(1 != 3)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
parseExpression("#{2 != 3}");
assertEquals("(2 != 3)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
+ parseExpression("#{action != 2}");
+ assertEquals("!isEqual(action,2)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{2 ne action}");
+ assertEquals("!isEqual(2,action)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
parseExpression("#{action != clientId}");
assertEquals("!isEqual(action,clientId)",
visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
@@ -114,6 +129,16 @@
assertEquals("!isEqual(action,clientId)",
visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{action ne null}");
+ assertEquals("(action != null)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
+
+ parseExpression("#{2 != null}");
+ assertEquals("!isEqual(2,null)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
}
@Test
@@ -190,11 +215,23 @@
parseExpression("#{1 eq 2}");
assertEquals("(1 == 2)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
parseExpression("#{3 == 2}");
assertEquals("(3 == 2)", visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
+ parseExpression("#{action == 2}");
+ assertEquals("isEqual(action,2)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{2 eq action}");
+ assertEquals("isEqual(2,action)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
+
parseExpression("#{action == clientId}");
assertEquals("isEqual(action,clientId)",
visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
@@ -204,6 +241,16 @@
assertEquals("isEqual(action,clientId)",
visitor.getParsedExpression());
assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
assertTrue(visitor.isUseEqualsCheck());
+
+ parseExpression("#{action eq null}");
+ assertEquals("(action == null)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertFalse(visitor.isUseEqualsCheck());
+
+ parseExpression("#{2 == null}");
+ assertEquals("isEqual(2,null)", visitor.getParsedExpression());
+ assertEquals(Boolean.TYPE, visitor.getVariableType().getRawType());
+ assertTrue(visitor.isUseEqualsCheck());
}
@Test
Added:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/util/ComparatorUtilsTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/util/ComparatorUtilsTest.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/util/ComparatorUtilsTest.java 2010-02-11
20:51:58 UTC (rev 16444)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.cdk.util;
+
+import static org.junit.Assert.*;
+import static org.richfaces.cdk.util.ComparatorUtils.*;
+
+import org.junit.Test;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ComparatorUtilsTest {
+
+ @Test
+ public void testNullSafeCompare() throws Exception {
+ assertTrue(nullSafeCompare("a", "b") < 0);
+ assertTrue(nullSafeCompare("b", "a") > 0);
+ assertTrue(nullSafeCompare("a", "a") == 0);
+
+ assertTrue(nullSafeCompare("", "a") < 0);
+ assertTrue(nullSafeCompare("a", "") > 0);
+ assertTrue(nullSafeCompare("", "") == 0);
+
+ assertTrue(nullSafeCompare(null, "a") < 0);
+ assertTrue(nullSafeCompare("a", null) > 0);
+ assertTrue(nullSafeCompare((String) null, null) == 0);
+
+ assertTrue(nullSafeCompare(null, "") < 0);
+ assertTrue(nullSafeCompare("", null) > 0);
+ }
+}
Modified: root/cdk/trunk/plugins/maven-cdk-plugin/pom.xml
===================================================================
--- root/cdk/trunk/plugins/maven-cdk-plugin/pom.xml 2010-02-11 17:35:12 UTC (rev 16443)
+++ root/cdk/trunk/plugins/maven-cdk-plugin/pom.xml 2010-02-11 20:51:58 UTC (rev 16444)
@@ -115,6 +115,7 @@
<settingsFile>src/it/settings.xml</settingsFile>
<goals>
<goal>compile</goal>
+ <goal>test</goal>
</goals>
</configuration>
<executions>