Skip to content

Commit

Permalink
Make grailsk-common a folder. Remove git submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
maiconandsilva committed Aug 9, 2022
1 parent a127d77 commit bdcb7ab
Show file tree
Hide file tree
Showing 14 changed files with 705 additions and 5 deletions.
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion grailszk-common
Submodule grailszk-common deleted from 0ddb6d
4 changes: 4 additions & 0 deletions grailszk-common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# zk-grails-extension

An extension library for the [zk-grails](https://github.com/zk-groovy/zk-grails) plugin with hooks for accessing and
modifying ZK internals to adapt to the Grails environment.
16 changes: 16 additions & 0 deletions grailszk-common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
dependencies {
implementation "org.grails:grails-dependencies:$grailsVersion"
implementation "org.grails:grails-core"
implementation "org.grails.plugins:gsp"
implementation "org.zkoss.zk:zk:$zkVersion"
implementation "org.zkoss.zk:zul:$zkVersion"
implementation "org.zkoss.zk:zhtml:$zkVersion"
implementation "org.zkoss.zk:zkplus:$zkVersion"
implementation "org.zkoss.zk:zkbind:$zkVersion"
implementation ("org.zkoss.common:zweb:$zkVersion") {
transitive = true
exclude module: "ResourceCaches"
}
implementation "org.zkoss.common:zel:$zkVersion"
implementation "org.zkoss.common:zcommon:$zkVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.codehaus.groovy.grails.compiler.zk;

import org.grails.compiler.injection.AbstractGrailsArtefactTransformer;
import org.zkoss.zk.grails.api.AbstractComposersApi;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.SourceUnit;
import java.net.URL;
import java.util.regex.Pattern;

public class GrailsComposerTransformer extends AbstractGrailsArtefactTransformer {
public static Pattern COMPOSER_PATTERN = Pattern.compile(".+/grails-app/composers/(.+)Composer\\.groovy");

public boolean shouldInject(final URL url) {
return url != null && COMPOSER_PATTERN.matcher(url.getFile()).find();
}

public String getArtefactType() {
return "Composer";
}

protected void performInjectionInternal(final String apiInstanceProperty, final SourceUnit source,
final ClassNode classNode) {
final ClassNode superClass = classNode.getSuperClass();
String superClassName = "";
if (superClass == null) { // TODO: Verify this code. Seems wrong. Probably unused.
superClassName = superClass.getName();
}

if (!superClassName.equals("java.lang.Object") && !superClassName.equals("groovy.lang.GroovyObject")) {
return;
}

try {
super.performInjectionInternal(apiInstanceProperty, source, classNode);
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}

public Class<?> getStaticImplementation() {
return null;
}

public Class<?> getInstanceImplementation() {
return AbstractComposersApi.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.zkoss.web.util.resource;

import grails.core.GrailsApplication;
import groovy.lang.Writable;
import groovy.text.Template;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.grails.core.io.DefaultResourceLocator;
import org.grails.gsp.GroovyPagesTemplateEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.util.resource.Locator;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.WebApp;
import org.zkoss.zk.ui.metainfo.PageDefinition;
import org.zkoss.zk.ui.metainfo.PageDefinitions;
import org.zkoss.zk.ui.metainfo.Parser;

import java.io.*;
import java.net.URL;
import java.util.Map;

@SuppressWarnings("deprecation")
public class GrailsContentLoader extends ResourceLoader<PageDefinition> {
private static final String GROOVY_PAGES_TEMPLATE_ENGINE = "groovyPagesTemplateEngine";
private static final String UTF_8_ENCODING = "UTF-8";
private static final String CONFIG_OPTION_GSP_ENCODING = "grails.views.gsp.encoding";
private static final String CONFIG_ZKGRAILS_TAGLIB_DISABLE = "grails.zk.taglib.disabled";
private static final Log log = LogFactory.getLog(GrailsContentLoader.class);
private final WebApp webApp;
private final ApplicationContext appCtx;
private final GrailsApplication grailsApplication;
private final ResourceUtils resourceUtils;

public GrailsContentLoader(final WebApp webApp) {
this.webApp = webApp;
final WebApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(webApp.getServletContext());
grailsApplication = ctx.getBean("grailsApplication", GrailsApplication.class);
appCtx = grailsApplication.getMainContext();
appCtx.getBean("grailsResourceLocator", DefaultResourceLocator.class);
resourceUtils = new ResourceUtils(appCtx);
}

@Override
public PageDefinition load(final ResourceInfo si) throws Exception {
final org.springframework.core.io.Resource springResource = resourceUtils.getResource(si.path);

if (springResource.exists()) {
log.debug("Load from Spring Resource: " + springResource);
try {
return parse(si.path, springResource, si.extra);
} catch (Throwable e) {
log.debug("Cannot parse ZUL from a Spring Resource", e);
throw (Exception) e;
}
}

if (si.url != null) {
log.debug("Load from URL: " + si.url);
return parse(si.path, si.url, si.extra);
}

if (!si.file.exists()) {
log.debug("File " + si.file + " not found");
return null;
}

try {
log.debug("Load from File: " + si.file);
return parse(si.path, si.file, si.extra);
} catch (FileNotFoundException ex) {
return null;
}
}

private StringReader preprocessGSP(final Map<?, ?> config, final long length, final InputStream in)
throws IOException {
log.debug("Enter :: preprocessGSP");
final GroovyPagesTemplateEngine gsp = (GroovyPagesTemplateEngine) appCtx.getBean(GROOVY_PAGES_TEMPLATE_ENGINE);
log.debug("Got GSP Template bean: " + gsp);
byte[] buffer;
final UnicodeBOMInputStream ubomIn = new UnicodeBOMInputStream(in);
if (ubomIn.getBOM() != UnicodeBOMInputStream.BOM.NONE) {
log.debug("BOM detected");
ubomIn.skipBOM();
buffer = new byte[(int)length - ubomIn.getBOM().getBytes().length];
}
else {
buffer = new byte[(int)length];
}
final BufferedInputStream bis = new BufferedInputStream(ubomIn);
bis.read(buffer);
String encoding = (String) config.get(CONFIG_OPTION_GSP_ENCODING);
if (encoding == null) {
encoding = UTF_8_ENCODING;
}
String bufferStr = new String(buffer, encoding).replaceAll("@\\{", "\\$\\{'@'\\}\\{");
bufferStr = TagDehyphen.dehyphen(bufferStr);

final Template template = gsp.createTemplate(new ByteArrayResource(bufferStr.getBytes(encoding)), false);
final Writable w = template.make();
final StringWriter sw = new StringWriter();
w.writeTo(new PrintWriter(sw));
final String zulSrc = sw.toString().replaceAll("\\#\\{", "\\$\\{");
final StringReader reader = new StringReader(zulSrc);
log.debug("Returning pre-processed ::: " + reader);
return reader;
}

private PageDefinition parse(final String path, final org.springframework.core.io.Resource resource,
final Object extra) throws Throwable {
final Map<?, ?> config = grailsApplication.getConfig().flatten();
final Boolean disable = (Boolean)config.get(CONFIG_ZKGRAILS_TAGLIB_DISABLE);
final Locator locator = (Locator)((extra != null) ? extra : PageDefinitions.getLocator(webApp, path));
if (disable != null && disable) {
return new Parser(webApp, locator).parse(new InputStreamReader(resource.getInputStream()), path);
}
final StringReader reader = preprocessGSP(config, resource.contentLength(), resource.getInputStream());
final PageDefinition pgdef = new Parser(webApp, locator).parse(reader, Servlets.getExtension(path));
pgdef.setRequestPath(path);
return pgdef;
}

@Override
protected PageDefinition parse(final String path, final URL url, final Object extra) throws Exception {
final Locator locator = (Locator)((extra != null) ? extra : PageDefinitions.getLocator(webApp, path));
return new Parser(webApp, locator).parse(url, path);
}

@Override
protected PageDefinition parse(final String path, final File file, final Object extra) throws Exception {
final GrailsApplication grailsApplication = (GrailsApplication) appCtx.getBean("grailsApplication");
final Map<?, ?> config = grailsApplication.getConfig().flatten();
final Boolean disable = (Boolean) config.get(CONFIG_ZKGRAILS_TAGLIB_DISABLE);
final Locator locator = (Locator)((extra != null) ? extra : PageDefinitions.getLocator(webApp, path));
if (disable != null && disable) {
return new Parser(webApp, locator).parse(file, path);
}
final StringReader reader = preprocessGSP(config, file.length(), new FileInputStream(file));
final PageDefinition pgdef = new Parser(webApp, locator).parse(reader, Servlets.getExtension(path));
pgdef.setRequestPath(path);
return pgdef;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* ResourceCaches.java
Purpose:
Description:
History:
Tue Aug 30 18:31:05 2005, Created by tomyeh
Copyright (C) 2005 Potix Corporation. All Rights Reserved.
{{IS_RIGHT
This program is distributed under LGPL Version 2.1 in the hope that
it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.web.util.resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.zkoss.lang.SystemException;
import org.zkoss.web.servlet.Servlets;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext;

/**
* Utilities to load (and parse) the Servlet resource.
* Notice that {@link ResourceCache} and {@link ResourceLoader}
* must be used rather than
* {@link org.zkoss.util.resource.ResourceCache}
* and {@link org.zkoss.util.resource.Loader}.
*
* <p>Usage:
* <ol>
* <li>Implements a loader by extending from {@link ResourceLoader}.</li>
* <li>Creates a resource cache ({@link ResourceCache})
* by use of the loader in the previous step.</li>
* <li>Invoke {@link #get} to load the resource.</li>
* </ol>
*
* @see <a href="https://github.com/zkoss/zk/blob/master/zweb/src/org/zkoss/web/util/resource/ResourceCaches.java"/>
* ResourceCaches.java Original class
* </a>
*
* @author tomyeh
*/
public class ResourceCaches {
private static final Logger log = LoggerFactory.getLogger(ResourceCaches.class);

static {
log.info("Using custom org.zkoss.web.util.resource.ResourceCaches."
+ " See https://github.com/zkgroovy/zk-grails/issues/6");
}

/** Loads, parses and returns the resource of the specified URI,
* or null if not found. The parser is defined by the loader defined
* in {@link ResourceCache}.
*
* @param cache the resource cache.
* Note: its loader must extend from {@link ResourceLoader}.
* @param path the URI path
* @param extra the extra parameter that will be passed to
* {@link ResourceLoader#parse(String,File,Object)} and
* {@link ResourceLoader#parse(String,URL,Object)}
*/
public static final <V>
V get(ResourceCache<V> cache, ServletContext ctx, String path, Object extra) {
//20050905: Tom Yeh
//We don't need to handle the default name if user specifies only a dir
//because it is handled by the container directly
//And, web developer has to specify <welcome-file> in web.xml
URL url = null;
if (path == null || path.length() == 0) path = "/";
else if (path.charAt(0) != '/') {
if (path.indexOf("://") > 0) {
try {
url = new URL(path);
} catch (MalformedURLException ex) {
throw new SystemException(ex);
}
}else path = '/' + path;
}

if (url == null) {
if (path.startsWith("/~")) {
final ServletContext ctx0 = ctx;
final String path0 = path;
final int j = path.indexOf('/', 2);
final String ctxpath;
if (j >= 0) {
ctxpath = "/" + path.substring(2, j);
path = path.substring(j);
} else {
ctxpath = "/" + path.substring(2);
path = "/";
}

final ExtendletContext extctx =
Servlets.getExtendletContext(ctx, ctxpath.substring(1));
if (extctx != null) {
url = extctx.getResource(path);
// if (log.isDebugEnabled()) log.debug("Resolving "+path0+" to "+url);
if (url == null)
return null;
try {
return cache.get(new ResourceInfo(path, url, extra));
} catch (Throwable ex) {
final IOException ioex = getIOException(ex);
if (ioex == null)
throw SystemException.Aide.wrap(ex);
log.warn("Unable to load "+url, ioex);
}
return null;
}

ctx = ctx.getContext(ctxpath);
if (ctx == null) { //failed
// if (log.isDebugEnabled()) log.debug("Context not found: "+ctxpath);
ctx = ctx0; path = path0;//restore
}
}

//! replace ServletContext#getRealPath by lines below
final WebApplicationContext applicationContext = getRequiredWebApplicationContext(ctx);
String flnm = new ResourceUtils(applicationContext).getRealPath(path);

if (flnm != null) {
try {
return cache.get(new ResourceInfo(path, new File(flnm), extra));
//it is loader's job to check the existence
} catch (Throwable ex) {
final IOException ioex = getIOException(ex);
if (ioex == null)
throw SystemException.Aide.wrap(ex);
log.warn("Unable to load "+flnm, ioex);
}
return null;
}
}

//try url because some server uses JAR format
try {
if (url == null)
url = ctx.getResource(path);
if (url != null)
return cache.get(new ResourceInfo(path, url, extra));
} catch (Throwable ex) {
final IOException ioex = getIOException(ex);
if (ioex == null)
throw SystemException.Aide.wrap(ex);
log.warn("Unable to load "+path, ioex);
}
return null;
}
//don't eat exceptions other than IOException
private static IOException getIOException(Throwable ex) {
for (; ex != null; ex = ex.getCause())
if (ex instanceof IOException)
return (IOException)ex;
return null;
}
}
Loading

0 comments on commit bdcb7ab

Please sign in to comment.