private Resource executeSimpleMerge(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { return resource.getResolver() .resolve( new SourceMerger("once".equals(options.get("source")) ? true : false).getMergedJsonFile(vfs, resource.getResolver(), resource.getPath())); }
@Override public void call(final Reader reader, final Writer writer) throws IOException { String root = resource.getPath(); final int idx = root.lastIndexOf('/'); root = root.substring(0, idx + 1); final int processorOptions = CSSURLEmbedder.DATAURI_OPTION | CSSURLEmbedder.SKIP_MISSING_OPTION; final int maxUriLength = getMaxUriLength(options); final int maxImageSize = getMaxImageSize(options); new Embedder(resource, new StringReader(resource.getContents()), processorOptions, true, maxUriLength, maxImageSize) .embedImages(writer, root); } });
/** * @see de.matrixweb.smaller.resource.Resource#apply(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Processor, java.util.Map) */ @Override public Resource apply(final VFS vfs, final Processor processor, final Map<String, Object> options) throws IOException { if (processor instanceof MultiResourceProcessor) { return processor.execute(vfs, this, options); } final List<Resource> list = new ArrayList<Resource>(); for (final Resource resource : this.resources) { list.add(resource.apply(vfs, processor, options)); } this.resources.clear(); this.resources.addAll(list); return this; }
private List<Resource> getJsonSourceFiles(final Resource resource) throws IOException { final List<Resource> list = new ArrayList<Resource>(); final Set<String> alreadyHandled = new HashSet<String>(); for (final String s : new ObjectMapper().readValue(resource.getContents(), String[].class)) { if (!isUniqueFileResolved(alreadyHandled, s)) { list.add(resource.getResolver().resolve(s)); alreadyHandled.add(s); } } return list; }
/** * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Resource, java.util.Map) */ @Override public Resource execute(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { // Version 1.0.0 handling if (getVersion(options).isAtLeast(Version._1_0_0)) { try { if (!(resource instanceof ResourceGroup) && resource != null && FilenameUtils.isExtension(resource.getPath(), "json")) { return executeSimpleMerge(vfs, resource, options); } return executeComplexMerge(vfs, resource, options); } catch (final IOException e) { throw new SmallerException("Failed to merge files", e); } } final VFile snapshot = vfs.stack(); try { final VFile file = vfs.find(resource.getPath()); VFSUtils.write(file, resource.getContents()); return resource.getResolver().resolve(file.getPath()); } catch (final IOException e) { vfs.rollback(snapshot); throw e; } }
private Resource executeComplexMerge(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { final Object typeOption = options.get("type"); if (!(resource instanceof ResourceGroup) || typeOption != null && resource.getType() != Type.valueOf(typeOption.toString())) { return resource; } final ResourceGroup group = (ResourceGroup) resource; final Resource input = group.getResources().get(0); final VFile snapshot = vfs.stack(); try { final VFile target = vfs.find(input.getPath()); final Writer writer = VFSUtils.createWriter(target); try { writer.write(group.getMerger().merge(group.getResources())); } finally { IOUtils.closeQuietly(writer); } return input.getResolver().resolve(target.getPath()); } catch (final IOException e) { vfs.rollback(snapshot); throw e; } }
/** * @see de.matrixweb.smaller.resource.Resource#getPath() */ @Override public String getPath() { return this.resources.get(0).getPath(); }
/** * @see de.matrixweb.smaller.resource.Resource#getResolver() */ @Override public ResourceResolver getResolver() { return this.resources.get(0).getResolver(); }
imgurl = new URL(url); } else { Resource img = this.resource.getResolver().resolve(url); imgurl = img.getURL(); if (imgurl == null) { img = this.resource.getResolver().resolve(originalUrl); imgurl = img.getURL();
private String merge(final List<Resource> resources, final String separator) throws IOException { final List<String> contents = new ArrayList<String>(); for (final Resource resource : resources) { contents.add(resource.getContents()); } return StringUtils.join(contents, separator); }
/** * @see de.matrixweb.smaller.resource.Resource#getType() */ @Override public Type getType() { return this.resources.isEmpty() ? Type.UNKNOWN : this.resources.get(0) .getType(); }
/** * @see de.matrixweb.smaller.resource.Resource#getPath() */ @Override public String getPath() { return this.resources.isEmpty() ? "" : this.resources.get(0).getPath(); }
vfs.stack(); vfs.importFS(outfolder); return resultPath == null ? input : input.getResolver().resolve(resultPath); } catch (NodeJsException e) { return resource;
@Deprecated private List<Resource> getJsonSourceFiles(final ResourceResolver resolver, final Resource resource) throws IOException { final List<Resource> list = new ArrayList<Resource>(); final Set<String> alreadyHandled = new HashSet<String>(); for (final String s : new ObjectMapper().readValue(resource.getContents(), String[].class)) { if (!isUniqueFileResolved(alreadyHandled, s)) { list.add(resolver.resolve(s)); alreadyHandled.add(s); } } return list; }
/** * @param type * @return Returns a list of {@link Resource}s matching the given type */ public final List<Resource> getByType(final Type type) { final List<Resource> list = new ArrayList<Resource>(); for (final Resource resource : this.resources) { if (resource.getType() == type) { list.add(resource); } } return list; }
@Deprecated private List<Resource> getSourceFiles(final ResourceResolver resolver, final List<String> in) throws IOException { final List<Resource> inputs = new ArrayList<Resource>(); for (final String s : in) { final Resource resource = resolver.resolve(s); if (resource.getPath().endsWith("json")) { inputs.addAll(getJsonSourceFiles(resource.getResolver(), resource)); } else { inputs.add(resource); } } return inputs; }
private List<String> scanResource(final Resource resource, final Map<String, Object> options) throws IOException { final List<String> results = new ArrayList<String>(); final Map<String, Object> opts = new HashMap<String, Object>(options); opts.remove("version"); final Map<String, Object> params = new HashMap<String, Object>(); params.put("source", resource.getContents()); params.put("options", opts); final StringWriter writer = new StringWriter(); this.executor .run(new StringReader(new ObjectMapper().writeValueAsString(params)), writer); final JsHintResult result = new ObjectMapper().readValue(writer.toString(), JsHintResult.class); if (result.errors != null) { for (final JsHintError error : result.errors) { results.add(resource.getPath() + " line " + error.line + ": " + error.reason + "\n\t" + error.evidence); } } return results; }
command.put("indir", infolder.getAbsolutePath()); if (resource != null) { command.put("file", resource.getPath());
/** * @see de.matrixweb.smaller.resource.Resource#apply(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Processor, java.util.Map) */ @Override public Resource apply(final VFS vfs, final Processor processor, final Map<String, Object> options) throws IOException { // Version 1.1.0 handling if (this.resources.isEmpty()) { LOGGER.info("Found empty input-ResourcesGroup; Processor '" + processor + "' will decide what to process"); return processor.execute(vfs, null, options); } if (processor instanceof MultiResourceProcessor) { LOGGER.info("MultiResourceProcessor '" + processor + "' execution on " + this); final Resource result = processor.execute(vfs, this, options); this.resources.clear(); this.resources.add(result); return result; } final List<Resource> list = new ArrayList<Resource>(); for (final Resource resource : this.resources) { LOGGER.info("Processor '" + processor + "' execution on " + resource); list.add(resource.apply(vfs, processor, options)); } this.resources.clear(); this.resources.addAll(list); return this; }
/** * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS, * de.matrixweb.smaller.resource.Resource, java.util.Map) */ @Override public Resource execute(final VFS vfs, final Resource resource, final Map<String, Object> options) throws IOException { if (this.node == null) { try { this.node = new NodeJsExecutor(); this.node.setModule(getClass(), "coffeescript-" + this.version, "coffeescript.js"); } catch (final IOException e) { this.node = null; throw new SmallerException("Failed to setup node for coffeescript", e); } } final String outfile = this.node.run(vfs, resource != null ? resource.getPath() : null, options); Resource result = resource; if (resource != null) { if (outfile != null) { result = resource.getResolver().resolve(outfile); } else if (FilenameUtils.isExtension(resource.getPath(), "coffee")) { result = resource.getResolver().resolve( FilenameUtils.removeExtension(resource.getPath()) + ".js"); } } return result; }