Example usage for com.google.gwt.user.rebind SourceWriter println

List of usage examples for com.google.gwt.user.rebind SourceWriter println

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter println.

Prototype

void println(String s);

Source Link

Usage

From source file:org.broadleafcommerce.openadmin.generator.i18nConstantsGenerator.java

License:Apache License

protected Map<String, String> generateDynamicConstantClasses(JClassType clazz, TreeLogger logger,
        GeneratorContext context) throws NotFoundException {
    ResourceOracle resourceOracle = context.getResourcesOracle();
    Map<String, String> generatedClassses = new HashMap<String, String>();
    String myTypeName = clazz.getQualifiedSourceName().replace('.', '/');
    Map<String, Resource> resourceMap = resourceOracle.getResourceMap();
    for (Map.Entry<String, Resource> entry : resourceMap.entrySet()) {
        if (entry.getKey().contains(myTypeName) && entry.getKey().endsWith(".properties")) {
            String noSuffix = entry.getKey().substring(0, entry.getKey().indexOf(".properties"));
            String position1 = null;
            String position2 = null;
            if (noSuffix.contains("_")) {
                String i18nMatch = noSuffix.substring(noSuffix.lastIndexOf("_") + 1, noSuffix.length());
                if (i18nMatch != null && i18nMatch.length() == 2) {
                    position1 = i18nMatch;
                    noSuffix = noSuffix.substring(0, noSuffix.lastIndexOf("_"));
                    if (noSuffix.contains("_")) {
                        i18nMatch = noSuffix.substring(noSuffix.lastIndexOf("_") + 1, noSuffix.length());
                        if (i18nMatch != null && i18nMatch.length() == 2) {
                            position2 = i18nMatch;
                        }//from w  w w . j  a va  2s  . co m
                    }
                }
            }
            String packageName = clazz.getPackage().getName();
            StringBuilder suffix = new StringBuilder();
            if (position1 != null) {
                suffix.append("_");
                suffix.append(position1);
            }
            if (position2 != null) {
                suffix.append("_");
                suffix.append(position2);
            }
            if (position1 == null && position2 == null) {
                suffix.append("_default");
            }
            String simpleName = clazz.getName() + suffix.toString();
            SourceWriter sourceWriter = getSourceWriter(packageName, simpleName, context, logger,
                    new String[] {});
            if (sourceWriter != null) {
                Map<String, String> props = new HashMap<String, String>();
                InputStream is = entry.getValue().openContents();
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    boolean eof = false;
                    while (!eof) {
                        String temp = in.readLine();
                        if (temp == null) {
                            eof = true;
                        } else {
                            temp = temp.trim();
                            if (!temp.startsWith("#") && temp.length() > 0 && temp.contains("=")) {
                                String key = temp.substring(0, temp.indexOf("=")).trim();
                                String value = temp.substring(temp.indexOf("=") + 1, temp.length()).trim();
                                props.put(key, value);
                            }
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        is.close();
                    } catch (Throwable ignored) {
                    }
                }

                logger.log(TreeLogger.INFO, "Emitting localized code for: " + entry.getKey(), null);
                sourceWriter.println(
                        "private java.util.Map<String, String> i18nProperties = new java.util.HashMap<String, String>();");
                sourceWriter.println("public " + simpleName + "() {");
                for (Map.Entry<String, String> prop : props.entrySet()) {
                    sourceWriter.print("i18nProperties.put(\"");
                    sourceWriter.print(prop.getKey());
                    sourceWriter.print("\",\"");
                    sourceWriter.print(prop.getValue().replace("\"", "\\\""));
                    sourceWriter.print("\");\n");
                }
                sourceWriter.println("}");
                sourceWriter.println("");
                sourceWriter.println("public java.util.Map<String, String> getAlli18nProperties() {");
                sourceWriter.println("return i18nProperties;");
                sourceWriter.println("}");
                sourceWriter.commit(logger);
                logger.log(TreeLogger.INFO, "Done Generating source for " + packageName + "." + simpleName,
                        null);

                generatedClassses.put(suffix.toString().substring(1, suffix.toString().length()),
                        packageName + "." + simpleName);
            }
        }
    }

    return generatedClassses;
}

