[jboss-cvs] JBossAS SVN: r88016 - in projects/fresh/trunk: fresh-util/src/main/java/org/jboss/fresh/util and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Apr 29 12:35:42 EDT 2009
Author: ctomc
Date: 2009-04-29 12:35:42 -0400 (Wed, 29 Apr 2009)
New Revision: 88016
Added:
projects/fresh/trunk/fresh-util/src/main/java/org/jboss/fresh/util/TextLayout.java
Modified:
projects/fresh/trunk/pom.xml
Log:
deployers
Added: projects/fresh/trunk/fresh-util/src/main/java/org/jboss/fresh/util/TextLayout.java
===================================================================
--- projects/fresh/trunk/fresh-util/src/main/java/org/jboss/fresh/util/TextLayout.java (rev 0)
+++ projects/fresh/trunk/fresh-util/src/main/java/org/jboss/fresh/util/TextLayout.java 2009-04-29 16:35:42 UTC (rev 88016)
@@ -0,0 +1,239 @@
+package org.jboss.fresh.util;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+public class TextLayout {
+
+ private LinkedList spec;
+ private Set allowed;
+ private String beginPat = "${";
+ private String endPat = "}";
+
+ public TextLayout() {
+ }
+
+ public TextLayout(String begin, String end) {
+ beginPat = begin;
+ endPat = end;
+ }
+
+ public TextLayout(String[] val) {
+ allowed = new HashSet();
+ for (int i = 0; i < val.length; i++) {
+ allowed.add(val[i]);
+ }
+ }
+
+ public TextLayout(Set val) {
+ allowed = val;
+ }
+
+ public void setPattern(String pat) {
+//System.out.println("setPattern " + pat);
+ try {
+
+ spec = new LinkedList();
+
+ // parse pattern
+ // look for ${
+ int pos = pat.indexOf(beginPat);
+ if (pos == -1) spec.add(new LayData(pat));
+
+ int start = 0;
+
+ while (pos != -1) {
+ spec.add(new LayData(pat.substring(start, pos)));
+
+ // when you find it find }
+ start = pos + beginPat.length();
+ pos = pat.indexOf(endPat, start);
+ if (pos == -1) throw new RuntimeException("Syntax error: unclosed variable reference at pos " + start + " : " + pat);
+
+ String type = null, format = null;
+ String token = pat.substring(start, pos);
+
+ // now see if ] is the last char if so, find [ from end to start
+ if (token.length() == 0) throw new RuntimeException("Syntax error: empty variable reference at pos " + start + " : " + pat);
+
+ if (token.endsWith("]")) {
+ int p2 = token.lastIndexOf("[", token.length() - 1);
+ if (p2 == -1) throw new RuntimeException("Syntax error: format specifier without beginning at pos " + (start + token.length()) + " : " + pat);
+
+ format = token.substring(p2 + 1, token.length() - 1);
+
+ token = token.substring(0, p2);
+ }
+
+ // find : starting from beginning if you find it we have a type specification
+ int p3 = token.indexOf(":");
+ if (p3 != -1) {
+ type = token.substring(0, p3);
+ token = token.substring(p3 + 1);
+ }
+ //System.out.println("lay-data (" + token + " " + type + " " + format + ")");
+ if (allowed != null && !allowed.contains(token)) throw new RuntimeException("Pattern contains illegal variable reference: " + token);
+
+//System.out.println("LayData: " + token + ", " + type + ", " + format);
+ spec.add(new LayData(token, type, format));
+
+ start = pos + endPat.length();
+ pos = pat.indexOf(beginPat, start);
+
+ if (pos == -1) spec.add(new LayData(pat.substring(start)));
+ }
+
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException(ex);
+ }
+
+ // prepare array that is analog to prepared statement
+ }
+
+
+ public Iterator specIterator() {
+ return spec.iterator();
+ }
+
+
+ public void bind(Writer w, Map values) throws IOException {
+
+ Iterator it = spec.iterator();
+ while (it.hasNext()) {
+ LayData dat = (LayData) it.next();
+
+ if (dat.isStatic()) {
+//System.out.println("+ (V) " + dat.getValue());
+ w.write(String.valueOf(dat.getValue()).toCharArray());
+ } else {
+
+ String val = String.valueOf(values.get(dat.field));
+ if (val != null) {
+//System.out.println("+ (F:" + val + ") " + dat.format(val));
+ w.write(String.valueOf(dat.format(val)).toCharArray());
+ } else {
+//System.out.println("+ NULL NULL NULL NULL");
+ }
+ }
+ }
+
+ w.flush();
+ }
+
+ public static class LayData {
+
+ public String field;
+ public String type;
+ public String format;
+ public String value;
+ private boolean statik = false;
+
+ private SimpleDateFormat sdf;
+
+ LayData(String val) {
+ value = val;
+ statik = true;
+ }
+
+ LayData(String fld, String typ, String form) throws Exception {
+
+ field = fld.intern();
+ type = (typ == null ? null : typ.intern());
+ format = form;
+
+ if (format != null) {
+ if (type != null && type.equals("date")) {
+ sdf = new SimpleDateFormat(format);
+ }
+ }
+ }
+
+ public boolean isStatic() {
+ return statik;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public String format(String val) {
+ return val;
+ }
+
+ // check for number format
+ public String format(int val) {
+ return String.valueOf(val);
+ }
+
+ // check for number format
+ public String format(float val) {
+ return String.valueOf(val);
+ }
+
+ public String format(long val) {
+
+ // check for date format
+ if (type == "date" && sdf != null) {
+ // apply
+ return sdf.format(new Date(val));
+ }
+
+ return String.valueOf(val);
+ }
+
+ public String format(Date val) {
+
+ if (val == null) return "null";
+
+ // check for date format
+ if (type == "date" && sdf != null) {
+ // apply
+ return sdf.format(val);
+ }
+
+ return String.valueOf(val);
+ }
+
+ public String format(StackTraceElement[] val) {
+
+ // tukaj bi lahko imeli razno razne formate
+ // od tega da se dolocene reci izvzame do
+ // tega da format vrstice specificiras
+ // Default je obicajen format
+ if (val == null) return "null";
+
+ int max = Integer.MAX_VALUE;
+
+ if (format != null) {
+ try {
+ max = Integer.parseInt(format);
+ } catch (NumberFormatException ex) {
+ }
+ }
+
+ StringBuffer sb = new StringBuffer("\n");
+
+ for (int i = 0; i < val.length && i < max; i++) {
+ sb.append("\tat ").append(val[i].getClassName()).append(".").append(val[i].getMethodName()).append("(").append(val[i].getFileName()).append(":").append(val[i].getLineNumber()).append(")\n");
+ }
+
+ return sb.toString();
+ }
+
+ public String format(Object val) {
+ return String.valueOf(val);
+ }
+
+ }
+ /**
+ * @param set
+ */
+ public void setAllowed(Set set) {
+ allowed = set;
+ }
+
+}
\ No newline at end of file
Modified: projects/fresh/trunk/pom.xml
===================================================================
--- projects/fresh/trunk/pom.xml 2009-04-29 16:35:08 UTC (rev 88015)
+++ projects/fresh/trunk/pom.xml 2009-04-29 16:35:42 UTC (rev 88016)
@@ -11,7 +11,7 @@
<packaging>pom</packaging>
<name>JBoss Fresh Parent POM</name>
<url>http://www.jboss.com/products/fresh</url>
- <description>JBoss Spring integration</description>
+ <description>JBoss Free shell</description>
<scm>
<connection>scm:svn:http://anonsvn.jboss.org/repos/jbossas/projects/spring-int/trunk</connection>
<developerConnection>scm:svn:https://svn.jboss.org/repos/jbossas/projects/spring-int/trunk</developerConnection>
More information about the jboss-cvs-commits
mailing list