-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move "inline parser assistance" to "junk" package for some incubation
- Loading branch information
1 parent
161b7f9
commit 276f73c
Showing
2 changed files
with
98 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
ng-appserver/src/main/java/x/junk/NGElementNotFoundElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package x.junk; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.nio.file.attribute.FileTime; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
import ng.appserver.NGActionResults; | ||
import ng.appserver.NGApplication; | ||
import ng.appserver.NGContext; | ||
import ng.appserver.NGElement; | ||
import ng.appserver.NGRequest; | ||
import ng.appserver.NGResponse; | ||
|
||
public class NGElementNotFoundElement implements NGElement { | ||
|
||
private String _type; | ||
|
||
public NGElementNotFoundElement( final String type ) { | ||
_type = type; | ||
} | ||
|
||
@Override | ||
public void appendToResponse( NGResponse response, NGContext context ) { | ||
String s = """ | ||
<a href="%s" style="padding: 10px; border: 2px solid rgba(50,50,200,0.6); box-shadow: 4px 4px 1px red; background-color: rgba(0,0,200,0.5); border-radius: 4px; text-decoration: none; color: white"> | ||
Can't find an element/component '<strong>%s</strong>'. Would you like to create it? | ||
</a> | ||
""".formatted( context.componentActionURL() + "?elementName=" + _type, _type ); | ||
|
||
response.appendContentString( s ); | ||
}; | ||
|
||
@Override | ||
public NGActionResults invokeAction( NGRequest request, NGContext context ) { | ||
|
||
if( context.currentElementIsSender() ) { | ||
System.out.println( "Let's create that component!" ); | ||
|
||
final String packageName = NGApplication.application().getClass().getPackageName() + ".elements"; | ||
final String elementName = request.formValueForKey( "elementName" ); | ||
final String elementString = """ | ||
package %s; | ||
import ng.appserver.NGContext; | ||
import ng.appserver.NGElement; | ||
import ng.appserver.NGResponse; | ||
/** | ||
* Your new element | ||
*/ | ||
public class %s implements NGElement { | ||
@Override | ||
public void appendToResponse( final NGResponse response, final NGContext context ) { | ||
response.appendContentString( "Congratulations on your new element!" ); | ||
} | ||
} | ||
""".formatted( packageName, elementName, elementName ); | ||
|
||
final Path root = Paths.get( "." ).normalize().toAbsolutePath(); | ||
final Path dirPath = Paths.get( root.toString(), "src", "main", "java", "smu", "elements" ); // FIXME: Split the package name elements into corresponding directories | ||
final Path filePath = Paths.get( dirPath.toString(), elementName + ".java" ); // FIXME: Split the package name elements into corresponding directories | ||
|
||
System.out.println( "Creating new dynamic element at %s".formatted( filePath ) ); | ||
|
||
try { | ||
Files.createDirectories( dirPath ); | ||
Files.write( filePath, elementString.getBytes(), StandardOpenOption.CREATE_NEW ); | ||
touch( Paths.get( root.toString(), ".project" ) ); | ||
} | ||
catch( IOException e ) { | ||
throw new UncheckedIOException( e ); | ||
} | ||
} | ||
|
||
return NGElement.super.invokeAction( request, context ); | ||
} | ||
|
||
public static void touch( final Path path ) throws IOException { | ||
Objects.requireNonNull( path, "path is null" ); | ||
try { | ||
Files.createFile( path ); | ||
} | ||
catch( FileAlreadyExistsException e ) { | ||
Files.setLastModifiedTime( path, FileTime.from( Instant.now() ) ); | ||
} | ||
} | ||
}; |