001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    package org.kuali.student.contract.model.util;
006    
007    import java.io.PrintStream;
008    import java.util.Stack;
009    
010    import org.kuali.student.contract.model.MessageStructure;
011    import org.kuali.student.contract.model.ServiceContractModel;
012    import org.kuali.student.contract.model.XmlType;
013    
014    /**
015     *
016     * @author nwright
017     */
018    public class MessageStructureHierarchyDumper {
019    
020        private ServiceContractModel model;
021        private ModelFinder finder;
022        private PrintStream out;
023    
024        public MessageStructureHierarchyDumper(PrintStream out,
025                ServiceContractModel model) {
026            this.out = out;
027            this.model = model;
028            this.finder = new ModelFinder(model);
029        }
030    
031        public void writeTabbedHeader() {
032            out.print("id");
033            out.print("\t");
034            out.print("Action");
035            out.print("\t");
036            out.print("xmlObject");
037            out.print("\t");
038            out.print("ShortName");
039            out.print("\t");
040            out.print("Name");
041            out.print("\t");
042            out.print("Type");
043            out.print("\t");
044            out.print("Description");
045            out.print("\t");
046            out.print("Required");
047            out.print("\t");
048            out.print("Cardinality");
049            out.print("\t");
050            out.print("XMLAttribute");
051            out.print("\t");
052            out.print("Status");
053            out.print("\t");
054            out.print("Feedback");
055            out.println("");
056        }
057    
058        private String clean(String str) {
059            int len = str.length();
060            StringBuffer buffer = new StringBuffer(len);
061            for (int i = 0; i < len; i++) {
062                char c = str.charAt(i);
063                // skip \n, \r, \r\n
064                switch (c) {
065                    case '\n':
066                    case '\r': // do lookahead
067                        if (i + 1 < len && str.charAt(i + 1) == '\n') {
068                            i++;
069                        }
070    
071                        buffer.append(" ");
072                        break;
073                    default:
074                        buffer.append(c);
075                }
076            }
077            return buffer.toString();
078        }
079    
080        public String calcId(MessageStructure ms, Stack<String> parents) {
081            StringBuilder bldr = new StringBuilder();
082            {
083                for (String parent : parents) {
084                    bldr.append(parent);
085                    bldr.append(".");
086                }
087            }
088            bldr.append(ms.getShortName());
089            return bldr.toString();
090        }
091    
092        public void writeTabbedData(MessageStructure ms, Stack<String> parents) {
093            out.print(calcId(ms, parents));
094            out.print("\t");
095            out.print("");
096            out.print("\t");
097            out.print(ms.getXmlObject());
098            out.print("\t");
099            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    }