From source file:org.chromium.distiller.rebind.JsTestEntryGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typename)
        throws UnableToCompleteException {
    String packageName = "org.chromium.distiller";
    String outputClassname = "JsTestBuilderImpl";

    List<TestCase> testCases = getTestCases(logger, context);

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, outputClassname);
    composer.addImplementedInterface("org.chromium.distiller.JsTestSuiteBuilder");
    PrintWriter printWriter = context.tryCreate(logger, packageName, outputClassname);
    if (printWriter != null) {
        for (TestCase ts : testCases) {
            String className = ts.classType.getName();
            String qualifiedClassName = ts.classType.getPackage().getName() + "." + className;
            composer.addImport(qualifiedClassName);
        }// www . j  ava 2 s.c  o  m
        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
        sourceWriter.println("JsTestBuilderImpl () {");
        sourceWriter.println("}");
        sourceWriter.println("public JsTestSuiteBase build() {");
        sourceWriter.indent();
        sourceWriter.println("JsTestSuiteBase testSuite = new JsTestSuiteBase();");
        for (TestCase ts : testCases) {
            String className = ts.classType.getName();
            String qualifiedClassName = ts.classType.getPackage().getName() + "." + className;
            sourceWriter.println("testSuite.addTestCase(");
            sourceWriter.println("        new JsTestSuiteBase.TestCaseFactory() {");
            sourceWriter.println("            @Override");
            sourceWriter.println("            public JsTestCase build() {");
            sourceWriter.println("                return new " + className + "();");
            sourceWriter.println("            }");
            sourceWriter.println("        }, \"" + qualifiedClassName + "\")");
            sourceWriter.indent();
            for (JMethod test : ts.tests) {
                String methodName = test.getName();
                sourceWriter.println(".addTest(");
                sourceWriter.println("        new JsTestSuiteBase.TestCaseRunner() {");
                sourceWriter.println("            @Override");
                sourceWriter.println("            public void run(JsTestCase testCase) throws Throwable {");
                sourceWriter.println("                ((" + className + ")testCase)." + methodName + "();");
                sourceWriter.println("            }");
                sourceWriter.println("        }, \"" + methodName + "\")");
            }
            sourceWriter.println(";");
            sourceWriter.outdent();
        }
        sourceWriter.println("return testSuite;");
        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.commit(logger);
    }
    return composer.getCreatedClassName();
}

From source file:org.cruxframework.crux.core.rebind.crossdevice.DeviceFeaturesPropertyGenerator.java

License:Apache License

static void writeDeviceFeaturesPropertyJavaScript(SourceWriter body) {
    body.println("var ua = navigator.userAgent.toLowerCase();");
    body.println("var supportsTouch = ('ontouchstart' in window);");

    body.println("if ((ua.indexOf('googletv') != -1) || (ua.indexOf('ipad;') != -1)){");
    body.indent();/*from  ww  w.  j a  va2s .  c om*/
    body.println("return 'largeDisplay'+(supportsTouch?'Touch':'Arrows');");
    body.outdent();
    body.println(
            "} else if ((ua.indexOf('opera mini') != -1)  || (ua.indexOf('opera mobi') != -1) || (ua.indexOf('mobile') != -1) || "
                    + "(ua.indexOf('iphone') != -1) || (ua.indexOf('ipod;') != -1)){");
    body.indent();
    body.println("return 'smallDisplay'+(supportsTouch?'Touch':'Arrows');");
    body.outdent();
    body.println("} else if (ua.indexOf('android') != -1){");
    body.indent();
    body.println("return 'largeDisplay'+(supportsTouch?'Touch':'Arrows');");
    body.outdent();
    body.println("} else {");
    body.indent();
    body.println("return 'largeDisplay'+(supportsTouch?'Touch':'Mouse');");
    body.outdent();
    body.println("}");
}

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * /* w w w  . ja  v  a 2 s  . co  m*/
 * @param parameter
 * @param methodDescVar 
 * @param blocksScreen 
 */
