litesoft
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.codegen; import java.util.*; public class SourceCodeGenerator { public static final SourceCodeGenerator[] EMPTY = new SourceCodeGenerator[0]; protected static final String JAVA_LANG_PACKAGE = "java.lang."; public enum Access { PUBLIC( "public " ), PROTECTED( "protected " ), PRIVATE( "private " ), PACKAGE( "" ); Access( String pForm ) { mForm = pForm; } private String mForm; public String getForm() { return mForm; } } public static final String INDENT = " "; public interface MultiFactory { public String getName(); public SourceCodeGenerator[] getSourceCodeGenerators(); } private String mReportingType; private String mExternalReference; // eg a File...getPath() private Access mClassAccess = Access.PUBLIC; private String mClassModifiers = ""; private String mCurTabDepth = ""; private StringBuilder mSourceCodeText = new StringBuilder(); private boolean mOverwrite = true; public SourceCodeGenerator( String pReportingType, String pExternalReference ) { mReportingType = pReportingType; mExternalReference = pExternalReference; } public final String getReportingType() { return mReportingType; } public final String getExternalReference() { return mExternalReference; } public final boolean isOverwrite() { return mOverwrite; } public final void setOverwrite( boolean pOverwrite ) { mOverwrite = pOverwrite; } public final void indentStart() { mCurTabDepth += INDENT; } public final void indentEnd() { if ( mCurTabDepth.startsWith( INDENT ) ) { mCurTabDepth = mCurTabDepth.substring( INDENT.length() ); } } public final void indentPlus( int pBy ) { while ( pBy-- > 0 ) { mCurTabDepth += " "; } } public final void indentMinus( int pBy ) { for ( int i = 0; (i < pBy) && mCurTabDepth.startsWith( " " ); i++ ) { mCurTabDepth = mCurTabDepth.substring( 1 ); } } public final void indentedAddLine( String pLine ) { indentStart(); addLine( pLine ); indentEnd(); } public final void doubleIndentedAddLine( String pLine ) { indentStart(); indentedAddLine( pLine ); indentEnd(); } public final void addLineIf( String pLine, boolean pAdd ) { if ( pAdd ) { addLine( pLine ); } } public final void addLine( String pLine ) { if ( isNotNullOrEmpty( pLine ) ) { mSourceCodeText.append( mCurTabDepth ).append( pLine ); } mSourceCodeText.append( '\n' ); } public final void addLines( String... pLines ) { if ( pLines != null ) { for ( String line : pLines ) { if ( line != null ) { addLine( line ); } } } } public final void addLines( List<String> pLines ) { if ( pLines != null ) { for ( String line : pLines ) { if ( line != null ) { addLine( line ); } } } } public final void addBlankLine() { mSourceCodeText.append( '\n' ); } public final void addBlockStart() { addLine( "{" ); indentStart(); } public final void addBlockStart( String pPlus ) { addLine( "{" + pPlus ); indentStart(); } public final void addBlockEnd() { indentEnd(); addLine( "}" ); } public final void addBlockEnd( String pPlus ) { indentEnd(); addLine( "}" + pPlus ); } public final void addPackage( String pPackageName ) { if ( isNotNullOrEmpty( pPackageName ) ) { addLine( "package " + pPackageName.trim() + ";" ); addBlankLine(); } } public final void addImports( String... pImportRefs ) { if ( pImportRefs != null ) { for ( String zImportRef : pImportRefs ) { addImport( zImportRef ); } addBlankLine(); } } public final void addImport( String pImportRef ) { if ( pImportRef != null ) { if ( pImportRef.length() != 0 ) { addLine( "import " + pImportRef + ";" ); } else { addBlankLine(); } } } public final void addInterfaceDefinition( String pInterfaceName, String... pExtendsNames ) { addLine( "public interface " + pInterfaceName ); String extendsName = csEm( pExtendsNames ); if ( isNotNullOrEmpty( extendsName ) ) { doubleIndentedAddLine( "extends " + extendsName ); } addBlockStart(); } public final void addInterfaceEnd() { addBlockEnd(); } public final void makeClassAbstract() { mClassModifiers += "abstract "; } public final void makeClassStatic() { mClassModifiers += "static "; } public final void makeClassPrivate() { mClassAccess = Access.PRIVATE; } public final void makeClassProtected() { mClassAccess = Access.PROTECTED; } public final void makeClassPackageFriendly() { mClassAccess = Access.PACKAGE; } private Access getClassAccess() { Access rv = mClassAccess; mClassAccess = Access.PUBLIC; return rv; } private String getClassModifiers() { String rv = mClassModifiers; mClassModifiers = ""; return rv; } public final void addClassDefinition( String pClassName ) { addClassDefinition( pClassName, null, (String[]) null ); } public final void addClassDefinition( String pClassName, String[] pImplementsNames ) // Note: Can NOT use VarArgs as they would collide with other method! { addClassDefinition( pClassName, null, pImplementsNames ); } public final void addClassDefinition( String pClassName, String pExtendsName, String... pImplementsNames ) { addLine( getClassAccess().getForm() + getClassModifiers() + "class " + pClassName ); if ( isNotNullOrEmpty( pExtendsName ) ) { doubleIndentedAddLine( "extends " + pExtendsName.trim() ); } String implementsName = csEm( pImplementsNames ); if ( isNotNullOrEmpty( implementsName ) ) { doubleIndentedAddLine( "implements " + implementsName.trim() ); } addBlockStart(); } public final void addClassEnd() { addBlockEnd(); } public final void addPSFS( String pConstantName, String pValue ) { addLine( "public static final String " + pConstantName + " = " + quote( pValue ) + ";" ); } public final String quote( String pValue ) { return (pValue == null) ? "null" : "\"" + pValue + "\""; } public final void addConstructorPublic( String pClassName, String... pParameters ) { addConstructor( Access.PUBLIC, pClassName, pParameters ); } public final void addConstructorProtected( String pClassName, String... pParameters ) { addConstructor( Access.PROTECTED, pClassName, pParameters ); } public final void addConstructorPrivate( String pClassName, String... pParameters ) { addConstructor( Access.PRIVATE, pClassName, pParameters ); } public final void addConstructorPackageFriendly( String pClassName, String... pParameters ) { addConstructor( Access.PACKAGE, pClassName, pParameters ); } public final void addConstructor( Access pAccess, String pClassName, String... pParameters ) { addLine( pAccess.getForm() + pClassName + parenEm( csEm( pParameters ) ) ); addBlockStart(); } public final void addMethodPublic( String pReturnType, String pMethodName, String... pParameters ) { addMethod( Access.PUBLIC, pReturnType, pMethodName, pParameters ); } public final void addMethodProtected( String pReturnType, String pMethodName, String... pParameters ) { addMethod( Access.PROTECTED, pReturnType, pMethodName, pParameters ); } public final void addMethodPrivate( String pReturnType, String pMethodName, String... pParameters ) { addMethod( Access.PRIVATE, pReturnType, pMethodName, pParameters ); } public final void addMethodPackageFriendly( String pReturnType, String pMethodName, String... pParameters ) { addMethod( Access.PACKAGE, pReturnType, pMethodName, pParameters ); } public final void addMethod( Access pAccess, String pReturnType, String pMethodName, String... pParameters ) { addLine( pAccess.getForm() + pReturnType + " " + pMethodName + parenEm( csEm( pParameters ) ) ); addBlockStart(); } public final void addStaticMethodPublic( String pReturnType, String pMethodName, String... pParameters ) { addStaticMethod( Access.PUBLIC, pReturnType, pMethodName, pParameters ); } public final void addStaticMethodProtected( String pReturnType, String pMethodName, String... pParameters ) { addStaticMethod( Access.PROTECTED, pReturnType, pMethodName, pParameters ); } public final void addStaticMethodPrivate( String pReturnType, String pMethodName, String... pParameters ) { addStaticMethod( Access.PRIVATE, pReturnType, pMethodName, pParameters ); } public final void addStaticMethodPackageFriendly( String pReturnType, String pMethodName, String... pParameters ) { addStaticMethod( Access.PACKAGE, pReturnType, pMethodName, pParameters ); } private void addStaticMethod( Access pAccess, String pReturnType, String pMethodName, String... pParameters ) { addLine( pAccess.getForm() + "static " + pReturnType + " " + pMethodName + parenEm( csEm( pParameters ) ) ); addBlockStart(); } public final void addAbstractMethodPublic( String pReturnType, String pMethodName, String... pParameters ) { addAbstractMethod( Access.PUBLIC, pReturnType, pMethodName, pParameters ); } public final void addAbstractMethodProtected( String pReturnType, String pMethodName, String... pParameters ) { addAbstractMethod( Access.PROTECTED, pReturnType, pMethodName, pParameters ); } private void addAbstractMethod( Access pAccess, String pReturnType, String pMethodName, String... pParameters ) { addLine( "abstract " + pAccess.getForm() + pReturnType + " " + pMethodName + parenEm( csEm( pParameters ) ) + ";" ); } public final void addMethodEnd() { addBlockEnd(); addBlankLine(); } public final void addInterfaceMethodDefinition( String pReturnType, String pMethodName, String... pParameters ) { addLine( "public " + pReturnType + " " + pMethodName + parenEm( csEm( pParameters ) ) ); addBlankLine(); } public final void addTryStart() { addLine( "try" ); addBlockStart(); } public final void addTryEnd() { addBlockEnd(); } public final void addCatch( String pClause ) { addBlockEnd(); addLine( "catch ( " + pClause + " )" ); addBlockStart(); } public final void addCatchEnd() { addBlockEnd(); } private String csEm( String[] pNames ) { if ( (pNames != null) && (pNames.length > 0) ) { StringBuilder sb = new StringBuilder(); for ( String name : pNames ) { if ( isNotNullOrEmpty( name ) ) { sb.append( ", " ); sb.append( name ); } } if ( sb.length() != 0 ) { return sb.toString().substring( 2 ); } } return null; } public final String parenEm( String pParameters ) { return isNotNullOrEmpty( pParameters ) ? "( " + pParameters.trim() + " )" : "()"; } @Override public final String toString() { return mSourceCodeText.toString(); } protected static boolean isNotNullOrEmpty( String pStringToCheck ) { return ((pStringToCheck != null) && (pStringToCheck.trim().length() != 0)); } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/codegen/SourceCodeGenerator.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
610 Diff | GeorgeS | Mon 12 Mar, 2012 00:54:00 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |