KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > par > ProcessArchive


1 package org.jbpm.jpdl.par;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.zip.*;
6
7 import org.dom4j.*;
8 import org.dom4j.io.*;
9 import org.jbpm.graph.def.*;
10 import org.jbpm.instantiation.*;
11 import org.jbpm.jpdl.xml.Problem;
12
13 public class ProcessArchive {
14   
15   static final int BUFFERSIZE = 4096;
16   static List processArchiveParsers = getProcessArchiveParsers();
17   
18   // fields ///////////////////////////////////////////////////////////////////
19

20   String JavaDoc name = "";
21   // maps entry-names (String) to byte-arrays (byte[])
22
Map entries = new HashMap();
23   List problems = new ArrayList();
24
25   // constructors /////////////////////////////////////////////////////////////
26

27   public ProcessArchive(ZipInputStream zipInputStream) throws IOException {
28     ZipEntry zipEntry = zipInputStream.getNextEntry();
29     while(zipEntry!=null) {
30       String JavaDoc entryName = zipEntry.getName();
31       byte[] bytes = readBytes(zipInputStream);
32       if (bytes!=null) {
33         entries.put(entryName, bytes);
34       }
35       zipEntry = zipInputStream.getNextEntry();
36     }
37   }
38   
39   // parse the process definition from the contents ///////////////////////////
40

41   public ProcessDefinition parseProcessDefinition() {
42     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
43     Iterator iter = processArchiveParsers.iterator();
44     while (iter.hasNext()) {
45       ProcessArchiveParser processArchiveParser = (ProcessArchiveParser) iter.next();
46       processDefinition = processArchiveParser.readFromArchive(this, processDefinition);
47     }
48     return processDefinition;
49   }
50   
51   // methods for the process archive parsers //////////////////////////////////
52

53   public String JavaDoc toString() {
54     return "process-archive("+name+")";
55   }
56   
57   public Map getEntries() {
58     return entries;
59   }
60   
61   public byte[] getEntry(String JavaDoc entryName) {
62     return (byte[]) entries.get(entryName);
63   }
64
65   public InputStream getEntryInputStream(String JavaDoc entryName) {
66     return new ByteArrayInputStream(getEntry(entryName));
67   }
68
69   public byte[] removeEntry(String JavaDoc entryName) {
70     return (byte[]) entries.remove(entryName);
71   }
72
73   public InputStream removeEntryInputStream(String JavaDoc entryName) {
74     return new ByteArrayInputStream(removeEntry(entryName));
75   }
76   public void addProblem(Problem problem) {
77     problems.add(problem);
78   }
79   
80   public void addError(String JavaDoc description) {
81     addProblem(new Problem(Problem.LEVEL_ERROR, description));
82   }
83
84   public void addError(String JavaDoc description, Throwable JavaDoc exception) {
85     addProblem(new Problem(Problem.LEVEL_ERROR, description, exception));
86   }
87
88   public void addWarning(String JavaDoc description) {
89     addProblem(new Problem(Problem.LEVEL_WARNING, description));
90   }
91
92   public List getProblems() {
93     return problems;
94   }
95
96   public void resetProblems() {
97     problems = new ArrayList();
98   }
99
100   static byte[] readBytes(InputStream inputStream) throws IOException {
101     byte[] bytes = null;
102     if (inputStream==null) {
103       throw new NullPointerException JavaDoc("inputStream is null in ProcessArchive.readBytes()");
104     }
105     byte[] buffer = new byte[BUFFERSIZE];
106     int bytesRead = 0;
107     while ( (bytesRead = inputStream.read(buffer)) != -1) {
108       if (bytes!=null) {
109         byte[] oldBytes = bytes;
110         bytes = new byte[oldBytes.length+bytesRead];
111         System.arraycopy(oldBytes, 0, bytes, 0, oldBytes.length);
112         System.arraycopy(buffer, 0, bytes, oldBytes.length, bytesRead);
113       } else {
114         bytes = new byte[bytesRead];
115         System.arraycopy(buffer, 0, bytes, 0, bytesRead);
116       }
117     }
118     return bytes;
119   }
120
121   private static List getProcessArchiveParsers() {
122     List processArchiveParsers = new ArrayList();
123     try {
124       InputStream parsersStream = ClassLoaderUtil.getStream("jbpm.parsers.xml", "org/jbpm/jpdl/par");
125       Document document = new SAXReader().read(parsersStream);
126       Iterator iter = document.getRootElement().elementIterator("parser");
127       while (iter.hasNext()) {
128         Element element = (Element) iter.next();
129         String JavaDoc className = element.attributeValue("class");
130         ProcessArchiveParser processArchiveParser= (ProcessArchiveParser) ClassLoaderUtil.loadClass(className).newInstance();
131         processArchiveParsers.add(processArchiveParser);
132       }
133     } catch (Exception JavaDoc e) {
134       throw new RuntimeException JavaDoc("couldn't parse process archive parsers (jbpm.parsers.xml)", e);
135     }
136     return processArchiveParsers;
137   }
138
139 }
140
Popular Tags