/*******************************************************************************
* Copyright © 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*
*******************************************************************************/
package org.eclipse.edt.demo.csharp.gen;
import java.io.IOException;
import org.eclipse.edt.compiler.core.IEGLConstants;
import org.eclipse.edt.compiler.internal.interfaces.IGenerationMessageRequestor;
import org.eclipse.edt.compiler.internal.util.IGenerationResultsMessage;
import org.eclipse.edt.demo.csharp.gen.templates.CSharpTemplate;
import org.eclipse.edt.gen.AbstractGeneratorCommand;
import org.eclipse.edt.gen.CommonUtilities;
import org.eclipse.edt.gen.GenerationException;
import org.eclipse.edt.gen.Generator;
import org.eclipse.edt.gen.EGLMessages.EGLMessage;
import org.eclipse.edt.mof.codegen.api.TabbedReportWriter;
import org.eclipse.edt.mof.codegen.api.TabbedWriter;
import org.eclipse.edt.mof.codegen.api.TemplateException;
import org.eclipse.edt.mof.egl.Annotation;
import org.eclipse.edt.mof.egl.Part;
public class CSharpGenerator extends Generator {
protected Context context;
protected TabbedWriter out;
protected AbstractGeneratorCommand generator;
public CSharpGenerator(AbstractGeneratorCommand processor) {
this(processor, null);
}
public CSharpGenerator(AbstractGeneratorCommand processor, IGenerationMessageRequestor requestor) {
super(processor, requestor);
generator = processor;
}
public String getResult() {
if (out == null)
return "";
return out.getWriter().toString();
}
@Override
public TabbedReportWriter getReport() {
return (out instanceof TabbedReportWriter) ? (TabbedReportWriter) out : null;
}
public Context makeContext(AbstractGeneratorCommand processor) {
context = new Context(processor);
return context;
}
public void generate(Object part) throws GenerationException {
makeWriter();
try {
context.invoke(CSharpTemplate.preGenPart, part, context);
if (!context.getMessageRequestor().isError()) {
context.invoke(CSharpTemplate.genPart, part, context, out);
}
}
// catch (IOException e) {
// throw new GenerationException(e);
// }
catch (TemplateException e) {
String[] details1 = new String[] { e.getLocalizedMessage() };
EGLMessage message1 = EGLMessage.createEGLMessage(context.getMessageMapping(), EGLMessage.EGL_ERROR_MESSAGE,
"9998", e, details1, CommonUtilities.includeEndOffset(context.getLastStatementLocation(), context));
context.getMessageRequestor().addMessage(message1);
if (e.getCause() != null) {
String[] details2 = new String[] { e.getCause().toString() };
EGLMessage message2 = EGLMessage.createEGLMessage(context.getMessageMapping(), EGLMessage.EGL_ERROR_MESSAGE, "9999",
e, details2, CommonUtilities.includeEndOffset(context.getLastStatementLocation(), context));
context.getMessageRequestor().addMessage(message2);
}
// print out the whole stack trace
int startLine = 0;
Annotation annotation = context.getLastStatementLocation();
if (annotation != null) {
if (annotation.getValue(IEGLConstants.EGL_PARTLINE) != null)
startLine = ((Integer) annotation.getValue(IEGLConstants.EGL_PARTLINE)).intValue();
}
System.err.println("generating:" + ((Part) part).getFullyQualifiedName() + "[" + ((Part) part).getFileName() + "]:(" + startLine + ")");
e.printStackTrace();
}
// close the output
out.close();
}
private void makeWriter() {
if (out == null)
out = context.getTabbedWriter();
}
private String unqualifyFileName(String fileName) {
int lastSlash = fileName.lastIndexOf('/');
if (lastSlash != -1) {
return fileName.substring(lastSlash + 1);
}
return fileName;
}
public void dumpErrorMessages() {
// dump out all validation and generation messages
for (IGenerationResultsMessage message : context.getMessageRequestor().getMessages()) {
System.out.println(message.getBuiltMessage());
}
}
public void processFile(String fileName) {
// // do any post processing once the file has been written
// writeReport(context, fileName, getReport(), Constants.EGLMESSAGE_ENCODING_ERROR, Constants.EGLMESSAGE_GENERATION_REPORT_FAILED);
// // if no error was created, update the class file with the accumulated debug info
// if (!context.getMessageRequestor().isError()) {
// File outSmapFile = new File(fileName.substring(0, fileName.length() - getFileExtension().length()) + Constants.smap_fileExtension);
// try {
// FileOutputStream outStream = new FileOutputStream(outSmapFile);
// byte[] outSmapBytes = context.getSmapData().toString().getBytes(Constants.smap_encoding);
// outStream.write(outSmapBytes, 0, outSmapBytes.length);
// outStream.close();
// }
// catch (UnsupportedEncodingException e) {
// String[] details = new String[] { "UTF-8" };
// EGLMessage message = EGLMessage
// .createEGLMessage(context.getMessageMapping(), EGLMessage.EGL_ERROR_MESSAGE, Constants.EGLMESSAGE_SMAPFILE_ENCODING_FAILED, null, details,
// CommonUtilities.includeEndOffset(context.getLastStatementLocation(), context));
// context.getMessageRequestor().addMessage(message);
// }
// catch (IOException e) {
// String[] details = new String[] { outSmapFile.getName() };
// EGLMessage message = EGLMessage.createEGLMessage(context.getMessageMapping(), EGLMessage.EGL_ERROR_MESSAGE,
// Constants.EGLMESSAGE_SMAPFILE_WRITE_FAILED, null, details, CommonUtilities.includeEndOffset(context.getLastStatementLocation(), context));
// context.getMessageRequestor().addMessage(message);
// return;
// }
// }
}
@Override
public String getRelativeFileName(Part part) {
StringBuilder buf = new StringBuilder(50);
String pkg = part.getPackageName();
if (pkg.length() > 0) {
buf.append(pkg.replaceAll("\\.", "/"));
buf.append('/');
}
buf.append(part.getId());
buf.append(getFileExtension());
return buf.toString();
}
@Override
public String getFileExtension() {
return ".cs";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
CSharpDemo2
共74个文件
java:27个
class:27个
properties:7个
需积分: 9 8 下载量 23 浏览量
2012-04-10
16:42:05
上传
评论
收藏 65KB ZIP 举报
温馨提示
EGL与别的语言相比,最大的特点就是其具有的高度可扩展性。以下为EGL语言扩展文章示例代码。
资源推荐
资源详情
资源评论
收起资源包目录
CSharpDemo2.zip (74个子文件)
org.eclipse.edt.demo.csharp.gen
META-INF
MANIFEST.MF 525B
build.properties 80B
src
org
eclipse
edt
demo
csharp
gen
CommonUtilities.java 593B
templates
CSharpTemplate.java 3KB
Context.java 2KB
CSharpGenerationContributor.java 2KB
EGL2CSharp.java 989B
CSharpGenerator.java 6KB
bin
org
eclipse
edt
demo
csharp
gen
CommonUtilities.class 1KB
templates
CSharpTemplate.class 1KB
Context.class 1KB
EGL2CSharp.class 801B
CSharpGenerator.class 6KB
CSharpGenerationContributor.class 1KB
.classpath 376B
.settings
org.eclipse.jdt.core.prefs 395B
.project 692B
org.eclipse.edt.demo.csharp.gen.templates
META-INF
MANIFEST.MF 513B
build.properties 80B
src
org
eclipse
edt
demo
csharp
gen
templates
ExpressionStatementTemplate.java 976B
FieldTemplate.java 2KB
NamedElementTemplate.java 928B
WindowHandlerTemplate.java 1KB
StatementBlockTemplate.java 1KB
EGLClassTemplate.java 3KB
AssignmentTemplate.java 1KB
HandlerTemplate.java 1KB
RecordTemplate.java 931B
nativeTypes.properties 2KB
templates.properties 2KB
ExternalTypeTemplate.java 1KB
AssignmentStatementTemplate.java 984B
StringLiteralTemplate.java 999B
PartTemplate.java 2KB
QualifiedFunctionInvocationTemplate.java 1KB
ProgramTemplate.java 1KB
FunctionTemplate.java 1KB
bin
org
eclipse
edt
demo
csharp
gen
templates
HandlerTemplate.class 1KB
FieldTemplate.class 2KB
StatementBlockTemplate.class 2KB
WindowHandlerTemplate.class 2KB
nativeTypes.properties 2KB
templates.properties 2KB
PartTemplate.class 3KB
AssignmentStatementTemplate.class 1KB
NamedElementTemplate.class 984B
AssignmentTemplate.class 2KB
ExpressionStatementTemplate.class 1KB
RecordTemplate.class 988B
FunctionTemplate.class 2KB
StringLiteralTemplate.class 1KB
ExternalTypeTemplate.class 1KB
QualifiedFunctionInvocationTemplate.class 2KB
ProgramTemplate.class 2KB
EGLClassTemplate.class 3KB
.classpath 376B
.settings
org.eclipse.jdt.core.prefs 395B
.project 702B
org.eclipse.edt.demo.csharp.ide
META-INF
MANIFEST.MF 888B
build.properties 80B
src
org
eclipse
edt
demo
csharp
ide
gen
EclipseEGL2CSharp.java 3KB
CSharpGeneratorTabProvider.java 4KB
CSharpGenerator.java 2KB
EclipseCSharpGenerator.java 1KB
Activator.java 1KB
bin
org
eclipse
edt
demo
csharp
ide
Activator.class 930B
gen
EclipseEGL2CSharp.class 4KB
CSharpGeneratorTabProvider.class 4KB
EclipseCSharpGenerator.class 910B
CSharpGenerator.class 3KB
.classpath 376B
.settings
org.eclipse.jdt.core.prefs 395B
plugin.xml 1KB
.project 692B
共 74 条
- 1
资源评论
RationalGroup
- 粉丝: 57
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功