View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.kuali.student.contract.model.util;
6   
7   import java.io.PrintStream;
8   import java.util.Stack;
9   
10  import org.kuali.student.contract.model.MessageStructure;
11  import org.kuali.student.contract.model.ServiceContractModel;
12  import org.kuali.student.contract.model.XmlType;
13  
14  /**
15   *
16   * @author nwright
17   */
18  public class MessageStructureHierarchyDumper {
19  
20      private ServiceContractModel model;
21      private ModelFinder finder;
22      private PrintStream out;
23  
24      public MessageStructureHierarchyDumper(PrintStream out,
25              ServiceContractModel model) {
26          this.out = out;
27          this.model = model;
28          this.finder = new ModelFinder(model);
29      }
30  
31      public void writeTabbedHeader() {
32          out.print("id");
33          out.print("\t");
34          out.print("Action");
35          out.print("\t");
36          out.print("xmlObject");
37          out.print("\t");
38          out.print("ShortName");
39          out.print("\t");
40          out.print("Name");
41          out.print("\t");
42          out.print("Type");
43          out.print("\t");
44          out.print("Description");
45          out.print("\t");
46          out.print("Required");
47          out.print("\t");
48          out.print("Cardinality");
49          out.print("\t");
50          out.print("XMLAttribute");
51          out.print("\t");
52          out.print("Status");
53          out.print("\t");
54          out.print("Feedback");
55          out.println("");
56      }
57  
58      private String clean(String str) {
59          int len = str.length();
60          StringBuffer buffer = new StringBuffer(len);
61          for (int i = 0; i < len; i++) {
62              char c = str.charAt(i);
63              // skip \n, \r, \r\n
64              switch (c) {
65                  case '\n':
66                  case '\r': // do lookahead
67                      if (i + 1 < len && str.charAt(i + 1) == '\n') {
68                          i++;
69                      }
70  
71                      buffer.append(" ");
72                      break;
73                  default:
74                      buffer.append(c);
75              }
76          }
77          return buffer.toString();
78      }
79  
80      public String calcId(MessageStructure ms, Stack<String> parents) {
81          StringBuilder bldr = new StringBuilder();
82          {
83              for (String parent : parents) {
84                  bldr.append(parent);
85                  bldr.append(".");
86              }
87          }
88          bldr.append(ms.getShortName());
89          return bldr.toString();
90      }
91  
92      public void writeTabbedData(MessageStructure ms, Stack<String> parents) {
93          out.print(calcId(ms, parents));
94          out.print("\t");
95          out.print("");
96          out.print("\t");
97          out.print(ms.getXmlObject());
98          out.print("\t");
99          out.print(ms.getShortName());
100         out.print("\t");
101         out.print(ms.getName());
102         out.print("\t");
103         out.print(ms.getType());
104         out.print("\t");
105         out.print(clean(ms.getDescription()));
106         out.print("\t");
107         out.print(ms.getRequired());
108         out.print("\t");
109         out.print(ms.getCardinality());
110         out.print("\t");
111         out.print(ms.getXmlAttribute());
112         out.print("\t");
113         out.print(ms.getStatus());
114         out.print("\t");
115         out.print(ms.getImplNotes());
116         out.println("");
117         XmlType st = finder.findXmlType(stripList(ms.getType()));
118         if (st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
119             if (!parents.contains(st.getName())) {
120                 parents.push(st.getName());
121                 for (MessageStructure childMs : finder.findMessageStructures(st.getName())) {
122                     this.writeTabbedData(childMs, parents);
123                 }
124                 parents.pop();
125             }
126         }
127     }
128 
129     private String stripList(String type) {
130         if (type.endsWith("List")) {
131             return type.substring(0, type.length() - "List".length());
132         }
133         return type;
134     }
135 }