Example usage for com.google.gwt.core.ext.linker CompilationResult getPropertyMap

List of usage examples for com.google.gwt.core.ext.linker CompilationResult getPropertyMap

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.linker CompilationResult getPropertyMap.

Prototype

public abstract SortedSet<SortedMap<SelectionProperty, String>> getPropertyMap();

Source Link

Document

Provides values for SelectionProperty instances that are not explicitly set during the compilation phase.

Usage

From source file:cc.alcina.framework.gwt.appcache.linker.PermutationInfoLinker.java

License:Apache License

private List<Artifact<?>> emitSelectionInformation(String strongName, CompilationResult result) {
    List<Artifact<?>> emitted = new ArrayList<Artifact<?>>();
    for (SortedMap<SelectionProperty, String> propertyMap : result.getPropertyMap()) {
        TreeMap<String, String> propMap = new TreeMap<String, String>();
        for (Map.Entry<SelectionProperty, String> entry : propertyMap.entrySet()) {
            propMap.put(entry.getKey().getName(), entry.getValue());
        }/*from w w  w.jav a  2s.c  o  m*/
        // The soft properties may not be a subset of the existing set
        for (SoftPermutation soft : result.getSoftPermutations()) {
            // Make a copy we can add add more properties to
            TreeMap<String, String> softMap = new TreeMap<String, String>(propMap);
            // Make sure this SelectionInformation contains the soft
            // properties
            for (Map.Entry<SelectionProperty, String> entry : soft.getPropertyMap().entrySet()) {
                softMap.put(entry.getKey().getName(), entry.getValue());
            }
            emitted.add(new SelectionInformation(strongName, soft.getId(), softMap));
        }
    }
    return emitted;
}

From source file:com.bedatadriven.rebar.appcache.linker.AppCacheIFrameLinker.java

License:Apache License

/**
 * The permutation map is used on the server side to serve the appropriate permutation
 *///from  w  w  w  .  j  ava  2s. co  m
protected EmittedArtifact emitPermutationMap(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {

    try {

        // Emit the selection script.
        StringWriter json = new StringWriter();
        JsonWriter writer = new JsonWriter(json);
        writer.setIndent("  ");

        SortedSet<CompilationResult> compilationResults = artifacts.find(CompilationResult.class);
        writer.beginArray();

        for (CompilationResult result : compilationResults) {
            for (SortedMap<SelectionProperty, String> map : result.getPropertyMap()) {
                writer.beginObject();
                writer.name("permutation").value(result.getStrongName());
                writer.name("properties");
                writer.beginObject();

                for (Map.Entry<SelectionProperty, String> property : map.entrySet()) {
                    writer.name(property.getKey().getName());
                    writer.value(property.getValue());
                }
                writer.endObject();
                writer.endObject();
            }
        }
        writer.endArray();
        return emitString(logger, json.toString(), "permutations");

    } catch (IOException e) {
        logger.log(TreeLogger.Type.ERROR, "Error writing permutation map", e);
        throw new UnableToCompleteException();
    }
}

From source file:gwt.ns.webworker.linker.WorkerCompilationLinker.java

License:Apache License

/**
 * Searches for all instances of a placeholder String in a
 * CompilationResult. If found, they are replaced as specified and a
 * new CompilationResult, with a newly generated strong name, is returned.
 * If no occurrences were found, the original CompilationResult is
 * returned.//from  w  w  w.ja v  a2 s . c o m
 * 
 * @param logger
 * @param result CompilationResult to process
 * @param placeholder String to be replaced
 * @param replacement String to insert in place of placeholder
 * 
 * @return A CompilationResult suitable for emitting
 */
public CompilationResult replacePlaceholder(TreeLogger logger, CompilationResult result, String placeholder,
        String replacement) {

    boolean needsMod = false;

    String[] js = result.getJavaScript();
    StringBuffer[] jsbuf = new StringBuffer[js.length];
    for (int i = 0; i < jsbuf.length; i++) {
        jsbuf[i] = new StringBuffer(js[i]);
    }

    // search and replace in all fragments
    for (StringBuffer fragment : jsbuf) {
        needsMod |= replaceAll(fragment, placeholder, replacement);
    }

    // by default, returning unaltered result
    CompilationResult toReturn = result;

    // code has been altered, need to create new CompilationResult
    if (needsMod) {
        logger.log(TreeLogger.SPAM,
                "Compilation permutation " + result.getPermutationId() + " being modified.");

        // new js for compilation result
        byte[][] newcode = new byte[jsbuf.length][];
        for (int i = 0; i < jsbuf.length; i++) {
            newcode[i] = Util.getBytes(jsbuf[i].toString());
        }

        // new strong name
        String strongName = Util.computeStrongName(newcode);

        // same symbolMap copied over since none altered
        // symbolMap a little more complicated
        // can only get deserialized version, need to reserialize
        // code from com.google.gwt.dev.jjs.JavaToJavaScriptCompiler
        // TODO: turns out this can get pretty bad. Workaround?
        SymbolData[] symbolMap = result.getSymbolMap();
        byte[] serializedSymbolMap;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Util.writeObjectToStream(baos, (Object) symbolMap);
            serializedSymbolMap = baos.toByteArray();
        } catch (IOException e) {
            // should still never happen
            logger.log(TreeLogger.ERROR, "IOException while reserializing " + "SymbolMap.");
            throw new RuntimeException("Should never happen with in-memory stream", e);
        }

        StandardCompilationResult altered = new StandardCompilationResult(strongName, newcode,
                serializedSymbolMap, result.getStatementRanges(), result.getPermutationId());

        // need to copy permutation properties to new result
        for (Map<SelectionProperty, String> propertyMap : result.getPropertyMap()) {
            altered.addSelectionPermutation(propertyMap);
        }

        toReturn = altered;

        logger.log(TreeLogger.SPAM, "Compilation permuation " + toReturn.getPermutationId()
                + " altered to include path to worker script(s).");
    }

    return toReturn;
}

