001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    /*
017     * To change this template, choose Tools | Templates
018     * and open the template in the editor.
019     */
020    package org.kuali.student.contract.model.util;
021    
022    import java.io.PrintStream;
023    import java.util.Stack;
024    
025    import org.kuali.student.contract.model.MessageStructure;
026    import org.kuali.student.contract.model.ServiceContractModel;
027    import org.kuali.student.contract.model.XmlType;
028    
029    /**
030     *
031     * @author nwright
032     */
033    public class MessageStructureHierarchyDumper {
034    
035        private ServiceContractModel model;
036        private ModelFinder finder;
037        private PrintStream out;
038    
039        public MessageStructureHierarchyDumper(PrintStream out,
040                ServiceContractModel model) {
041            this.out = out;
042            this.model = model;
043            this.finder = new ModelFinder(model);
044        }
045    
046        public void writeTabbedHeader() {
047            out.print("id");
048            out.print("\t");
049            out.print("Action");
050            out.print("\t");
051            out.print("xmlObject");
052            out.print("\t");
053            out.print("ShortName");
054            out.print("\t");
055            out.print("Name");
056            out.print("\t");
057            out.print("Type");
058            out.print("\t");
059            out.print("Description");
060            out.print("\t");
061            out.print("Required");
062            out.print("\t");
063            out.print("Cardinality");
064            out.print("\t");
065            out.print("XMLAttribute");
066            out.print("\t");
067            out.print("Status");
068            out.print("\t");
069            out.print("Feedback");
070            out.println("");
071        }
072    
073        private String clean(String str) {
074            int len = str.length();
075            StringBuffer buffer = new StringBuffer(len);
076            for (int i = 0; i < len; i++) {
077                char c = str.charAt(i);
078                // skip \n, \r, \r\n
079                switch (c) {
080                    case '\n':
081                    case '\r': // do lookahead
082                        if (i + 1 < len && str.charAt(i + 1) == '\n') {
083                            i++;
084                        }
085    
086                        buffer.append(" ");
087                        break;
088                    default:
089                        buffer.append(c);
090                }
091            }
092            return buffer.toString();
093        }
094    
095        public String calcId(MessageStructure ms, Stack<String> parents) {
096            StringBuilder bldr = new StringBuilder();
097            {
098                for (String parent : parents) {
099                    bldr.append(parent);
100                    bldr.append(".");
101                }
102            }
103            bldr.append(ms.getShortName());
104            return bldr.toString();
105        }
106    
107        public void writeTabbedData(MessageStructure ms, Stack<String> parents) {
108            out.print(calcId(ms, parents));
109            out.print("\t");
110            out.print("");
111            out.print("\t");
112            out.print(ms.getXmlObject());
113            out.print("\t");
114            out.print(ms.getShortName());
115            out.print("\t");
116            out.print(ms.getName());
117            out.print("\t");
118            out.print(ms.getType());
119            out.print("\t");
120            out.print(clean(ms.getDescription()));
121            out.print("\t");
122            out.print(ms.getRequired());
123            out.print("\t");
124            out.print(ms.getCardinality());
125            out.print("\t");
126            out.print(ms.getXmlAttribute());
127            out.print("\t");
128            out.print(ms.getStatus());
129            out.print("\t");
130            out.print(ms.getImplNotes());
131            out.println("");
132            XmlType st = finder.findXmlType(stripList(ms.getType()));
133            if (st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
134                if (!parents.contains(st.getName())) {
135                    parents.push(st.getName());
136                    for (MessageStructure childMs : finder.findMessageStructures(st.getName())) {
137                        this.writeTabbedData(childMs, parents);
138                    }
139                    parents.pop();
140                }
141            }
142        }
143    
144        private String stripList(String type) {
145            if (type.endsWith("List")) {
146                return type.substring(0, type.length() - "List".length());
147            }
148            return type;
149        }
150    }