浏览 5540 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
||
---|---|---|
作者 | 正文 | |
发表时间:2008-05-13
package com.koders.se.parser; import com.koders.se.search.WikiDOC; import net.sf.mpxj.ProjectFile; import net.sf.mpxj.Resource; import net.sf.mpxj.ResourceAssignment; import net.sf.mpxj.Task; import net.sf.mpxj.mpp.MPPReader; import net.sf.mpxj.mpx.MPXReader; import net.sf.mpxj.mspdi.MSPDIReader; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; /** * Microsoft project 解析器 */ public class MppDocHandler implements DocumentHandler { public Document getDocument(InputStream is) throws DocumentHandlerException { ProjectFile mpx = readProject(is); String bodyText = dumpText(mpx); if (bodyText != null) { Document doc = new Document(); doc.add(new Field(WikiDOC.DOC_TITLE, dumpTitle(mpx), Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); doc.add(new Field(WikiDOC.DOC_CONTENT, bodyText, Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); return doc; } return null; } public static ProjectFile readProject(InputStream is) throws DocumentHandlerException { is = new BufferedInputStream(is); is.mark(0);//下面需要重复使用输入流,所以重新包装并设置重置标记 ProjectFile mpx = null; try { mpx = new MPXReader().read(is); } catch (Exception ex) { try { is.reset();//重置 } catch (IOException e) { e.printStackTrace(); } } if (mpx == null) { try { mpx = new MPPReader().read(is); } catch (Exception ex) { try { is.reset(); } catch (IOException e) { e.printStackTrace(); } } } if (mpx == null) { try { mpx = new MSPDIReader().read(is); } catch (Exception ex) { } } if (mpx == null) { throw new DocumentHandlerException("Failed to read file"); } return mpx; } private final static SimpleDateFormat f = new SimpleDateFormat("yyyy年M月d日"); private final static String IndentString = " "; private List idList = new ArrayList(); private List nameList = new ArrayList(); private List resList = new ArrayList(); private List startList = new ArrayList(); private List endList = new ArrayList(); public String dumpTitle(ProjectFile file) { String title = ""; List childTasks = file.getChildTasks(); for (int i = 0; i < childTasks.size(); i++) { Task task = (Task) childTasks.get(i); title += task.getName() + " "; } return title; } public String dumpText(ProjectFile file) { idList.add("【编号】"); nameList.add("【任务名】"); resList.add("【资源】"); startList.add("【开始时间】"); endList.add("【结束时间】"); List childTasks = file.getChildTasks(); for (int i = 0; i < childTasks.size(); i++) { Task task = (Task) childTasks.get(i); idList.add(" " + task.getID().toString()); nameList.add(task.getName()); resList.add(listTaskRes(task)); startList.add(f.format(task.getStart())); endList.add(f.format(task.getFinish())); listHierarchy(task, IndentString); } idList = fixLength(idList); nameList = fixLength(nameList); resList = fixLength(resList); startList = fixLength(startList); endList = fixLength(endList); StringBuffer buf = new StringBuffer(); for (int i = 0, size = nameList.size(); i < size; i++) { buf.append(idList.get(i)) .append(nameList.get(i)) .append(IndentString) .append(resList.get(i)) .append(IndentString) .append(startList.get(i)) .append(IndentString) .append(endList.get(i)) .append("\n"); } idList.clear(); nameList.clear(); startList.clear(); endList.clear(); resList.clear(); return buf.toString(); } private List fixLength(List data) { int max = 0; for (int ii = 0; ii < data.size(); ii++) { String str = (String) data.get(ii); int tmp = str.getBytes().length; max = (max < tmp ? tmp : max); } List ret = new ArrayList(); for (int ii = 0; ii < data.size(); ii++) { String str = (String) data.get(ii); int tmp = max - str.getBytes().length; for (int i = 0; i < tmp; i++) { str = str.concat(" "); } ret.add(str); } return ret; } private void listHierarchy(Task task, String indent) { List childTasks = task.getChildTasks(); for (int i = 0; i < childTasks.size(); i++) { Task child = (Task) childTasks.get(i); idList.add(" " + child.getID().toString()); nameList.add(indent + child.getName()); resList.add(listTaskRes(child)); startList.add(f.format(child.getStart())); endList.add(f.format(child.getFinish())); listHierarchy(child, indent + IndentString); } } private String listTaskRes(Task task) { StringBuffer buf = new StringBuffer(); List assignments = task.getResourceAssignments(); for (int i = 0; i < assignments.size(); i++) { ResourceAssignment assignment = (ResourceAssignment) assignments.get(i); Resource resource = assignment.getResource(); if (resource != null) { buf.append(resource.getName()).append(" "); } } return buf.toString(); } public static void main(String[] args) throws Exception { MppDocHandler mppDocHandler = new MppDocHandler(); ProjectFile projectFile = MppDocHandler.readProject(new FileInputStream("项目实施计划.mpp")); String s = mppDocHandler.dumpText(projectFile); System.out.println(s); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
||
返回顶楼 | ||
发表时间:2008-06-04
我需要用mpxj生成mpx文件, 但是中文是???之类 的乱码, 你知道如何解决吗?
我加上这个也不行: file.getFileCreationRecord().setCodePage(CodePage.ZH); |
||
返回顶楼 | ||
发表时间:2008-06-04
这样就可以了:
MPXWriter writer = new MPXWriter();
writer.setLocale(Locale.CHINESE);
|
||
返回顶楼 | ||
发表时间:2008-06-23
我需要把数据库中的数据导出生成mpp文件,用mpxj,请问怎么实现啊
邮箱chenhui_19850710@sina.com.cn |
||
返回顶楼 | ||