List of usage examples for com.google.gwt.dev.util Util readStreamAsString
public static String readStreamAsString(InputStream in)
From source file:com.ait.ext4j.rebind.TemplateGenerator.java
License:Apache License
/** * Given a TemplateResource interface, return the path to its .html file, * suitable for any classloader to find it as a resource. If the .html does * not exist or is empty see if the template was declared inline. If no * content is found we throw an exception. *//* w w w . j av a2 s.c om*/ private String getTemplateContent(GeneratorContext context, TreeLogger logger, JClassType interfaceType) throws UnableToCompleteException { String templateHtmlFile = null; TemplateResource annotation = interfaceType.getAnnotation(TemplateResource.class); if (annotation == null) { // if the interface is defined as a nested class, use the name of // the // enclosing type if (interfaceType.getEnclosingType() != null) { interfaceType = interfaceType.getEnclosingType(); } templateHtmlFile = slashify(interfaceType.getQualifiedBinaryName()) + TEMPLATE_SUFFIX; logger.log(TreeLogger.INFO, "Template : " + templateHtmlFile); InputStream stream = getTemplateResource(context, logger, templateHtmlFile); if (stream == null) { logger.log(Type.ERROR, "No data could be loaded - no data at path " + interfaceType.getQualifiedBinaryName() + TEMPLATE_SUFFIX); throw new UnableToCompleteException(); } return sanitize(Util.readStreamAsString(stream)); } else { // first we look at the HTML File templateHtmlFile = annotation.source(); if (templateHtmlFile.length() > 0) { if (!templateHtmlFile.endsWith(TEMPLATE_SUFFIX)) { logger.log(TreeLogger.ERROR, "Template file name must end with " + TEMPLATE_SUFFIX); throw new UnableToCompleteException(); } if (annotation.value().length() != 0) { logger.log(Type.WARN, "Found both source file and inline template, using source file"); } templateHtmlFile = slashify(interfaceType.getPackage().getName()) + "/" + templateHtmlFile; InputStream stream = getTemplateResource(context, logger, templateHtmlFile); return sanitize(Util.readStreamAsString(stream)); } else if (annotation.value().length() > 0) { return annotation.value(); } else { logger.log(Type.ERROR, "Template annotation found with no contents, cannot generate method , this may cause other failures."); } } return null; }
From source file:com.sencha.gxt.core.rebind.XTemplatesGenerator.java
License:sencha.com license
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { // make sure it is an interface TypeOracle oracle = context.getTypeOracle(); this.logger = logger; this.xTemplatesInterface = oracle.findType(Name.getSourceNameForClass(XTemplates.class)); this.listInterface = oracle.findType(Name.getSourceNameForClass(List.class)); JClassType toGenerate = oracle.findType(typeName).isInterface(); if (toGenerate == null) { logger.log(TreeLogger.ERROR, typeName + " is not an interface type"); throw new UnableToCompleteException(); }/* w ww . j a v a2 s.c o m*/ if (!toGenerate.isAssignableTo(xTemplatesInterface)) { logger.log(Type.ERROR, "This isn't a XTemplates subtype..."); throw new UnableToCompleteException(); } // Get the name of the new type String packageName = toGenerate.getPackage().getName(); String simpleSourceName = toGenerate.getName().replace('.', '_') + "Impl"; PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName); if (pw == null) { return packageName + "." + simpleSourceName; } ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName); factory.addImplementedInterface(typeName); // imports factory.addImport(Name.getSourceNameForClass(GWT.class)); factory.addImport(Name.getSourceNameForClass(SafeHtml.class)); factory.addImport(Name.getSourceNameForClass(SafeHtmlBuilder.class)); // Loop through the formatters declared for this type and supertypes FormatCollector formatters = new FormatCollector(context, logger, toGenerate); MethodCollector invokables = new MethodCollector(context, logger, toGenerate); SourceWriter sw = factory.createSourceWriter(context, pw); for (JMethod method : toGenerate.getOverridableMethods()) { TreeLogger l = logger.branch(Type.DEBUG, "Creating XTemplate method " + method.getName()); final String template; XTemplate marker = method.getAnnotation(XTemplate.class); if (marker == null) { l.log(Type.ERROR, "Unable to create template for method " + method.getReadableDeclaration() + ", this may cause other failures."); continue; } else { if (marker.source().length() != 0) { if (marker.value().length() != 0) { l.log(Type.WARN, "Found both source file and inline template, using source file"); } InputStream stream = getTemplateResource(context, method.getEnclosingType(), l, marker.source()); if (stream == null) { l.log(Type.ERROR, "No data could be loaded - no data at path " + marker.source()); throw new UnableToCompleteException(); } template = Util.readStreamAsString(stream); } else if (marker.value().length() != 0) { template = marker.value(); } else { l.log(Type.ERROR, "XTemplate annotation found with no contents, cannot generate method " + method.getName() + ", this may cause other failures."); continue; } } XTemplateParser p = new XTemplateParser( l.branch(Type.DEBUG, "Parsing provided template for " + method.getReadableDeclaration())); TemplateModel m = p.parse(template); SafeHtmlTemplatesCreator safeHtml = new SafeHtmlTemplatesCreator(context, l.branch(Type.DEBUG, "Building SafeHtmlTemplates"), method); sw.println(method.getReadableDeclaration(false, true, true, false, true) + "{"); sw.indent(); Map<String, JType> params = new HashMap<String, JType>(); for (JParameter param : method.getParameters()) { params.put(param.getName(), param.getType()); } Context scopeContext = new Context(context, l, params, formatters); // if there is only one parameter, wrap the scope up so that properties // can be accessed directly if (method.getParameters().length == 1) { JParameter param = method.getParameters()[0]; scopeContext = new Context(scopeContext, param.getName(), param.getType()); } String outerSHVar = scopeContext.declareLocalVariable("outer"); sw.println("SafeHtml %1$s;", outerSHVar); buildSafeHtmlTemplates(outerSHVar, sw, m, safeHtml, scopeContext, invokables); sw.println("return %1$s;", outerSHVar); sw.outdent(); sw.println("}"); safeHtml.create(); } // Save the file and return its type name sw.commit(logger); return factory.getCreatedClassName(); }
From source file:fr.onevu.gwt.uibinder.rebind.GwtResourceEntityResolver.java
License:Apache License
@Override public InputSource resolveEntity(String publicId, String systemId) { String matchingPrefix = findMatchingPrefix(systemId); Resource resource = null;// ww w . ja v a 2 s.c o m Map<String, Resource> map = resourceOracle.getResourceMap(); if (matchingPrefix != null) { resource = map.get(RESOURCES + systemId.substring(matchingPrefix.length())); } if (resource == null) { resource = map.get(pathBase + systemId); } if (resource != null) { String content; try { InputStream resourceStream = resource.openContents(); content = Util.readStreamAsString(resourceStream); } catch (IOException ex) { logger.log(TreeLogger.ERROR, "Error reading resource: " + resource.getLocation()); throw new RuntimeException(ex); } InputSource inputSource = new InputSource(new StringReader(content)); inputSource.setPublicId(publicId); inputSource.setSystemId(resource.getPath()); return inputSource; } /* * Let Sax find it on the interweb. */ return null; }
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 w w .j a v a 2 s . 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:sk.turn.gwtmvp.gen.ViewGenerator.java
License:Apache License
@Override public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { try {//from ww w . j a v a 2 s.c om TypeOracle typeOracle = context.getTypeOracle(); JClassType viewType = typeOracle.getType(typeName); boolean asyncView = (viewType.getAnnotation(AsyncView.class) != null); ViewHtml viewHtml = viewType.getAnnotation(ViewHtml.class); String packageName = viewType.getPackage().getName(); String generatedClassName = viewType.getName().replace('.', '_') + "Impl"; PrintWriter w = context.tryCreate(logger, packageName, generatedClassName); if (w == null) { return new RebindResult(RebindMode.USE_ALL_CACHED, packageName + "." + generatedClassName); } JParameterizedType superType = null; String handlerType = null; for (JClassType intfc : viewType.getImplementedInterfaces()) { if (intfc.getQualifiedSourceName().equals(View.class.getName()) || intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) { superType = intfc.isParameterized(); if (intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) { handlerType = superType.getTypeArgs()[1].getQualifiedSourceName(); } break; } } if (superType == null) { throw new Exception(typeName + " does not inherit from " + View.class.getName()); } // If the view HTML is not inline, make sure the view HTML file exists Resource htmlResource = null; if (viewHtml == null) { htmlResource = context.getResourcesOracle().getResource( packageName.replace('.', '/') + "/" + viewType.getSimpleSourceName() + ".html"); if (htmlResource == null) { throw new Exception("Cannot find " + viewType.getSimpleSourceName() + ".html"); } } String rootElementType = superType.getTypeArgs()[0].getQualifiedSourceName(); // Map all annotated methods Map<String, JMethod> fieldsMap = new LinkedHashMap<>(); // elemId -> { com.google.gwt.event.dom.client.???Handler -> JMethod, ... }, ... Map<String, Map<String, JMethod>> handlersMap = new LinkedHashMap<>(); for (JMethod method : viewType.getMethods()) { // Check element mapping HtmlElement elemAnn = method.getAnnotation(HtmlElement.class); if (elemAnn != null) { if (method.getParameters().length > 0) { throw new Exception("Method " + typeName + "." + method.getName() + "() must have zero parameters (has " + method.getParameters().length + ")."); } String id = elemAnn.value(); if (id.equals("")) { id = method.getName(); if (id.startsWith("get") && id.length() > 3) { id = id.substring(3, 4).toLowerCase() + id.substring(4); } } fieldsMap.put(id, method); } // Check handler mapping HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class); if (handlerAnn != null) { JParameter param = (method.getParameters().length == 1 ? method.getParameters()[0] : null); JClassType paramType = (param != null ? param.getType().isInterface() : null); if (paramType == null || !paramType.isAssignableTo(typeOracle.getType(EventHandler.class.getName()))) { throw new Exception("Method " + typeName + "." + method.getName() + "() must have exactly one parameter of an EventHandler subinterface (has " + (paramType == null ? "none" : paramType.getQualifiedSourceName()) + ")."); } for (String val : handlerAnn.value()) { Map<String, JMethod> methods = handlersMap.get(val); if (methods == null) { methods = new LinkedHashMap<>(); handlersMap.put(val, methods); } if (methods.containsKey(paramType.getQualifiedSourceName())) { throw new Exception( "Element \"" + val + "\" already has a " + paramType.getName() + " defined."); } methods.put(paramType.getQualifiedSourceName(), method); } } } // Check whether dictionary class is defined String html = (viewHtml != null ? viewHtml.value() : Util.readStreamAsString(htmlResource.openContents())); Matcher dictMatcher = Pattern.compile("^<[^<]+data-mvp-dict=\"(.+)\"").matcher(html); String dictClassName = null; JClassType dictClass = null; if (dictMatcher.find()) { dictClassName = dictMatcher.group(1); if ((dictClass = typeOracle.getType(dictClassName)) == null) { throw new Exception("Localization classs " + dictClassName + " does not exist"); } } // Check whether data-gwtid is still being used if (html.indexOf("data-gwtid=\"") != -1) { logger.log(TreeLogger.Type.WARN, "The use of \"data-gwtid\" attribute is deprecated and its support will be removed in future versions, please use \"data-mvp-id\" instead."); } w.println("package " + packageName + ";"); w.println(); w.println("import com.google.gwt.core.client.GWT;"); w.println("import com.google.gwt.dom.client.Element;"); w.println("import com.google.gwt.dom.client.Document;"); w.println("import com.google.gwt.dom.client.NodeList;"); w.println("import com.google.gwt.event.dom.client.DomEvent;"); w.println("import com.google.gwt.event.shared.HandlerManager;"); w.println("import com.google.gwt.resources.client.ClientBundle;"); w.println("import com.google.gwt.resources.client.ExternalTextResource;"); w.println("import com.google.gwt.resources.client.ResourceCallback;"); w.println("import com.google.gwt.resources.client.ResourceException;"); w.println("import com.google.gwt.resources.client.TextResource;"); w.println("import com.google.gwt.user.client.Event;"); w.println("import com.google.gwt.user.client.EventListener;"); w.println("import java.util.HashMap;"); w.println("import java.util.Map;"); w.println("import java.util.logging.Logger;"); w.println(); w.println("public class " + generatedClassName + " implements " + viewType.getQualifiedSourceName() + " {"); if (htmlResource != null) { w.println(" interface Resources extends ClientBundle {"); w.println(" Resources INSTANCE = GWT.create(Resources.class);"); w.println(" @Source(\"" + viewType.getSimpleSourceName() + ".html\") " + (asyncView ? "External" : "") + "TextResource htmlContent();"); w.println(" }"); w.println(); } w.println(" private static final Logger LOG = Logger.getLogger(\"" + packageName + "." + generatedClassName + "\");"); if (asyncView) { w.println(" private static String sHtml = null;"); } w.println(); w.println(" private " + rootElementType + " rootElement = null;"); w.println(" private final Map<String, Element> elementsMap = new HashMap<>();"); if (handlerType != null) { w.println(" private " + handlerType + " handler;"); } w.println(); w.println(" @Override"); w.println( " public void loadView(final ViewLoadedHandler<" + rootElementType + "> viewLoadedHandler) {"); w.println(" if (rootElement != null) {"); w.println(" viewLoadedHandler.onViewLoaded(rootElement);"); w.println(" }"); if (asyncView) { w.println(" if (sHtml != null) {"); w.println(" loadView(new String(sHtml), viewLoadedHandler);"); w.println(" } else {"); w.println(" try {"); w.println( " Resources.INSTANCE.htmlContent().getText(new ResourceCallback<TextResource>() {"); w.println(" public void onSuccess(TextResource r) {"); w.println(" sHtml = r.getText();"); w.println(" loadView(new String(sHtml), viewLoadedHandler);"); w.println(" }"); w.println(" public void onError(ResourceException e) {"); w.println(" LOG.severe(\"Failed to load " + viewType.getSimpleSourceName() + ".html: \" + e);"); w.println(" viewLoadedHandler.onViewLoaded(null);"); w.println(" }"); w.println(" });"); w.println(" } catch (ResourceException e) {"); w.println(" LOG.severe(\"Failed to load " + viewType.getSimpleSourceName() + ".html: \" + e);"); w.println(" viewLoadedHandler.onViewLoaded(null);"); w.println(" }"); w.println(" }"); } else if (viewHtml != null) { w.println(" loadView(\"" + escapeJavaString(viewHtml.value()) + "\", viewLoadedHandler);"); } else { w.println(" loadView(Resources.INSTANCE.htmlContent().getText(), viewLoadedHandler);"); } w.println(" }"); w.println(); w.println(" private void loadView(String html, ViewLoadedHandler<" + rootElementType + "> viewLoadedHandler) {"); // Replace any dictionary entries new SafeHtmlBuilder(); if (dictClassName != null) { w.println(" " + dictClassName + " dict = GWT.create(" + dictClassName + ".class);"); w.println(" Object dictEntry;"); dictMatcher = Pattern.compile("\\{mvpDict\\.([^}]+)\\}").matcher(html); Set<String> replacedEntries = new HashSet<>(); while (dictMatcher.find()) { String dictEntry = dictMatcher.group(1); if (!replacedEntries.contains(dictEntry)) { try { dictClass.getMethod(dictEntry, new JType[] {}); } catch (NotFoundException e) { throw new Exception("Localization method " + dictClassName + "." + dictEntry + "() does not exist."); } w.println(" dictEntry = dict." + dictEntry + "();"); w.println(" html = html.replace(\"" + dictMatcher.group(0) + "\", dictEntry instanceof com.google.gwt.safehtml.shared.SafeHtml ? ((com.google.gwt.safehtml.shared.SafeHtml)dictEntry).asString() : dictEntry.toString());"); replacedEntries.add(dictEntry); } } } w.println(" Element tempElem = Document.get().create" + (rootElementType.equals("com.google.gwt.dom.client.TableRowElement") ? "TBody" : rootElementType.equals("com.google.gwt.dom.client.TableCellElement") ? "TR" : "Div") + "Element();"); w.println(" tempElem.setInnerHTML(html);"); w.println(" rootElement = (" + rootElementType + ") tempElem.getFirstChild();"); if (dictClassName != null) { w.println(" rootElement.removeAttribute(\"data-mvp-dict\");"); } w.println(" addElementToMap(rootElement, elementsMap);"); w.println(" NodeList<Element> elements = rootElement.getElementsByTagName(\"*\");"); w.println(" for (int i = 0; i < elements.getLength(); i++) {"); w.println(" addElementToMap(elements.getItem(i), elementsMap);"); w.println(" }"); for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) { w.println(" if (elementsMap.get(\"" + entry.getKey() + "\") == null) {"); w.println(" LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey() + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");"); w.println(" }"); } for (Map.Entry<String, Map<String, JMethod>> entry : handlersMap.entrySet()) { w.println(" if (elementsMap.get(\"" + entry.getKey() + "\") == null) {"); w.println(" LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey() + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");"); w.println(" }"); } if (handlerType != null) { // Map @HtmlHandlers of enclosing class JClassType enclosingType = viewType.getEnclosingType(); JMethod[] methods = (enclosingType != null ? enclosingType.getMethods() : new JMethod[] {}); for (JMethod method : methods) { HtmlHandler handlerAnnotation = method.getAnnotation(HtmlHandler.class); if (handlerAnnotation == null) { continue; } String eventType = method.getParameters()[0].getType().getQualifiedSourceName(); String eventHandlerType = eventType.substring(0, eventType.length() - 5) + "Handler"; String handlerMethod = "on" + eventType.substring(0, eventType.length() - 5) .substring(eventType.lastIndexOf('.') + 1); for (String elemId : handlerAnnotation.value()) { w.println(" sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + elemId + "\"), " + eventType + ".getType(), new " + eventHandlerType + "() {"); w.println(" @Override"); w.println(" public void " + handlerMethod + "(" + eventType + " event) {"); w.println(" if (handler != null) {"); w.println(" try { handler." + method.getName() + "(event); }"); w.println(" catch (Exception e) { LOG.severe(\"Invoke of " + enclosingType.getName() + "." + method.getName() + " failed: \" + e); }"); w.println(" } else {"); w.println(" LOG.severe(\"Ignoring " + enclosingType.getName() + "." + method.getName() + " - no HandlerView.handler set\");"); w.println(" }"); w.println(" }"); w.println(" });"); } } } w.println(" viewLoadedHandler.onViewLoaded(rootElement);"); w.println(" }"); w.println(); w.println(" @Override"); w.println(" public " + rootElementType + " getRootElement() {"); w.println(" return rootElement;"); w.println(" }"); w.println(); w.println(" @Override"); w.println(" public <E2 extends Element> E2 getElement(String mvpId) {"); w.println(" return (E2) elementsMap.get(mvpId);"); w.println(" }"); if (handlerType != null) { w.println(); w.println(" @Override"); w.println(" public void setHandler(" + handlerType + " handler) {"); w.println(" this.handler = handler;"); w.println(" }"); } for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) { w.println(); w.println(" @Override"); w.println(" public " + entry.getValue().getReturnType().getQualifiedSourceName() + " " + entry.getValue().getName() + "() {"); w.println(" return elementsMap.get(\"" + entry.getKey() + "\").<" + entry.getValue().getReturnType().getQualifiedSourceName() + ">cast();"); w.println(" }"); } for (JMethod method : viewType.getMethods()) { HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class); if (handlerAnn == null) { continue; } String paramType = method.getParameters()[0].getType().getQualifiedSourceName(); String eventType = paramType.substring(0, paramType.length() - 7) + "Event.getType()"; w.println(); w.println(" @Override"); w.println(" public void " + method.getName() + "(" + paramType + " handler) {"); for (String id : handlerAnn.value()) { w.println(" sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + id + "\"), " + eventType + ", handler);"); } w.println(" }"); } w.println(); w.println(" private void addElementToMap(Element element, Map<String, Element> elementsMap) {"); w.println( " String attrName = (element.hasAttribute(\"data-mvp-id\") ? \"data-mvp-id\" : element.hasAttribute(\"data-gwtid\") ? \"data-gwtid\" : null);"); w.println(" if (attrName == null) {"); w.println(" return;"); w.println(" }"); w.println(" String mvpId = element.getAttribute(attrName);"); w.println(" if (!mvpId.equals(\"\")) {"); w.println(" element.removeAttribute(attrName);"); w.println(" elementsMap.put(mvpId, element);"); w.println(" }"); w.println(" }"); w.println("}"); context.commit(logger, w); return new RebindResult(RebindMode.USE_ALL_NEW, packageName + "." + generatedClassName); } catch (Exception e) { logger.log(TreeLogger.Type.ERROR, "Failed generating wrapper for class " + typeName + ": " + e.getMessage()); throw new UnableToCompleteException(); } }