private void generateAsyncCallbackForSyncTokenMethod(SourceWriter srcWriter, JParameter parameter,
        String methodDescVar, boolean blocksScreen) {
    JParameterizedType parameterizedType = parameter.getType().isParameterized();
    String typeSourceName = parameterizedType.getParameterizedQualifiedSourceName();
    JClassType[] typeArgs = parameterizedType.getTypeArgs();

    String typeParameterSourceName = typeArgs[0].getParameterizedQualifiedSourceName();

    srcWriter.println("new " + typeSourceName + "(){");
    srcWriter.indent();

    srcWriter.println("public void onSuccess(" + typeParameterSourceName + " result){");
    srcWriter.indent();
    srcWriter.println("try{");
    srcWriter.println(parameter.getName() + ".onSuccess(result);");
    srcWriter.println("}finally{");
    srcWriter.println("__endMethodCall(" + methodDescVar + ", " + blocksScreen + ");");
    srcWriter.println("}");
    srcWriter.outdent();
    srcWriter.println("}");

    srcWriter.println("public void onFailure(Throwable caught){");
    srcWriter.indent();
    srcWriter.println("try{");
    srcWriter.println(parameter.getName() + ".onFailure(caught);");
    srcWriter.println("}finally{");
    srcWriter.println("__endMethodCall(" + methodDescVar + ", " + blocksScreen + ");");
    srcWriter.println("}");
    srcWriter.outdent();
    srcWriter.println("}");

    srcWriter.outdent();
    srcWriter.print("}");
}

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * @param srcWriter/*from w ww  . j a v  a2 s  .  c o m*/
 * @param asyncMethod
 * @param parameters
 * @param methodDescVar
 */
private void generateProxyMethodCall(SourceWriter srcWriter, JMethod asyncMethod, List<JParameter> parameters,
        String methodDescVar, boolean blocksScreen) {

    srcWriter.print(getProxyWrapperQualifiedName() + ".super." + asyncMethod.getName() + "(");
    boolean needsComma = false;
    for (int i = 0; i < parameters.size(); ++i) {
        JParameter parameter = parameters.get(i);
        if (needsComma) {
            srcWriter.print(", ");
        }
        needsComma = true;
        if (i < (parameters.size() - 1)) {
            srcWriter.print(parameter.getName());
        } else {
            generateAsyncCallbackForSyncTokenMethod(srcWriter, parameter, methodDescVar, blocksScreen);
        }
    }
    srcWriter.println(");");
}

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * @param srcWriter// w w  w.  j  av  a 2 s  . co  m
 * @param asyncMethod
 * @throws UnableToCompleteException 
 */
private void generateProxyWrapperMethod(SourceWriter srcWriter, JMethod asyncMethod)
        throws UnableToCompleteException {
    try {
        JMethod syncMethod = getSyncMethodFromAsync(asyncMethod);

        if (syncMethod.getAnnotation(UseSynchronizerToken.class) != null) {
            JType asyncReturnType = asyncMethod.getReturnType().getErasedType();
            List<JParameter> parameters = generateProxyWrapperMethodDeclaration(srcWriter, asyncMethod,
                    asyncReturnType);

            generateProxyWrapperMethodCall(srcWriter, syncMethod, asyncMethod, asyncReturnType, parameters);

            srcWriter.outdent();
            srcWriter.println("}");
        }
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "No method found on service interface that matches the async method ["
                + asyncMethod.getName() + "].");
    }
}

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * @param srcWriter/*from  www.  ja v  a2 s .  com*/
 * @param asyncMethod
 * @param asyncReturnType
 * @param parameters
 * @throws UnableToCompleteException 
 */
