001 package com.softwarementors.extjs.djn.api; 002 003 import org.apache.log4j.Logger; 004 005 import com.softwarementors.extjs.djn.StringUtils; 006 import com.softwarementors.extjs.djn.jscodegen.Minifier; 007 008 import edu.umd.cs.findbugs.annotations.CheckForNull; 009 import edu.umd.cs.findbugs.annotations.NonNull; 010 011 public class RegisteredCode { 012 public @NonNull static final Logger logger = Logger.getLogger( RegisteredCode.class ); 013 014 private boolean minify; 015 private boolean debug; 016 private boolean minificationFailed; 017 018 private @NonNull String name; 019 private @NonNull String fullApiFileName; 020 private @NonNull StringBuilder debugCodeBuilder = new StringBuilder(); 021 private @NonNull StringBuilder nonCommentsCodeBuilder = new StringBuilder(); 022 private String minifiedCode; 023 private String debugCode; 024 private String nonCommentsCode; 025 026 public boolean hasDebugCode() { 027 return this.debug; 028 } 029 030 public boolean hasMinifiedCode() { 031 return this.minify; 032 } 033 034 public RegisteredCode( String name, String fullApiFileName, boolean minify, boolean debug ) { 035 assert !StringUtils.isEmpty(name); 036 assert !StringUtils.isEmpty(fullApiFileName); 037 038 this.name = name; 039 this.fullApiFileName = fullApiFileName; 040 this.minify = minify; 041 this.debug = debug; 042 } 043 044 @NonNull 045 public String getName() { 046 return this.name; 047 } 048 049 @NonNull 050 public String getFullApiFileName() { 051 return this.fullApiFileName; 052 } 053 054 @NonNull 055 public StringBuilder getDebugCodeBuilder() { 056 assert hasDebugCode(); 057 058 return this.debugCodeBuilder; 059 } 060 061 @NonNull 062 public String getDebugCode() { 063 if( this.debugCode == null ) { 064 this.debugCode = this.debugCodeBuilder.toString(); 065 } 066 return this.debugCode; 067 } 068 069 @NonNull 070 public StringBuilder getNonCommentsCodeBuilder() { 071 return this.nonCommentsCodeBuilder; 072 } 073 074 @NonNull 075 public String getNonCommentsCode() { 076 if( this.nonCommentsCode == null ) { 077 this.nonCommentsCode = this.nonCommentsCodeBuilder.toString(); 078 } 079 return this.nonCommentsCode; 080 } 081 082 @CheckForNull 083 public String getMinifiedCode() { 084 assert hasMinifiedCode(); 085 086 if( this.minifiedCode == null && !this.minificationFailed ) { 087 this.minifiedCode = Minifier.minify(getNonCommentsCode(), getName(), getDebugCode().length()); 088 this.minificationFailed = this.minifiedCode == null; 089 if( this.minificationFailed ) { 090 logger.warn( "Unable to minify code for '" + getName() + "'."); 091 } 092 } 093 return this.minifiedCode; 094 } 095 096 @NonNull 097 public String getCode() { 098 String code = null; 099 if( hasDebugCode() ) { 100 if( logger.isDebugEnabled()) { 101 logger.debug( "Production mode: using debug code for '" + getName() + "'"); 102 } 103 code = getDebugCode(); 104 } 105 else if( hasMinifiedCode() ) { 106 if( logger.isDebugEnabled()) { 107 logger.debug( "Production mode: using minified code for '" + getName() + "'"); 108 } 109 code = getMinifiedCode(); 110 } 111 if( code == null ) 112 code = getNonCommentsCode(); 113 return code; 114 } 115 }