allow [Embed(source="filename",mimeType="text/plain")] to initialize a string variable to the contents of a text file
Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/f56565d6 Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/f56565d6 Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/f56565d6 Branch: refs/heads/feature-autobuild/example-maven-dirs Commit: f56565d61e77c141eae1f71d50f51a737d65167a Parents: 9694d69 Author: Alex Harui <aha...@apache.org> Authored: Fri Oct 28 14:47:25 2016 -0700 Committer: Alex Harui <aha...@apache.org> Committed: Fri Oct 28 14:47:37 2016 -0700 ---------------------------------------------------------------------- .../flex/compiler/codegen/as/IASEmitter.java | 3 + .../internal/codegen/as/ASBlockWalker.java | 2 +- .../compiler/internal/codegen/as/ASEmitter.java | 6 ++ .../codegen/js/flexjs/JSFlexJSEmitter.java | 64 ++++++++++++++++++++ .../compiler/internal/embedding/EmbedData.java | 3 + .../internal/embedding/EmbedMIMEType.java | 2 + .../compiler/internal/tree/as/EmbedNode.java | 36 +++++++++++ 7 files changed, 115 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler-jx/src/main/java/org/apache/flex/compiler/codegen/as/IASEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/codegen/as/IASEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/codegen/as/IASEmitter.java index 2fe9eea..928259b 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/codegen/as/IASEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/codegen/as/IASEmitter.java @@ -32,6 +32,7 @@ import org.apache.flex.compiler.tree.as.ICatchNode; import org.apache.flex.compiler.tree.as.IClassNode; import org.apache.flex.compiler.tree.as.IContainerNode; import org.apache.flex.compiler.tree.as.IDynamicAccessNode; +import org.apache.flex.compiler.tree.as.IEmbedNode; import org.apache.flex.compiler.tree.as.IForLoopNode; import org.apache.flex.compiler.tree.as.IFunctionCallNode; import org.apache.flex.compiler.tree.as.IFunctionNode; @@ -360,6 +361,8 @@ public interface IASEmitter extends IEmitter void emitMetaTag(IMetaTagNode node); + void emitEmbed(IEmbedNode node); + void emitContainer(IContainerNode node); void emitE4XFilter(IMemberAccessExpressionNode node); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASBlockWalker.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASBlockWalker.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASBlockWalker.java index aa29f4d..5247583 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASBlockWalker.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASBlockWalker.java @@ -595,7 +595,7 @@ public class ASBlockWalker implements IASBlockVisitor, IASBlockWalker public void visitEmbed(IEmbedNode node) { debug("visitEmbed(" + node.getAttributes()[0].getValue() + ")"); - // TODO (mschmalle) visitEmbed() + emitter.emitEmbed(node); } @Override http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASEmitter.java index f25d6c9..0320d6a 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/as/ASEmitter.java @@ -54,6 +54,7 @@ import org.apache.flex.compiler.tree.as.IContainerNode; import org.apache.flex.compiler.tree.as.IContainerNode.ContainerType; import org.apache.flex.compiler.tree.as.IDefinitionNode; import org.apache.flex.compiler.tree.as.IDynamicAccessNode; +import org.apache.flex.compiler.tree.as.IEmbedNode; import org.apache.flex.compiler.tree.as.IExpressionNode; import org.apache.flex.compiler.tree.as.IForLoopNode; import org.apache.flex.compiler.tree.as.IFunctionCallNode; @@ -1511,6 +1512,11 @@ public class ASEmitter implements IASEmitter, IEmitter } @Override + public void emitEmbed(IEmbedNode node) + { + } + + @Override public void emitContainer(IContainerNode node) { } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/flexjs/JSFlexJSEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/flexjs/JSFlexJSEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/flexjs/JSFlexJSEmitter.java index a74b346..a7c0184 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/flexjs/JSFlexJSEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/flexjs/JSFlexJSEmitter.java @@ -19,10 +19,13 @@ package org.apache.flex.compiler.internal.codegen.js.flexjs; +import java.io.File; import java.io.FilterWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.io.FileUtils; import org.apache.flex.compiler.codegen.js.flexjs.IJSFlexJSEmitter; import org.apache.flex.compiler.codegen.js.goog.IJSGoogDocEmitter; import org.apache.flex.compiler.constants.IASKeywordConstants; @@ -33,6 +36,7 @@ import org.apache.flex.compiler.definitions.IDefinition; import org.apache.flex.compiler.definitions.INamespaceDefinition; import org.apache.flex.compiler.definitions.IPackageDefinition; import org.apache.flex.compiler.definitions.ITypeDefinition; +import org.apache.flex.compiler.definitions.metadata.IMetaTagAttribute; import org.apache.flex.compiler.internal.codegen.as.ASEmitterTokens; import org.apache.flex.compiler.internal.codegen.js.JSSessionModel.ImplicitBindableImplementation; import org.apache.flex.compiler.internal.codegen.js.goog.JSGoogEmitter; @@ -61,6 +65,10 @@ import org.apache.flex.compiler.internal.codegen.js.utils.EmitterUtils; import org.apache.flex.compiler.internal.codegen.mxml.flexjs.MXMLFlexJSEmitter; import org.apache.flex.compiler.internal.definitions.AccessorDefinition; import org.apache.flex.compiler.internal.definitions.FunctionDefinition; +import org.apache.flex.compiler.internal.embedding.EmbedAttribute; +import org.apache.flex.compiler.internal.embedding.EmbedData; +import org.apache.flex.compiler.internal.embedding.EmbedMIMEType; +import org.apache.flex.compiler.internal.projects.CompilerProject; import org.apache.flex.compiler.internal.projects.FlexJSProject; import org.apache.flex.compiler.internal.projects.FlexProject; import org.apache.flex.compiler.internal.tree.as.BinaryOperatorAsNode; @@ -72,6 +80,7 @@ import org.apache.flex.compiler.internal.tree.as.IdentifierNode; import org.apache.flex.compiler.internal.tree.as.LabeledStatementNode; import org.apache.flex.compiler.internal.tree.as.MemberAccessExpressionNode; import org.apache.flex.compiler.internal.tree.as.NumericLiteralNode; +import org.apache.flex.compiler.problems.EmbedUnableToReadSourceProblem; import org.apache.flex.compiler.projects.ICompilerProject; import org.apache.flex.compiler.tree.ASTNodeID; import org.apache.flex.compiler.tree.as.IASNode; @@ -80,6 +89,7 @@ import org.apache.flex.compiler.tree.as.IBinaryOperatorNode; import org.apache.flex.compiler.tree.as.IClassNode; import org.apache.flex.compiler.tree.as.IContainerNode; import org.apache.flex.compiler.tree.as.IDefinitionNode; +import org.apache.flex.compiler.tree.as.IEmbedNode; import org.apache.flex.compiler.tree.as.IExpressionNode; import org.apache.flex.compiler.tree.as.IFileNode; import org.apache.flex.compiler.tree.as.IForLoopNode; @@ -102,6 +112,7 @@ import org.apache.flex.compiler.utils.ASNodeUtils; import com.google.common.base.Joiner; import org.apache.flex.compiler.utils.NativeUtils; +import org.apache.flex.utils.FilenameNormalization; /** * Concrete implementation of the 'FlexJS' JavaScript production. @@ -799,6 +810,59 @@ public class JSFlexJSEmitter extends JSGoogEmitter implements IJSFlexJSEmitter literalEmitter.emit(node); } + @Override + public void emitEmbed(IEmbedNode node) + { + // if the embed is text/plain, return the actual text from the file. + // this assumes the variable being initialized is of type String. + // Embed node seems to not have location, so use parent. + EmbedData data = new EmbedData(node.getParent().getSourcePath(), null); + boolean hadError = false; + for (IMetaTagAttribute attribute : node.getAttributes()) + { + String key = attribute.getKey(); + String value = attribute.getValue(); + if (data.addAttribute((CompilerProject) project, node.getParent(), key, value, getProblems())) + { + hadError = true; + } + } + if (hadError) + { + write(""); + return; + } + String source = (String) data.getAttribute(EmbedAttribute.SOURCE); + EmbedMIMEType mimeType = (EmbedMIMEType) data.getAttribute(EmbedAttribute.MIME_TYPE); + if (mimeType != null && mimeType.toString().equals(EmbedMIMEType.TEXT.toString()) && source != null) + { + File file = new File(FilenameNormalization.normalize(source)); + try { + String newlineReplacement = "\\\\n"; + String s = FileUtils.readFileToString(file); + s = s.replaceAll("\n", "__NEWLINE_PLACEHOLDER__"); + s = s.replaceAll("\r", "__CR_PLACEHOLDER__"); + s = s.replaceAll("\t", "__TAB_PLACEHOLDER__"); + s = s.replaceAll("\f", "__FORMFEED_PLACEHOLDER__"); + s = s.replaceAll("\b", "__BACKSPACE_PLACEHOLDER__"); + s = s.replaceAll("\\\\", "__ESCAPE_PLACEHOLDER__"); + s = s.replaceAll("\\\\\"", "__QUOTE_PLACEHOLDER__"); + s = s.replaceAll("\"", "\\\\\""); + s = s.replaceAll("__QUOTE_PLACEHOLDER__", "\\\\\""); + s = s.replaceAll("__ESCAPE_PLACEHOLDER__", "\\\\\\\\"); + s = s.replaceAll("__BACKSPACE_PLACEHOLDER__", "\\\\b"); + s = s.replaceAll("__FORMFEED_PLACEHOLDER__", "\\\\f"); + s = s.replaceAll("__TAB_PLACEHOLDER__", "\\\\t"); + s = s.replaceAll("__CR_PLACEHOLDER__", "\\\\r"); + s = s.replaceAll("__NEWLINE_PLACEHOLDER__", newlineReplacement); + write("\"" + s + "\""); + } catch (IOException e) { + getProblems().add(new EmbedUnableToReadSourceProblem(e, file.getPath())); + } + } + } + + //-------------------------------------------------------------------------- // Specific //-------------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedData.java ---------------------------------------------------------------------- diff --git a/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedData.java b/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedData.java index 0df0482..4835e24 100644 --- a/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedData.java +++ b/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedData.java @@ -471,6 +471,9 @@ public class EmbedData transcoder = null; break; } + case TEXT: + case PROPERTIES: + break; // don't need transcoder for text default: { problems.add(new EmbedUnknownMimeTypeProblem(location, mimeType)); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedMIMEType.java ---------------------------------------------------------------------- diff --git a/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedMIMEType.java b/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedMIMEType.java index d7aa356..59aaa33 100644 --- a/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedMIMEType.java +++ b/compiler/src/main/java/org/apache/flex/compiler/internal/embedding/EmbedMIMEType.java @@ -30,6 +30,8 @@ public enum EmbedMIMEType GIF("image/gif", ".gif"), MP3("audio/mpeg", ".mp3"), FLASH("application/x-shockwave-flash", ".swf"), + TEXT("text/plain", ".txt"), + PROPERTIES("text/plain", ".properties"), XML("text/xml", ".xml"), PBJ("application/x-pbj", ".pbj"), OCT_STRM("application/octet-stream"), http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f56565d6/compiler/src/main/java/org/apache/flex/compiler/internal/tree/as/EmbedNode.java ---------------------------------------------------------------------- diff --git a/compiler/src/main/java/org/apache/flex/compiler/internal/tree/as/EmbedNode.java b/compiler/src/main/java/org/apache/flex/compiler/internal/tree/as/EmbedNode.java index bd062c0..14b67c7 100644 --- a/compiler/src/main/java/org/apache/flex/compiler/internal/tree/as/EmbedNode.java +++ b/compiler/src/main/java/org/apache/flex/compiler/internal/tree/as/EmbedNode.java @@ -19,15 +19,23 @@ package org.apache.flex.compiler.internal.tree.as; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import org.apache.commons.io.FileUtils; import org.apache.flex.compiler.common.IEmbedResolver; +import org.apache.flex.compiler.common.ISourceLocation; import org.apache.flex.compiler.definitions.ITypeDefinition; import org.apache.flex.compiler.definitions.metadata.IMetaTagAttribute; +import org.apache.flex.compiler.internal.embedding.EmbedAttribute; +import org.apache.flex.compiler.internal.embedding.EmbedData; +import org.apache.flex.compiler.internal.embedding.EmbedMIMEType; import org.apache.flex.compiler.internal.projects.CompilerProject; import org.apache.flex.compiler.internal.units.EmbedCompilationUnit; import org.apache.flex.compiler.internal.units.EmbedCompilationUnitFactory; +import org.apache.flex.compiler.problems.EmbedUnableToReadSourceProblem; import org.apache.flex.compiler.problems.ICompilerProblem; import org.apache.flex.compiler.projects.ICompilerProject; import org.apache.flex.compiler.tree.ASTNodeID; @@ -132,6 +140,34 @@ public class EmbedNode extends ExpressionNodeBase implements IEmbedNode, IEmbedR public String getName(ICompilerProject project, Collection<ICompilerProblem> problems) throws InterruptedException { + // if the embed is text/plain, return the actual text from the file. + // this assumes the variable being initialized is of type String. + EmbedData data = new EmbedData(containingSourceFilename, null); + boolean hadError = false; + for (IMetaTagAttribute attribute : getAttributes()) + { + String key = attribute.getKey(); + String value = attribute.getValue(); + if (data.addAttribute((CompilerProject) project, this, key, value, problems)) + { + hadError = true; + } + } + if (hadError) + return new String(); + String source = (String) data.getAttribute(EmbedAttribute.SOURCE); + EmbedMIMEType mimeType = (EmbedMIMEType) data.getAttribute(EmbedAttribute.MIME_TYPE); + if (mimeType != null && mimeType.toString().equals(EmbedMIMEType.TEXT.toString()) && source != null) + { + File file = new File(FilenameNormalization.normalize(source)); + try { + String string = FileUtils.readFileToString(file); + return string; + } catch (IOException e) { + problems.add(new EmbedUnableToReadSourceProblem(e, file.getPath())); + } + + } EmbedCompilationUnit cu = resolveCompilationUnit(project, problems); // If there was an error resolving the compilation unit, just return an empty string. if (cu == null)