Example usage for com.google.gwt.dev.resource ResourceOracle getResourceMap

List of usage examples for com.google.gwt.dev.resource ResourceOracle getResourceMap

Introduction

In this page you can find the example usage for com.google.gwt.dev.resource ResourceOracle getResourceMap.

Prototype

Map<String, Resource> getResourceMap();

Source Link

Document

Returns an unmodifiable map of abstract path name to resource.

Usage

From source file:fr.onevu.gwt.uibinder.rebind.UiBinderGenerator.java

License:Apache License

private Document getW3cDoc(MortalLogger logger, DesignTimeUtils designTime, ResourceOracle resourceOracle,
        String templatePath) throws UnableToCompleteException {

    Resource resource = resourceOracle.getResourceMap().get(templatePath);
    if (null == resource) {
        logger.die("Unable to find resource: " + templatePath);
    }/*from   w  ww.j  a  v a2s  .c o  m*/

    Document doc = null;
    try {
        String content = designTime.getTemplateContent(templatePath);
        if (content == null) {
            content = Util.readStreamAsString(resource.openContents());
        }
        doc = new W3cDomHelper(logger.getTreeLogger(), resourceOracle).documentFor(content, resource.getPath());
    } catch (IOException iex) {
        logger.die("Error opening resource:" + resource.getLocation(), iex);
    } catch (SAXParseException e) {
        logger.die("Error parsing XML (line " + e.getLineNumber() + "): " + e.getMessage(), e);
    }
    return doc;
}

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;
                        }//  w  w  w . j  av  a 2  s. com
                    }
                }
            }
            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;
}