From: Tim P <ti...@us...> - 2008-03-20 22:31:01
|
Update of /cvsroot/webmacro/webmacro/src/org/webmacro/directive In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv11132/src/org/webmacro/directive Modified Files: SetpropsDirective.java Log Message: Checkstyle: avoid nested blocks, eclipse format Index: SetpropsDirective.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/directive/SetpropsDirective.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** SetpropsDirective.java 20 Mar 2008 11:47:50 -0000 1.4 --- SetpropsDirective.java 20 Mar 2008 22:30:55 -0000 1.5 *************** *** 47,221 **** { ! private static final String DEFAULT_CLASS_NAME = "java.util.Hashtable"; ! private static final int PROPS_TARGET = 1; ! private static final int PROPS_CLASS = 2; ! private static final int PROPS_CLASSNAME = 3; ! private static final int PROPS_RESULT = 4; ! private Variable target; ! private Object result; ! private String _className; ! private static final ArgDescriptor[] myArgs = new ArgDescriptor[] ! { new LValueArg(PROPS_TARGET), new OptionalGroup(3), new KeywordArg(PROPS_CLASS, "class"), new AssignmentArg(), new QuotedStringArg(PROPS_CLASSNAME), new BlockArg(PROPS_RESULT) }; ! private static final DirectiveDescriptor myDescr = new DirectiveDescriptor( "setprops", null, myArgs, null); ! public static DirectiveDescriptor getDescriptor() ! { ! return myDescr; ! } ! public SetpropsDirective() ! { ! } ! public Object build(DirectiveBuilder builder, BuildContext bc) throws BuildException ! { ! try ! { ! target = (Variable) builder.getArg(PROPS_TARGET, bc); ! } ! catch (ClassCastException e) ! { ! throw new NotVariableBuildException(myDescr.name, e); ! } ! _className = (String) builder.getArg(PROPS_CLASSNAME, bc); ! if (_className == null) ! _className = DEFAULT_CLASS_NAME; ! result = builder.getArg(PROPS_RESULT, bc); ! return this; ! } ! public void write(FastWriter out, Context context) throws PropertyException, ! IOException ! { ! try ! { ! if (!context.containsKey(target.getName())) ! { ! // target doesn't exist. Must create. ! // TODO check for class loading restrictions as per bean directive ! try ! { ! Class c = Instantiator.getInstance(context.getBroker()) ! .classForName(_className); ! Object o = c.newInstance(); ! target.setValue(context, o); ! } ! catch (RuntimeException re) ! { ! throw new PropertyException("Failed to create instance of " ! + _className + " for the #properties directive. " + re, ! re); ! } - } - String res = (String) ((Macro) result).evaluate(context); - String[] lines = TextTool.getLines(res); - String s; - String prevLine = ""; - String prefix = "#set $" + target.getVariableName() + "."; - for (int i = 0; i < lines.length; i++) - { - s = prevLine + lines[i].trim(); - if (s.endsWith("\\")) - { - // ends with continuation character. Add to next line. - prevLine = s.substring(0, s.length() - 1); } ! else ! { ! prevLine = ""; ! setProp(context, s, prefix); } ! } ! } ! catch (PropertyException e) ! { ! throw e; ! } ! catch (Exception e) ! { ! String errorText = "#setprops: Unable to set " + target; ! writeWarning(errorText, context, out); ! } ! } ! private void setProp(Context context, String s, String prefix) ! throws PropertyException ! { ! String prop; ! String val; ! StringTemplate stmpl; ! { ! if (s.length() > 0 && !s.startsWith("#")) ! { ! for (int j = 0; j < s.length(); j++) ! { ! char ch = s.charAt(j); ! if (ch == ':' || ch == '=') ! { ! prop = s.substring(0, j).trim(); ! val = s.substring(j + 1).trim(); ! // convert to WM syntax and evaluate ! // if (val.length() > 0) ! // { ! // try first as a string ! s = prefix + prop + "=\"" + val + "\""; ! try ! { ! stmpl = new StringTemplate(context.getBroker(), s); ! stmpl.evaluateAsString(context); ! } ! catch (WebMacroException wme) ! { ! // try again without quotes. ! s = prefix + prop + "=" + val; ! try ! { stmpl = new StringTemplate(context.getBroker(), s); stmpl.evaluateAsString(context); ! } ! catch (WebMacroException wme2) ! { ! PropertyException pex = new PropertyException( ! "Failed to set property \"" + prop ! + "\" to value \"" + val ! + "\" on variable \"" ! + target.getVariableName() ! + "\" of type " ! + target.getClass().getName(), wme2); ! context.getEvaluationExceptionHandler().evaluate( ! this.target, context, pex); ! } ! } ! break; ! } } ! } ! } ! } ! public void accept(TemplateVisitor v) ! { ! v.beginDirective(myDescr.name); ! v.visitDirectiveArg("PropertiesClassKeyword", "class"); ! v.visitDirectiveArg("PropertiesClassName", _className); ! v.visitDirectiveArg("PropertiesTarget", target); ! v.visitDirectiveArg("PropertiesValue", result); ! v.endDirective(); ! } } --- 47,196 ---- { ! private static final String DEFAULT_CLASS_NAME = "java.util.Hashtable"; ! private static final int PROPS_TARGET = 1; ! private static final int PROPS_CLASS = 2; ! private static final int PROPS_CLASSNAME = 3; ! private static final int PROPS_RESULT = 4; ! private Variable target; ! private Object result; ! private String _className; ! private static final ArgDescriptor[] myArgs = new ArgDescriptor[] { ! new LValueArg(PROPS_TARGET), new OptionalGroup(3), new KeywordArg(PROPS_CLASS, "class"), new AssignmentArg(), new QuotedStringArg(PROPS_CLASSNAME), new BlockArg(PROPS_RESULT) }; ! private static final DirectiveDescriptor myDescr = new DirectiveDescriptor( "setprops", null, myArgs, null); ! public static DirectiveDescriptor getDescriptor () ! { ! return myDescr; ! } ! public SetpropsDirective() ! { ! } ! public Object build (DirectiveBuilder builder, BuildContext bc) throws BuildException ! { ! try { ! target = (Variable) builder.getArg(PROPS_TARGET, bc); ! } catch (ClassCastException e) { ! throw new NotVariableBuildException(myDescr.name, e); ! } ! _className = (String) builder.getArg(PROPS_CLASSNAME, bc); ! if (_className == null) ! _className = DEFAULT_CLASS_NAME; ! result = builder.getArg(PROPS_RESULT, bc); ! return this; ! } ! public void write (FastWriter out, Context context) ! throws PropertyException, IOException ! { ! try { ! if (!context.containsKey(target.getName())) { ! // target doesn't exist. Must create. ! // TODO check for class loading restrictions as per bean directive ! try { ! Class c = Instantiator.getInstance(context.getBroker()) ! .classForName(_className); ! Object o = c.newInstance(); ! target.setValue(context, o); ! } catch (RuntimeException re) { ! throw new PropertyException("Failed to create instance of " ! + _className + " for the #properties directive. " ! + re, re); ! } } ! String res = (String) ((Macro) result).evaluate(context); ! String[] lines = TextTool.getLines(res); ! String s; ! String prevLine = ""; ! String prefix = "#set $" + target.getVariableName() + "."; ! for (int i = 0; i < lines.length; i++) { ! s = prevLine + lines[i].trim(); ! if (s.endsWith("\\")) { ! // ends with continuation character. Add to next line. ! prevLine = s.substring(0, s.length() - 1); ! } else { ! prevLine = ""; ! setProp(context, s, prefix); ! } } ! } catch (PropertyException e) { ! throw e; ! } catch (Exception e) { ! String errorText = "#setprops: Unable to set " + target; ! writeWarning(errorText, context, out); ! } ! } ! private void setProp (Context context, String s, String prefix) ! throws PropertyException ! { ! String prop; ! String val; ! StringTemplate stmpl; ! ! if (s.length() > 0 && !s.startsWith("#")) { ! for (int j = 0; j < s.length(); j++) { ! char ch = s.charAt(j); ! if (ch == ':' || ch == '=') { ! prop = s.substring(0, j).trim(); ! val = s.substring(j + 1).trim(); ! // convert to WM syntax and evaluate ! // if (val.length() > 0) ! // { ! // try first as a string ! s = prefix + prop + "=\"" + val + "\""; ! try { stmpl = new StringTemplate(context.getBroker(), s); stmpl.evaluateAsString(context); ! } catch (WebMacroException wme) { ! // try again without quotes. ! s = prefix + prop + "=" + val; ! try { ! stmpl = new StringTemplate(context.getBroker(), s); ! stmpl.evaluateAsString(context); ! } catch (WebMacroException wme2) { ! PropertyException pex = new PropertyException( ! "Failed to set property \"" + prop ! + "\" to value \"" + val ! + "\" on variable \"" ! + target.getVariableName() ! + "\" of type " ! + target.getClass().getName(), wme2); ! context.getEvaluationExceptionHandler().evaluate( ! this.target, context, pex); ! } ! } ! break; ! } } ! } ! } ! ! public void accept (TemplateVisitor v) ! { ! v.beginDirective(myDescr.name); ! v.visitDirectiveArg("PropertiesClassKeyword", "class"); ! v.visitDirectiveArg("PropertiesClassName", _className); ! v.visitDirectiveArg("PropertiesTarget", target); ! v.visitDirectiveArg("PropertiesValue", result); ! v.endDirective(); ! } } |