实现Microsoft Project 的解析和内容抽取

 

  • 文本内容提取:  使用net.sf.mpxj 的工具提取文本内容;
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);
    }

}
  •  输出格式:

 


【编号】【任务名】                                          【资源】                                【开始时间】    【结束时间】
  0     **扩建项目实施计划_0903                                                                     2008年4月1日    2008年7月1日
  1        项目启动                                                                                2008年4月1日    2008年4月8日
  2           组建项目组、制订项目计划                                                               2008年4月1日    2008年4月8日
  3           项目启动会                                                                            2008年4月1日    2008年4月1日
  60          综合业务管理子系统需求分析                     张四季 黄纬 笋素爱 李海涛 李贤宇         2008年4月1日    2008年5月4日
  61             业务管理                                   张四季 黄纬 笋素爱 李海涛                2008年4月1日    2008年4月15日
  62                提交调研提纲文档                        张四季 黄纬 笋素爱                       2008年4月1日    2008年4月1日
  63                需求内容调研                            张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  64                   业务种类设置                         张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  65                   增值业务支撑                         张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  66                   监控策略设置                         张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  67                   封堵策略设置                         张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  68                   广告策略设置                         张四季 黄纬 笋素爱                        2008年4月2日    2008年4月3日
  69                需求分析                                张四季 黄纬 笋素爱                       2008年4月7日    2008年4月7日
  70                   整理调研文档                                                                  2008年4月7日    2008年4月7日
  71                   编写需求说明书-业务管理部分                                                   2008年4月7日    2008年4月7日
  72                页面原型设计                            张四季 黄纬 笋素爱                        2008年4月8日    2008年4月14日
  73                业务管理需求与页面原型确认              张四季 黄纬 笋素爱                        2008年4月15日   2008年4月15日
  74             资料管理                                   笋素爱                                    2008年4月16日   2008年4月23日
  75                提交调研提纲文档                                                                 2008年4月16日   2008年4月16日
  76                需求内容调研                                                                     2008年4月17日   2008年4月17日
  77                   客户资料管理                                                                  2008年4月17日   2008年4月17日
  78                   黑名单管理                                                                    2008年4月17日   2008年4月17日
  79                   白名单管理                                                                    2008年4月17日   2008年4月17日
  80                   ADSL帐号管理                                                                  2008年4月17日   2008年4月17日
  81                   IP地址管理                                                                    2008年4月17日   2008年4月17日
  82                需求分析                                                                         2008年4月18日   2008年4月18日
  83                   整理调研文档                                                                  2008年4月18日   2008年4月18日
  84                   编写需求说明书-资料管理部分                                                   2008年4月18日   2008年4月18日
  85                页面原型设计                                                                     2008年4月21日   2008年4月22日
  86                资料管理需求与页面原型确认                                                       2008年4月23日   2008年4月23日
  87             数据管理                                   张四季 黄纬                              2008年4月16日   2008年4月23日
  88                提交调研提纲文档                                                                 2008年4月16日   2008年4月16日
  89                需求内容调研                                                                     2008年4月17日   2008年4月17日
  90                   数据采集                                                                      2008年4月17日   2008年4月17日
  91                   数据转换                                                                      2008年4月17日   2008年4月17日
  92                   数据导出                                                                      2008年4月17日   2008年4月17日
  93                需求分析                                                                         2008年4月18日   2008年4月18日
  94                   整理调研文档                                                                  2008年4月18日   2008年4月18日
  95                   编写需求说明书数据管理部分                                                    2008年4月18日   2008年4月18日
  96                页面原型设计                                                                     2008年4月21日   2008年4月22日
  97                数据管理需求与页面原型确认                                                       2008年4月23日   2008年4月23日
  98             综合查询                                   笋素爱                                    2008年4月24日   2008年5月4日
  99                提交调研提纲文档                                                                 2008年4月24日   2008年4月24日
  100               需求内容调研                                                                     2008年4月25日   2008年4月25日
  101                  客户基本信息查询                                                              2008年4月25日   2008年4月25日
  102                  ADSL帐号查询                                                                  2008年4月25日   2008年4月25日
  103                  专线IP查询                                                                    2008年4月25日   2008年4月25日
  104                  业务策略查询                                                                  2008年4月25日   2008年4月25日
  105                  流量数据查询                                                                  2008年4月25日   2008年4月25日
  106               需求分析                                                                         2008年4月28日   2008年4月28日
  107                  整理调研文档                                                                  2008年4月28日   2008年4月28日
  108                  编写需求说明书-综合查询部分                                                   2008年4月28日   2008年4月28日
  109               页面原型设计                                                                     2008年4月29日   2008年4月30日
  110               综合查询需求与页面原型确认                                                       2008年5月4日    2008年5月4日
  208         提交第一版需求说明书                           全体需求人员                             2008年5月5日    2008年5月5日

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己写的示例 仅供参考: public static void main(String[] args) { File file; file = new File("e:\\客户管理项目计划V1.0.mpp"); LinkedHashMap infoMap = new LinkedHashMap(); Map userSbMap = new HashMap(); List resourceList = new ArrayList(); String a = readMpp(file, infoMap, userSbMap, resourceList); } public static String readMpp(File file, LinkedHashMap infoMap, Map userSbMap, List resourceList) { MPPFile mpp; String result = ""; try { mpp = new MPPFile(file); LinkedList allList = mpp.getAllTasks(); allList.remove(0); for (int i = 0; i < allList.size(); i++) { Task t = (Task) allList.get(i); Map taskMap = new HashMap(); String flag = t.getUniqueID().toString(); taskMap.put("唯一标识号", t.getUniqueID().toString()); taskMap.put("标识号", t.getID().toString()); taskMap.put("任务名称", t.getName()); taskMap.put("大纲级别", t.getOutlineLevel().toString()); //处理开始时间 Date planStartDate = t.getStart(); String planStart = "-1"; if (planStartDate != null) { planStart = new Long(planStartDate.getTime()).toString(); } //处理完成时间 Date planStopDate = t.getFinish(); String planFinish = "-1"; if (planStopDate != null) { planFinish = new Long(planStopDate.getTime()).toString(); } //处理实际开始时间 Date actStartDate = t.getActualStart(); String actStart = "-1"; if (actStartDate != null) { actStart = new Long(actStartDate.getTime()).toString(); } //处理实际结束日期 Date actStopDate = t.getActualFinish(); String actFinish = "-1"; if (actStopDate != null) { actFinish = new Long(actStopDate.getTime()).toString(); } //处理里程碑 Boolean markedB = t.getMilestone(); String y_or_n = "否"; if (markedB != null) { boolean marked = markedB.booleanValue(); if (marked) { y_or_n = "是"; } } //处理资源 List userList = new ArrayList(); LinkedList resources = t.getResourceAssignments(); if

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值