Search in sources :

Example 1 with IJavaScriptCompressor

use of org.apache.wicket.javascript.IJavaScriptCompressor in project wicket by apache.

the class CompositeJavaScriptCompressor method process.

/**
 * Compresses the given original content in the order of compressors. If no compressor has been
 * given the original content is going to be returned.
 */
@Override
public String process(String input, Class<?> scope, String name) {
    String compressed = input;
    for (IJavaScriptCompressor compressor : compressors) {
        if (compressor instanceof IScopeAwareTextResourceProcessor) {
            IScopeAwareTextResourceProcessor processor = (IScopeAwareTextResourceProcessor) compressor;
            compressed = processor.process(compressed, scope, name);
        } else {
            compressed = compressor.compress(compressed);
        }
    }
    return compressed;
}
Also used : IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor)

Example 2 with IJavaScriptCompressor

use of org.apache.wicket.javascript.IJavaScriptCompressor in project wicket by apache.

the class ResourceSettings method setJavaScriptCompressor.

/**
 * Set the javascript compressor implemententation use e.g. by
 * {@link org.apache.wicket.request.resource.JavaScriptPackageResource
 * JavaScriptPackageResource}. A typical implementation will remove comments and whitespace. But
 * a no-op implementation is available as well.
 *
 * @param compressor
 *            The implementation to be used
 * @return The old value
 * @return {@code this} object for chaining
 */
public IJavaScriptCompressor setJavaScriptCompressor(IJavaScriptCompressor compressor) {
    IJavaScriptCompressor old = javascriptCompressor;
    javascriptCompressor = compressor;
    return old;
}
Also used : IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor)

Example 3 with IJavaScriptCompressor

use of org.apache.wicket.javascript.IJavaScriptCompressor in project wicket by apache.

the class ResourceBundles method addJavaScriptBundle.

/**
 * Adds a javascript bundle that is automatically generated by concatenating the given package
 * resources. If the given resources depend on each other, you should make sure that the
 * resources are provided in the order they need to be concatenated. If the resources depend on
 * other resources, that are not part of the bundle, the bundle will inherit these dependencies.
 *
 * This method is equivalent to {@link #addBundle(HeaderItem)} with a
 * {@link JavaScriptHeaderItem} for a {@link ConcatResourceBundleReference}.
 *
 * @param scope
 *            The {@linkplain ResourceReference#getScope() scope} of the bundle
 * @param defer
 *            specifies that the execution of a script should be deferred (delayed) until after
 *            the page has been loaded.
 * @param name
 *            The name of the resource. This will show up as the filename in the markup.
 * @param references
 *            The resources this bundle will consist of.
 * @return the newly created bundle
 */
public JavaScriptReferenceHeaderItem addJavaScriptBundle(Class<?> scope, String name, boolean defer, JavaScriptResourceReference... references) {
    List<JavaScriptReferenceHeaderItem> items = new ArrayList<>();
    for (JavaScriptResourceReference curReference : references) {
        items.add(JavaScriptHeaderItem.forReference(curReference));
    }
    ConcatResourceBundleReference<JavaScriptReferenceHeaderItem> bundleReference = newBundleResourceReference(scope, name, items);
    if (Application.exists()) {
        IJavaScriptCompressor javaScriptCompressor = Application.get().getResourceSettings().getJavaScriptCompressor();
        bundleReference.setCompressor(javaScriptCompressor);
    }
    if (defer) {
        return addBundle(JavaScriptHeaderItem.forReference(bundleReference, defer));
    } else {
        return addBundle(JavaScriptHeaderItem.forReference(bundleReference));
    }
}
Also used : JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) JavaScriptResourceReference(org.apache.wicket.request.resource.JavaScriptResourceReference) IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor) ArrayList(java.util.ArrayList)

Example 4 with IJavaScriptCompressor

use of org.apache.wicket.javascript.IJavaScriptCompressor in project wicket by apache.

the class JavaScriptPackageResource method processResponse.

@Override
protected byte[] processResponse(final Attributes attributes, byte[] bytes) {
    final byte[] processedResponse = super.processResponse(attributes, bytes);
    IJavaScriptCompressor compressor = getCompressor();
    if (compressor != null && getCompress()) {
        try {
            String charsetName = "UTF-8";
            String nonCompressed = new String(processedResponse, charsetName);
            String output;
            if (compressor instanceof IScopeAwareTextResourceProcessor) {
                IScopeAwareTextResourceProcessor scopeAwareProcessor = (IScopeAwareTextResourceProcessor) compressor;
                output = scopeAwareProcessor.process(nonCompressed, getScope(), name);
            } else {
                output = compressor.compress(nonCompressed);
            }
            return output.getBytes(charsetName);
        } catch (Exception e) {
            log.error("Error while filtering content", e);
            return processedResponse;
        }
    } else {
        // don't strip the comments
        return processedResponse;
    }
}
Also used : IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor) IScopeAwareTextResourceProcessor(org.apache.wicket.resource.IScopeAwareTextResourceProcessor)

Example 5 with IJavaScriptCompressor

use of org.apache.wicket.javascript.IJavaScriptCompressor in project wicket by apache.

the class JavaScriptPackageResourceTest method newApplication.

@Override
protected WebApplication newApplication() {
    return new MockApplication() {

        @Override
        protected void init() {
            super.init();
            getResourceSettings().setJavaScriptCompressor(new IJavaScriptCompressor() {

                @Override
                public String compress(String original) {
                    return APP_COMPRESSED;
                }
            });
        }
    };
}
Also used : MockApplication(org.apache.wicket.mock.MockApplication) IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor)

Aggregations

IJavaScriptCompressor (org.apache.wicket.javascript.IJavaScriptCompressor)5 ArrayList (java.util.ArrayList)1 JavaScriptReferenceHeaderItem (org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem)1 MockApplication (org.apache.wicket.mock.MockApplication)1 JavaScriptResourceReference (org.apache.wicket.request.resource.JavaScriptResourceReference)1 IScopeAwareTextResourceProcessor (org.apache.wicket.resource.IScopeAwareTextResourceProcessor)1