Java 在Eclipse运行时实例工作区中保存文件
我有一个插件项目和自定义命令,它通过单击鼠标右键来获取文件。我的算法获取docx文件并将其转换为EMF模型实例文件(.xmi)。问题是典型的资源节约不起作用。我想将运行时创建的文件保存在和输入文件相同的目录中 下面的java类是我的命令处理程序类。At产品p=Docx2ReqModelConverter.Convert(文档)line,我使用导入的项目类解析文档并转换它。它给出了ecore模型的根元素对象 我只想创建一个xmi文件并将其保存在运行时eclipse中Java 在Eclipse运行时实例工作区中保存文件,java,eclipse,eclipse-plugin,resources,Java,Eclipse,Eclipse Plugin,Resources,我有一个插件项目和自定义命令,它通过单击鼠标右键来获取文件。我的算法获取docx文件并将其转换为EMF模型实例文件(.xmi)。问题是典型的资源节约不起作用。我想将运行时创建的文件保存在和输入文件相同的目录中 下面的java类是我的命令处理程序类。At产品p=Docx2ReqModelConverter.Convert(文档)line,我使用导入的项目类解析文档并转换它。它给出了ecore模型的根元素对象 我只想创建一个xmi文件并将其保存在运行时eclipse中 public class Co
public class ConvertHandler extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) {
// TODO Auto-generated method stub
XWPFDocument document = null;
// Shell shell = HandlerUtil.getActiveShell(event);
ISelection sel = HandlerUtil.getActiveMenuSelection(event);
IStructuredSelection selection = (IStructuredSelection) sel;
Object firstElement = selection.getFirstElement();
if(firstElement instanceof IFile){
IFile ifile = (IFile) Platform.getAdapterManager().getAdapter(firstElement, IFile.class);
if (ifile == null) {
if (firstElement instanceof IAdaptable) {
ifile = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
}
}
if (ifile != null) {
File f = ifile.getLocation().toFile();
try {
document = new XWPFDocument(new FileInputStream(f));
Product p = Docx2ReqModelConverter.Convert(document);
createXMIFile(p);
//Product p = Docx2ReqModelConverter.Convert(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
/**
* Saves the model instance and writes it to xmi file
*
* @param product
*/
private static Resource createXMIFile(Product product) {
ResourceSet resourceSet = new ResourceSetImpl();
// Register XML Factory implementation using xmi extension
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
"xmi", new XMLResourceFactoryImpl());
// Create empty resource with the given URI
Resource resource = resourceSet.createResource(URI.createURI("Testing/ReqModel.xmi"));
// Add Product to contents list of the resource
resource.getContents().add(product);
try{
// Save the resource
resource.save(null);
}catch (IOException e) {
e.printStackTrace();
}
return resource;
}
}
非常感谢您的任何意见和建议。不起作用怎么办?会发生什么?你以为会发生什么?什么都没发生。createXMI()实际上正在开发环境中工作。但在运行时eclipse实例中(我的意思是当我作为eclipse应用程序启动插件时),并没有创建文件。我确实打印了资源对象,内容正常。看起来您使用的是相对于当前目录的文件路径-可能不是您所认为的路径。尝试指定完整的文件路径。如果要在工作区中创建文件,还需要刷新文件夹。确定。我猜你的意思是.getLocation().toString()。我试试看。谢谢