View Javadoc

1   package net.sf.quarrel.file;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import java.io.BufferedReader;
7   import java.io.ByteArrayInputStream;
8   import java.io.InputStreamReader;
9   import java.util.ArrayList;
10  import java.util.HashMap;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  /***
15   * This class will take an instance of ElementText and produce an instance
16   * of FileElement.
17   *
18   * @see StreamParser
19   * @deprecated this is now handled in the StreamParser class
20   */
21  public class ElementParsingService {
22  
23      /***
24       * The logging instance for this class.
25       */
26      private final static Log log = LogFactory.getLog(ElementParsingService.class);
27  
28      public static FileElement parse(final ElementText elementText)
29              throws Exception {
30  
31          log.debug(elementText.getText());
32  
33          final ElementType elementType = elementText.getElementType();
34  
35          final HashMap featuresMap = new HashMap();
36  
37          final List features = elementType.getFeatures();
38          for (Iterator iterator = features.iterator(); iterator.hasNext();) {
39              featuresMap.put(iterator.next(), new ArrayList());
40          }
41  
42          final ByteArrayInputStream bais = new ByteArrayInputStream(elementText.getText().getBytes());
43          final BufferedReader reader = new BufferedReader(new InputStreamReader(bais));
44  
45          String line;
46  
47          // get the first line and pull the name out of it
48          line = reader.readLine();
49  
50          final int i = line.indexOf(" ");
51          final String name = line.substring(i + 1);
52          FeatureType currentFeature = null;
53          while ((line = reader.readLine()) != null) {
54  
55              try {
56  
57                  line = line.trim();
58  
59                  final FeatureType type = FeatureType.getEnum(line);
60  
61                  if (type != null && features.contains(type)) {
62                      currentFeature = type;
63                      continue;
64                  }
65  
66                  List featuresList = (List) featuresMap.get(currentFeature);
67                  featuresList.add(line);
68  
69              } catch (Exception e) {
70                  throw new Exception("error processing [" + line + "]");
71              }
72  
73          }
74  
75          return new ElementImpl(name, elementType, featuresMap);
76  
77      }
78  
79  }