From source file:org.cruxframework.crux.core.rebind.offline.AppCacheLinker.java

License:Apache License

private static String getProperty(ArtifactSet artifacts, String property) {
    for (CompilationResult result : artifacts.find(CompilationResult.class)) {
        if (result.getPropertyMap() != null && !result.getPropertyMap().isEmpty()) {
            for (SortedMap<SelectionProperty, String> propertyMap : result.getPropertyMap()) {
                for (SelectionProperty selectionProperty : propertyMap.keySet()) {
                    if (property.equals(selectionProperty.getName())) {
                        return propertyMap.get(selectionProperty);
                    }//  w w w .jav a2  s.com
                }
            }
        }

    }
    return null;
}

From source file:org.sigmah.linker.ManifestGenerationLinker.java

License:Open Source License

@Override
public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
        throws UnableToCompleteException {
    final ArtifactSet artifactSet = new ArtifactSet(artifacts);

    final SortedSet<EmittedArtifact> emittedArtifacts = artifacts.find(EmittedArtifact.class);
    final SortedSet<CompilationResult> compilationResults = artifacts.find(CompilationResult.class);

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    final HashMap<Manifest, StringBuilder> manifests = new HashMap<>();

    final Map<String, Manifest> permutationMap = new HashMap<>();
    for (final CompilationResult compilationResult : compilationResults) {
        final String permutationName = compilationResult.getStrongName();

        for (final SortedMap<SelectionProperty, String> map : compilationResult.getPropertyMap()) {
            String userAgent = null;
            String locale = null;

            for (Map.Entry<SelectionProperty, String> entry : map.entrySet()) {
                if (entry.getKey().getName().equals("user.agent")) {
                    userAgent = entry.getValue();
                }//from w  ww .j  a v a  2s. co  m
                if (entry.getKey().getName().equals("locale")) {
                    locale = entry.getValue();
                }
            }

            final Manifest manifest = new Manifest(userAgent, locale);
            System.out.println("      Permutation '" + permutationName + "' : " + manifest);

            permutationMap.put(permutationName, manifest);
            addManifest(manifests, manifest, dateFormat);
        }
    }

    if (permutationMap.isEmpty()) {
        addManifest(manifests, new Manifest(), dateFormat);
    }

    appendToAll(manifests, MODULE_NAME + ".nocache.js\n");
    appendToAll(manifests, MODULE_NAME + ".extra.nocache.js\n");

    for (EmittedArtifact artifact : emittedArtifacts) {
        if (EmittedArtifact.Visibility.Public.matches(artifact.getVisibility())) {
            final String partialPath = artifact.getPartialPath();
            Manifest manifest = null;
            for (Map.Entry<String, Manifest> entry : permutationMap.entrySet()) {
                if (partialPath.contains(entry.getKey())) {
                    manifest = entry.getValue();
                    break;
                }
            }

            final StringBuilder manifestBuilder = manifests.get(manifest);
            if (manifestBuilder != null) {
                manifestBuilder.append(artifact.getPartialPath()).append("\n");
            } else if (!partialPath.startsWith("manuals/")) {
                appendToAll(manifests, artifact.getPartialPath()).appendToAll(manifests, "\n");
            }
        }
    }

    // Generating offline fallback sources
    appendToAll(manifests, "FALLBACK:\nonline.nocache.json offline.nocache.json\n");
    artifactSet.add(emitString(logger, "{\"online\": false}", "offline.nocache.json"));
    artifactSet.add(emitString(logger, "{\"online\": true}", "online.nocache.json"));

    appendToAll(manifests, "is_online.nocache.js is_offline.nocache.js\n");
    artifactSet.add(emitString(logger, "window.online = false;", "is_offline.nocache.js"));
    artifactSet.add(emitString(logger, "window.online = true;", "is_online.nocache.js"));

    appendToAll(manifests, "export export_offline.html\n");
    artifactSet.add(emitString(logger,
            "<html><header><title>Sigmah</title></header><body>Export functionality is only available when online.</body></html>",
            "export_offline.html"));

    if (PREFER_ONLINE) {
        appendToAll(manifests, "SETTINGS:\nprefer-online\n");
    }

    appendToAll(manifests, "NETWORK:\n*");

    for (Map.Entry<Manifest, StringBuilder> entry : manifests.entrySet()) {
        artifactSet.add(emitString(logger, entry.getValue().toString(), entry.getKey().toFileName()));
    }
    return artifactSet;
}