Example usage for com.google.gwt.core.ext GeneratorContext getPropertyOracle

List of usage examples for com.google.gwt.core.ext GeneratorContext getPropertyOracle

Introduction

In this page you can find the example usage for com.google.gwt.core.ext GeneratorContext getPropertyOracle.

Prototype

PropertyOracle getPropertyOracle();

Source Link

Document

Gets the property oracle for the current generator context.

Usage

From source file:com.allen_sauer.gwt.log.rebind.LogMessageFormatterGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;//  w  ww. j av a  2  s  .co m
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    composerFactory.addImport(Date.class.getName());
    composerFactory.addImport(GWT.class.getName());
    composerFactory.addImport(LogUtil.class.getName());
    composerFactory.addImport(Duration.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logPattern;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_PATTERN);
            List<String> values = logPatternProperty.getValues();
            logPattern = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_PATTERN + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("private double BIG_BANG = Duration.currentTimeMillis();");

        sw.println();
        sw.println(
                "public String format(String logLevelText, String category, String message, Throwable throwable) {");
        sw.indent();
        sw.println("if (category == null) {");
        sw.indent();
        sw.println("category = \"<null category>\";");
        sw.outdent();
        sw.println("}");
        sw.println("if (message == null) {");
        sw.indent();
        sw.println("message = \"<null message>\";");
        sw.outdent();
        sw.println("}");
        sw.println(logPatternToCode(logPattern));
        sw.outdent();
        sw.println("}");

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.allen_sauer.gwt.log.rebind.RemoteLoggerConfigGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;//from   www .  j ava  2 s . c  om
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logUrl;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_URL);
            List<String> values = logPatternProperty.getValues();
            logUrl = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_URL + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("public String serviceEntryPointUrl() {");
        sw.indent();

        if (logUrl == null) {
            sw.println("return null;");
        } else {
            sw.println("return \"" + logUrl.trim() + "\";");
        }

        sw.outdent();
        sw.println("}");

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.allen_sauer.gwt.voices.crowd.rebind.GwtUserAgentProviderGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;/*from w w  w  .  j  av a2s. co  m*/
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String userAgent;
        try {
            SelectionProperty userAgentProperty = propertyOracle.getSelectionProperty(logger,
                    PROPERTY_USER_AGENT);

            // ALWAYS RETURNS 'gecko'
            userAgent = userAgentProperty.getCurrentValue();
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_USER_AGENT + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("public String getGwtUserAgent() {");
        sw.indent();

        if (userAgent == null) {
            sw.println("return null;");
        } else {
            sw.println("return \"" + userAgent + "\";");
        }

        sw.outdent();
        sw.println("}\n");

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

private AssetFilter getAssetFilter(GeneratorContext context) {
    ConfigurationProperty assetFilterClassProperty = null;
    try {/*from w  ww.  j  av a2  s.  com*/
        assetFilterClassProperty = context.getPropertyOracle().getConfigurationProperty("gdx.assetfilterclass");
    } catch (BadPropertyValueException e) {
        return new DefaultAssetFilter();
    }
    if (assetFilterClassProperty.getValues().size() == 0) {
        return new DefaultAssetFilter();
    }
    String assetFilterClass = assetFilterClassProperty.getValues().get(0);
    if (assetFilterClass == null)
        return new DefaultAssetFilter();
    try {
        return (AssetFilter) Class.forName(assetFilterClass).newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Couldn't instantiate custom AssetFilter '" + assetFilterClass
                + "', make sure the class is public and has a public default constructor", e);
    }
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

private String getAssetPath(GeneratorContext context) {
    ConfigurationProperty assetPathProperty = null;
    try {//  w ww. ja v a2s.  c om
        assetPathProperty = context.getPropertyOracle().getConfigurationProperty("gdx.assetpath");
    } catch (BadPropertyValueException e) {
        throw new RuntimeException(
                "No gdx.assetpath defined. Add <set-configuration-property name=\"gdx.assetpath\" value=\"relative/path/to/assets/\"/> to your GWT projects gwt.xml file");
    }
    if (assetPathProperty.getValues().size() == 0) {
        throw new RuntimeException(
                "No gdx.assetpath defined. Add <set-configuration-property name=\"gdx.assetpath\" value=\"relative/path/to/assets/\"/> to your GWT projects gwt.xml file");
    }
    String paths = assetPathProperty.getValues().get(0);
    if (paths == null) {
        throw new RuntimeException(
                "No gdx.assetpath defined. Add <set-configuration-property name=\"gdx.assetpath\" value=\"relative/path/to/assets/\"/> to your GWT projects gwt.xml file");
    } else {
        ArrayList<String> existingPaths = new ArrayList<String>();
        String[] tokens = paths.split(",");
        for (String token : tokens) {
            System.out.println(token);
            if (new FileWrapper(token).exists() || new FileWrapper("../" + token).exists()) {
                return token;
            }
        }
        throw new RuntimeException(
                "No valid gdx.assetpath defined. Fix <set-configuration-property name=\"gdx.assetpath\" value=\"relative/path/to/assets/\"/> in your GWT projects gwt.xml file");
    }
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

private String getAssetOutputPath(GeneratorContext context) {
    ConfigurationProperty assetPathProperty = null;
    try {/*from   www . j a  v  a 2s  .c o  m*/
        assetPathProperty = context.getPropertyOracle().getConfigurationProperty("gdx.assetoutputpath");
    } catch (BadPropertyValueException e) {
        return null;
    }
    if (assetPathProperty.getValues().size() == 0) {
        return null;
    }
    String paths = assetPathProperty.getValues().get(0);
    if (paths == null) {
        return null;
    } else {
        ArrayList<String> existingPaths = new ArrayList<String>();
        String[] tokens = paths.split(",");
        String path = null;
        for (String token : tokens) {
            if (new FileWrapper(token).exists() || new FileWrapper(token).mkdirs()) {
                path = token;
            }
        }
        if (path != null && !path.endsWith("/")) {
            path += "/";
        }
        return path;
    }
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

private List<String> getClasspathFiles(GeneratorContext context) {
    List<String> classpathFiles = new ArrayList<String>();
    try {/*ww  w  .ja v a 2 s  .co m*/
        ConfigurationProperty prop = context.getPropertyOracle()
                .getConfigurationProperty("gdx.files.classpath");
        for (String value : prop.getValues()) {
            classpathFiles.add(value);
        }
    } catch (BadPropertyValueException e) {
        // Ignore
    }
    return classpathFiles;
}

From source file:com.bramosystems.oss.player.core.rebind.PlayerManagerGenerator.java

License:Apache License

/**
 * @param logger Logger object/*from  ww w  .  j  ava2 s  .c  om*/
 * @param context Generator context
 */
private void generateClass(TreeLogger logger, GeneratorContext context)
        throws NotFoundException, BadPropertyValueException, UnableToCompleteException, IOException {
    // get print writer that receives the source code
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);

    // print writer if null, source code has ALREADY been generated,  return
    if (printWriter == null) {
        return;
    }

    // build plugin mime types ...
    ConfigurationProperty mimeFile = context.getPropertyOracle()
            .getConfigurationProperty("bstplayer.media.mimeTypes");
    String val = mimeFile.getValues().get(0);
    if (val == null) {
        logger.log(TreeLogger.Type.INFO,
                "'" + mimeFile.getName() + "' configuration property not set! Using defaults");
        parseMimeFile(DEFAULT_MIME_TYPES_FILE);
    } else {
        logger.log(TreeLogger.Type.INFO, "'" + mimeFile.getName() + "' set! Using '" + val + "'");
        parseMimeFile(val);
    }

    collatePlayers(context.getTypeOracle());

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.setSuperclass("PlayerManager");
    composer.addImport("com.google.gwt.core.client.GWT");
    composer.addImport("com.bramosystems.oss.player.core.client.spi.PlayerProviderFactory");
    composer.addImport("com.bramosystems.oss.player.core.client.impl.CallbackUtility");
    composer.addImport("com.bramosystems.oss.player.core.client.spi.ConfigurationContext");
    composer.addImport("com.bramosystems.oss.player.core.client.*");
    composer.addImport("java.util.*");

    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    sourceWriter.println(
            "private HashMap<String, HashMap<String, PlayerInfo>> pInfos = new HashMap<String, HashMap<String, PlayerInfo>>();");

    // collate widget factories & create static holders ...
    Iterator<String> fact = pMap.keySet().iterator();
    while (fact.hasNext()) {
        String provClass = fact.next();
        sourceWriter.println("private static PlayerProviderFactory pwf_"
                + escapeProviderName(pMap.get(provClass).name) + " = GWT.create(" + provClass + ".class);");
    }
    sourceWriter.println();

    Pattern ptrn = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");

    // generate constructor source code
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();

    // init widget factories ...
    fact = pMap.keySet().iterator();
    while (fact.hasNext()) {
        String provClass = fact.next();
        String provName = escapeProviderName(pMap.get(provClass).name);
        sourceWriter.println(
                "pwf_" + provName + ".init(new ConfigurationContext(CallbackUtility.initCallbackHandlers(\""
                        + provName + "\"), \"bstplayer.handlers." + provName + "\"));");
    }

    sourceWriter.println();

    // init providers ...
    fact = pMap.keySet().iterator();
    while (fact.hasNext()) {
        _provider pvd = pMap.get(fact.next());
        sourceWriter.println("pInfos.put(\"" + pvd.name + "\", new HashMap<String, PlayerInfo>());");

        Iterator<_player> pns = pvd.players.iterator();
        while (pns.hasNext()) {
            _player ply = pns.next();
            boolean ps = false, ms = false;

            JClassType ints[] = ply.interfaces;
            for (int j = 0; j < ints.length; j++) {
                if (ints[j].getQualifiedSourceName().equals(MatrixSupport.class.getName())) {
                    ms = true;
                } else if (ints[j].getQualifiedSourceName().equals(PlaylistSupport.class.getName())) {
                    ps = true;
                }
            }
            Matcher m = ptrn.matcher(ply.minPluginVer);
            if (m.matches()) {
                sourceWriter.println("pInfos.get(\"" + pvd.name + "\").put(\"" + ply.name
                        + "\", new PlayerInfo(\"" + pvd.name + "\",\"" + ply.name + "\"," + "PluginVersion.get("
                        + Integer.parseInt(m.group(1)) + "," + Integer.parseInt(m.group(2)) + ","
                        + Integer.parseInt(m.group(3)) + ")," + ps + "," + ms + "));");
            } else {
                logger.log(TreeLogger.Type.WARN, "Min");
            }
        }
    }
    sourceWriter.outdent();
    sourceWriter.println("}"); // end constructor source generation

    sourceWriter.println();

    // implement get provider names ...
    sourceWriter.println("@Override");
    sourceWriter.println("public Set<String> getProviders(){");
    sourceWriter.indent();
    sourceWriter.println("return new TreeSet<String>(pInfos.keySet());");
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.println();

    // implement get player names by provider ...
    sourceWriter.println("@Override");
    sourceWriter.println("public Set<String> getPlayerNames(String providerName) {");
    sourceWriter.indent();
    sourceWriter.println("if(!pInfos.containsKey(providerName))");
    sourceWriter.println("throw new IllegalArgumentException(\"Unknown player provider - \" + providerName);");
    sourceWriter.println("return new TreeSet<String>(pInfos.get(providerName).keySet());");
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.println();

    // implement get player infos ...
    sourceWriter.println("@Override");
    sourceWriter.println("public PlayerInfo getPlayerInfo(String providerName, String playerName) {");
    sourceWriter.indent();
    sourceWriter.println("if(!pInfos.containsKey(providerName)) {");
    sourceWriter.println("throw new IllegalArgumentException(\"Unknown player provider - \" + providerName);}");
    sourceWriter.println("if(providerName.equals(\"api\")){");
    sourceWriter.println("return getApiPlayerInfoForPlugin(playerName);}");
    sourceWriter.println("else if(!pInfos.get(providerName).containsKey(playerName)){");
    sourceWriter.println("throw new IllegalArgumentException(\"Unknown player name - \" + playerName);}");
    sourceWriter.println("return pInfos.get(providerName).get(playerName);");
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.println();

    // implement get widget factory with defered binding on demand ....
    sourceWriter.println("@Override");
    sourceWriter.println("public PlayerProviderFactory getProviderFactory(String provider) {");
    sourceWriter.indent();
    sourceWriter.println("PlayerProviderFactory wf = null;");

    boolean firstRun = true;
    Iterator<_provider> provs = pMap.values().iterator();
    while (provs.hasNext()) {
        _provider prov = provs.next();
        if (firstRun) {
            sourceWriter.println("if(\"" + prov.name + "\".equals(provider)) {");
        } else {
            sourceWriter.println("else if(\"" + prov.name + "\".equals(provider)) {");
        }
        sourceWriter.indent();
        sourceWriter.println("wf = pwf_" + escapeProviderName(prov.name) + ";");
        sourceWriter.outdent();
        sourceWriter.println("}");
        firstRun = false;
    }
    sourceWriter.println("return wf;");
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.println();

    // implement mimeTypes ....
    sourceWriter.println("@Override");
    sourceWriter.println("protected void initMimeTypes(HashMap<String, String> mimeTypes) {");
    sourceWriter.indent();
    Iterator<String> mimeKeys = mimeMap.keySet().iterator();
    while (mimeKeys.hasNext()) {
        String mime = mimeKeys.next();
        sourceWriter.println("mimeTypes.put(\"" + mime + "\",\"" + mimeMap.get(mime) + "\");");
    }
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.println();

    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

    // commit generated class
    context.commit(logger, printWriter);
}

From source file:com.cgxlib.xq.rebind.BrowserGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    PropertyOracle propOracle = context.getPropertyOracle();

    String ua = null;//from  ww w  . ja  v  a 2s .  c  o  m
    try {
        ua = propOracle.getSelectionProperty(logger, "user.agent").getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(TreeLogger.ERROR, "Can not resolve user.agent property", e);
        throw new UnableToCompleteException();
    }

    JClassType clz = oracle.findType(typeName);
    String pName = clz.getPackage().getName();
    String cName = clz.getName() + "_" + ua;

    PrintWriter pWriter = context.tryCreate(logger, pName, cName);

    if (pWriter != null) {
        ClassSourceFileComposerFactory cFact = new ClassSourceFileComposerFactory(pName, cName);
        cFact.setSuperclass(pName + "." + clz.getName());

        SourceWriter writer = cFact.createSourceWriter(context, pWriter);

        writer.println("protected boolean isWebkit() {return " + "safari".equals(ua) + ";}");
        writer.println("protected boolean isSafari() {return " + "safari".equals(ua) + ";}");
        writer.println("protected boolean isOpera() {return " + "opera".equals(ua) + ";}");
        writer.println("protected boolean isMozilla() {return " + ua.contains("gecko") + ";}");
        writer.println("protected boolean isMsie() {return " + ua.contains("ie") + ";}");
        writer.println("protected boolean isIe6() {return " + "ie6".equals(ua) + ";}");
        writer.println("protected boolean isIe8() {return " + "ie8".equals(ua) + ";}");
        writer.println("protected boolean isIe9() {return " + "ie9".equals(ua) + ";}");
        writer.println("protected boolean isIe10() {return " + "ie10".equals(ua) + ";}");
        writer.println("protected boolean isIe11() {return " + "gecko1_8".equals(ua) + ";}");
        writer.println("public String toString() {return \"Browser:\"" + " + \" webkit=\" + webkit"
                + " + \" mozilla=\" + mozilla" + " + \" opera=\" + opera" + " + \" msie=\" + msie"
                + " + \" ie6=\" + ie6" + " + \" ie8=\" + ie8" + " + \" ie9=\" + ie9" + ";}");
        writer.commit(logger);
    }

    return pName + "." + cName;
}

From source file:com.em.validation.rebind.generator.gwt.GwtMessageGenerator.java

License:Apache License

/**
 * Get the locale that is currently being generated
 * <br/><br/>//ww w .  j a va2s.  c o  m
 * taken from http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/i18n/rebind/LocalizableGenerator.java
 * 
 * @param logger
 * @param context
 * @return
 */
public GwtLocale getCurrentGenerationLocale(TreeLogger logger, GeneratorContext context) {
    // Get the current locale
    PropertyOracle propertyOracle = context.getPropertyOracle();
    LocaleUtils localeUtils = LocaleUtils.getInstance(logger, propertyOracle, context);
    GwtLocale locale = localeUtils.getCompileLocale();
    return locale;
}