Coverage Report - org.kuali.student.contract.model.util.HtmlContractWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlContractWriter
0%
0/108
0%
0/36
2.615
HtmlContractWriter$1
0%
0/2
N/A
2.615
HtmlContractWriter$2
0%
0/2
N/A
2.615
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may        obtain a copy of the License at
 7  
  *
 8  
  *         http://www.osedu.org/licenses/ECL-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.student.contract.model.util;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Comparator;
 21  
 import java.util.List;
 22  
 import java.util.Stack;
 23  
 
 24  
 import org.kuali.student.contract.model.MessageStructure;
 25  
 import org.kuali.student.contract.model.Service;
 26  
 import org.kuali.student.contract.model.ServiceContractModel;
 27  
 import org.kuali.student.contract.model.XmlType;
 28  
 import org.kuali.student.contract.writer.HtmlWriter;
 29  
 
 30  
 /**
 31  
  *
 32  
  * @author nwright
 33  
  */
 34  
 public class HtmlContractWriter
 35  
 {
 36  
 
 37  
  private HtmlWriter writer;
 38  
  private ServiceContractModel model;
 39  
  private ModelFinder finder;
 40  
  private String directory;
 41  
 
 42  
  public HtmlContractWriter (String directory,
 43  
                             ServiceContractModel model)
 44  0
  {
 45  0
   this.writer = new HtmlWriter (directory, "index.html",
 46  
                                 "Service Contracts Index");
 47  0
   this.directory = directory;
 48  0
   this.model = model;
 49  0
   this.finder = new ModelFinder (this.model);
 50  0
  }
 51  
 
 52  
  public void write ()
 53  
  {
 54  0
   this.writeIndexPage ();
 55  0
   for (Service service : model.getServices ())
 56  
   {
 57  0
    HtmlContractServiceWriter swriter = new HtmlContractServiceWriter (service,
 58  
                                                                       directory,
 59  
                                                                       model);
 60  0
    swriter.write ();
 61  0
   }
 62  0
   for (XmlType xmlType : model.getXmlTypes ())
 63  
   {
 64  0
    HtmlContractMessageStructureWriter msWriter =
 65  
                                       new HtmlContractMessageStructureWriter (
 66  
      xmlType,
 67  
      directory,
 68  
      model);
 69  0
    msWriter.write ();
 70  0
   }
 71  0
  }
 72  0
  private static final Comparator<XmlType> XML_TYPE_NAME_COMPARATOR =
 73  
                                           new Comparator<XmlType> ()
 74  0
  {
 75  
 
 76  
   @Override
 77  
   public int compare (XmlType e1, XmlType e2)
 78  
   {
 79  0
    return e1.getName ().toLowerCase ().compareTo (e2.getName ().toLowerCase ());
 80  
   }
 81  
  };
 82  0
  private static final Comparator<Service> SERVICE_NAME_COMPARATOR =
 83  
                                           new Comparator<Service> ()
 84  0
  {
 85  
 
 86  
   @Override
 87  
   public int compare (Service e1, Service e2)
 88  
   {
 89  0
    return e1.getName ().compareTo (e2.getName ());
 90  
   }
 91  
  };
 92  
 
 93  
  private void writeIndexPage ()
 94  
  {
 95  0
   writer.print ("<a href=\"index.html\">home</a>");
 96  0
   writer.writeTag ("h1", "Service Contracts");
 97  0
   writer.indentPrintln (
 98  
     "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
 99  0
   writer.indentPrintln (
 100  
     "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
 101  0
   writer.indentPrintln ("<b><a name=\"Services\"></a>Services</b>");
 102  0
   writer.indentPrintln (
 103  
     "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
 104  0
   writer.indentPrintln ("<ul>");
 105  0
   List<Service> services = new ArrayList (model.getServices ());
 106  0
   Collections.sort (services, SERVICE_NAME_COMPARATOR);
 107  0
   for (Service service : services)
 108  
   {
 109  0
    writer.indentPrint ("<li>");
 110  0
    writer.print ("<a href=\"" + service.getName () + ".html"
 111  
                  + "\">" + service.getName () + "</a>");
 112  0
    writer.print ("</li>");
 113  
   }
 114  0
   writer.indentPrintln ("</ul>");
 115  0
   writer.indentPrintln ("</div>");
 116  0
   writer.indentPrintln ("</div>");
 117  
 
 118  0
   this.writeAlphabeticalList ();
 119  0
   this.writeMainOrRootList ();
 120  
 
 121  0
   writer.writeHeaderBodyAndFooterOutToFile ();
 122  
 
 123  0
  }
 124  
 
 125  
  private void writeMainOrRootList ()
 126  
  {
 127  0
   Stack stack = new Stack ();
 128  0
   List<XmlType> types = this.getMainMessageStructures ();
 129  0
   writer.indentPrintln (
 130  
     "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
 131  0
   writer.indentPrintln (
 132  
     "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
 133  0
   writer.indentPrintln (
 134  
     "<b><a name=\"MessageStructures\"></a> " + types.size () + " Main (root) Message Structures</b>");
 135  0
   writer.indentPrintln (
 136  
     "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
 137  0
   writer.indentPrintln ("<ul>");
 138  0
   for (XmlType type : types)
 139  
   {
 140  0
    this.writeLink (type);
 141  0
    if ( ! stack.contains (type.getName ()))
 142  
    {
 143  0
     stack.push (type.getName ());
 144  0
     this.writeSubStructures (type, stack);
 145  0
     stack.pop ();
 146  
    }
 147  
   }
 148  0
   writer.indentPrintln ("</ul>");
 149  0
   writer.indentPrintln ("</div>");
 150  0
   writer.indentPrintln ("</div>");
 151  0
  }
 152  
 
 153  
  private String stripListOffEnd (String name)
 154  
  {
 155  0
   if (name.endsWith ("List"))
 156  
   {
 157  0
    return name.substring (0, name.length () - "List".length ());
 158  
   }
 159  0
   return name;
 160  
  }
 161  
  
 162  
  private void writeSubStructures (XmlType type, Stack stack)
 163  
  {
 164  0
   boolean first = true;
 165  0
   for (MessageStructure ms : finder.findMessageStructures (type.getName ()))
 166  
   {
 167  0
    XmlType st = finder.findXmlType (this.stripListOffEnd (ms.getType ()));
 168  0
    if ( ! st.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX))
 169  
    {
 170  0
     continue;
 171  
    }
 172  0
    if (first)
 173  
    {
 174  0
     first = false;
 175  0
     writer.indentPrintln ("<ul>");
 176  
    }
 177  0
    this.writeLink (st);
 178  0
    if ( ! stack.contains (st.getName ()))
 179  
    {
 180  0
     stack.push (st.getName ());
 181  0
     this.writeSubStructures (st, stack);
 182  0
     stack.pop ();
 183  
    }
 184  0
   }
 185  0
   if ( ! first)
 186  
   {
 187  0
    writer.indentPrintln ("</ul>");
 188  
   }
 189  0
  }
 190  
 
 191  
  private void writeLink (XmlType type)
 192  
  {
 193  0
   writer.indentPrint ("<li>");
 194  0
   writer.print ("<a href=\"" + type.getName () + ".html"
 195  
                 + "\">" + type.getName () + "</a>");
 196  0
   writer.print ("</li>");
 197  0
  }
 198  
 
 199  
  private List<XmlType> getMainMessageStructures ()
 200  
  {
 201  0
   List<XmlType> types = new ArrayList (model.getXmlTypes ().size ());
 202  0
   for (XmlType type : this.getComplexMessageStructures ())
 203  
   {
 204  0
    if (isMainMessageStructure (type))
 205  
    {
 206  0
     types.add (type);
 207  
    }
 208  
   }
 209  0
   Collections.sort (types, XML_TYPE_NAME_COMPARATOR);
 210  0
   return types;
 211  
  }
 212  
 
 213  
  private boolean isMainMessageStructure (XmlType xmlType)
 214  
  {
 215  0
   if ( ! HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages (model,
 216  
                                                                     xmlType).isEmpty ())
 217  
   {
 218  0
    return false;
 219  
   }
 220  0
   return true;
 221  
  }
 222  
 
 223  
  private List<XmlType> getComplexMessageStructures ()
 224  
  {
 225  0
   List<XmlType> types = new ArrayList (model.getXmlTypes ().size ());
 226  0
   for (XmlType type : model.getXmlTypes ())
 227  
   {
 228  0
    if (type.getPrimitive () == null)
 229  
    {
 230  0
     throw new NullPointerException (type.getName ()
 231  
                                     + " has no primitive flag set");
 232  
    }
 233  0
    if (type.getPrimitive ().equals (XmlType.COMPLEX))
 234  
    {
 235  0
     types.add (type);
 236  
    }
 237  
   }
 238  0
   Collections.sort (types, XML_TYPE_NAME_COMPARATOR);
 239  0
   return types;
 240  
  }
 241  
 
 242  
  private void writeAlphabeticalList ()
 243  
  {
 244  0
     List<XmlType> types = this.getComplexMessageStructures ();
 245  0
   writer.indentPrintln (
 246  
     "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
 247  0
   writer.indentPrintln (
 248  
     "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
 249  0
   writer.indentPrintln (
 250  
     "<b><a name=\"MessageStructures\"></a>All " + types.size () + " Message Structures in Alphabetical Order</b>");
 251  0
   writer.indentPrintln (
 252  
     "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
 253  0
   writer.indentPrintln ("<ul>");
 254  0
   for (XmlType type : types)
 255  
   {
 256  0
    this.writeLink (type);
 257  
   }
 258  0
   writer.indentPrintln ("</ul>");
 259  0
   writer.indentPrintln ("</div>");
 260  0
   writer.indentPrintln ("</div>");
 261  0
  }
 262  
 }