private void generateProxyWrapperMethodCall(SourceWriter srcWriter, JMethod syncMethod, JMethod asyncMethod,
        JType asyncReturnType, List<JParameter> parameters) throws UnableToCompleteException {
    if (asyncReturnType != JPrimitiveType.VOID) {
        logger.log(TreeLogger.ERROR,
                "UseSynchronizer Token only can be used with void return type on Async interface.");
        throw new UnableToCompleteException();
    }
    UseSynchronizerToken synchronizerTokenAnnot = syncMethod.getAnnotation(UseSynchronizerToken.class);
    boolean blocksScreen = synchronizerTokenAnnot.blocksUserInteraction();
    JParameter parameter = parameters.get(parameters.size() - 1);

    srcWriter.println("final String methodDesc = \"" + JClassUtils.getMethodDescription(syncMethod) + "\";");
    srcWriter.println("if (__startMethodCall(methodDesc, " + blocksScreen + ")){");
    srcWriter.indent();

    srcWriter.println("__syncTokenService.getSynchronizerToken(methodDesc,");
    srcWriter.println("new AsyncCallback<String>(){");
    srcWriter.indent();

    srcWriter.println("public void onSuccess(String result){");
    srcWriter.indent();
    srcWriter.println("__updateMethodToken(methodDesc, result);");
    generateProxyMethodCall(srcWriter, asyncMethod, parameters, "methodDesc", blocksScreen);
    srcWriter.outdent();
    srcWriter.println("}");

    srcWriter.println("public void onFailure(Throwable caught){");
    srcWriter.indent();
    srcWriter.println("try{");
    srcWriter.println(parameter.getName() + ".onFailure(caught);");
    srcWriter.println("}finally{");
    srcWriter.println("__endMethodCall(methodDesc, " + blocksScreen + ");");
    srcWriter.println("}");
    srcWriter.outdent();
    srcWriter.println("}");

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

    srcWriter.outdent();
    srcWriter.println("}");
    if (synchronizerTokenAnnot.notifyCallsWhenProcessing()) {
        srcWriter.println("else{");
        srcWriter.indent();

        String sensitiveErrMsg = Crux.class.getName() + ".getMessages().methodIsAlreadyBeingProcessed()";
        srcWriter.println(Crux.class.getName() + ".getErrorHandler().handleError(" + sensitiveErrMsg + ", new "
                + SensitiveMethodAlreadyBeingProcessedException.class.getName() + "(" + sensitiveErrMsg + ")"
                + ");");

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

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * @param srcWriter/*from www.ja  v a2 s  .  c o  m*/
 */
private void generateWrapperProxyContructor(SourceWriter srcWriter) {
    srcWriter.println("public " + getProxyWrapperSimpleName() + "() {");
    srcWriter.indent();

    srcWriter.println("super();");
    srcWriter.println(
            "this.__hasParameters = (getServiceEntryPoint()!=null?getServiceEntryPoint().indexOf('?')>0:false);");
    if (this.hasSyncTokenMethod) {
        srcWriter.println("this.__baseEntrypoint = getServiceEntryPoint();");
        srcWriter.println(
                "this.__syncTokenService = (CruxSynchronizerTokenServiceAsync)GWT.create(CruxSynchronizerTokenService.class);");
    }
    srcWriter.println("String locale = Screen.getLocale();");
    srcWriter.println("if (locale != null && locale.trim().length() > 0){");
    srcWriter.indent();
    srcWriter.println("if (this.__hasParameters){");
    srcWriter.indent();
    srcWriter.println("setServiceEntryPoint(getServiceEntryPoint() + \"&locale=\" + locale);");
    srcWriter.outdent();
    srcWriter.println("}else{");
    srcWriter.indent();
    srcWriter.println("setServiceEntryPoint(getServiceEntryPoint() + \"?locale=\" + locale);");
    srcWriter.println("this.__hasParameters = true;");
    srcWriter.outdent();
    srcWriter.println("}");
    srcWriter.outdent();
    srcWriter.println("}");

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

From source file:org.cruxframework.crux.core.rebind.rpc.CruxProxyCreator.java

License:Apache License

/**
 * @param srcWriter/*from  w ww  . j  a  v  a  2  s  .co m*/
 * @param asyncServiceInterfaceName 
 */
private void generateWrapperProxyFields(SourceWriter srcWriter, String asyncServiceInterfaceName) {
    srcWriter.println("private boolean __hasParameters = false;");
    if (this.hasSyncTokenMethod) {
        srcWriter.println(
                "private Map<String, Boolean> __syncProcessingMethods = new HashMap<String, Boolean>();");
        srcWriter.println("private CruxSynchronizerTokenServiceAsync __syncTokenService;");
        srcWriter.println("private String __baseEntrypoint;");
    }
}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

/**
 *  Create a custom font-resource to inject at a stylesheet
 *  @param logger/* ww  w  . j  a v  a 2  s.co m*/
 *  @param context
 *  @param method
 */
@Override
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method)
        throws UnableToCompleteException {
    SourceWriter sw = new StringSourceWriter();

    sw.print("new ");
    sw.print(method.getReturnType().getQualifiedSourceName());
    sw.println("() {");
    sw.indent();

    writeGetFontName(method, sw);
    writeGetName(method, sw);
    writeEnsureInjected(sw);
    writeGetText(logger, context, method, sw);

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