1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.schema.generator; 23 24 import java.io.File; 25 import java.io.FileNotFoundException; 26 import java.io.PrintStream; 27 import java.util.Map.Entry; 28 29 import org.apache.openoffice.ooxml.schema.model.attribute.Attribute; 30 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeGroup; 31 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeGroupReference; 32 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeReference; 33 import org.apache.openoffice.ooxml.schema.model.base.INode; 34 import org.apache.openoffice.ooxml.schema.model.base.Node; 35 import org.apache.openoffice.ooxml.schema.model.complex.ComplexType; 36 import org.apache.openoffice.ooxml.schema.model.complex.Element; 37 import org.apache.openoffice.ooxml.schema.model.schema.Schema; 38 import org.apache.openoffice.ooxml.schema.model.schema.SchemaBase; 39 import org.apache.openoffice.ooxml.schema.model.simple.Restriction; 40 import org.apache.openoffice.ooxml.schema.model.simple.SimpleType; 41 import org.apache.openoffice.ooxml.schema.model.simple.SimpleTypeReference; 42 43 public class LogGenerator 44 { Write( final File aOutputFile, final SchemaBase aSchemaBase, final Iterable<Schema> aTopLevelSchemas)45 public static void Write ( 46 final File aOutputFile, 47 final SchemaBase aSchemaBase, 48 final Iterable<Schema> aTopLevelSchemas) 49 { 50 final long nStartTime = System.currentTimeMillis(); 51 52 try 53 { 54 final LogGenerator aGenerator = new LogGenerator( 55 new PrintStream(aOutputFile), 56 aSchemaBase); 57 58 aGenerator.WriteNamespaces(aSchemaBase); 59 aGenerator.WriteTopLevelElements(aTopLevelSchemas); 60 aGenerator.WriteComplexTypes(aSchemaBase); 61 aGenerator.WriteGroups(aSchemaBase); 62 aGenerator.WriteSimpleTypes(aSchemaBase); 63 aGenerator.WriteAttributeGroups(aSchemaBase); 64 aGenerator.WriteAttributes(aSchemaBase); 65 } 66 catch (final FileNotFoundException aException) 67 { 68 aException.printStackTrace(); 69 } 70 71 final long nEndTime = System.currentTimeMillis(); 72 System.out.printf("wrote log output to '%s' in %fs\n", 73 aOutputFile.toString(), 74 (nEndTime-nStartTime)/1000.0f); 75 } 76 77 78 79 LogGenerator( final PrintStream aOut, final SchemaBase aSchemaBase)80 private LogGenerator ( 81 final PrintStream aOut, 82 final SchemaBase aSchemaBase) 83 { 84 maSchemaBase = aSchemaBase; 85 maOut = aOut; 86 } 87 88 89 90 WriteComment(final String sFormat, final Object ... aArgumentList)91 private void WriteComment (final String sFormat, final Object ... aArgumentList) 92 { 93 maOut.printf("// "+sFormat+"\n", aArgumentList); 94 } 95 96 97 98 WriteNamespaces(final SchemaBase aSchema)99 private void WriteNamespaces (final SchemaBase aSchema) 100 { 101 // Write namespace definitions. 102 WriteComment("%d Namespaces.", aSchema.Namespaces.GetCount()); 103 for (final Entry<String,String> aEntry : aSchema.Namespaces.GetSorted()) 104 { 105 maOut.printf(" %s -> %s\n", 106 aEntry.getValue()==null ? "<no-prefix>" : aEntry.getValue(), 107 aEntry.getKey()); 108 } 109 } 110 111 112 WriteTopLevelElements(final Iterable<Schema> aTopLevelSchemas)113 private void WriteTopLevelElements (final Iterable<Schema> aTopLevelSchemas) 114 { 115 // Write top level elements. 116 WriteComment("Top-level elements."); 117 for (final Schema aSchema : aTopLevelSchemas) 118 { 119 WriteComment(" Schema %s.", aSchema.GetShortName()); 120 for (final Element aElement : aSchema.TopLevelElements.GetSorted()) 121 maOut.printf(" \"%s\" -> %s\n", 122 aElement.GetElementName().GetDisplayName(), 123 aElement.GetTypeName().GetDisplayName()); 124 } 125 } 126 127 128 129 WriteComplexTypes(final SchemaBase aSchema)130 private void WriteComplexTypes (final SchemaBase aSchema) 131 { 132 WriteComment(" %d Complex Types.", aSchema.ComplexTypes.GetCount()); 133 for (final ComplexType aType : aSchema.ComplexTypes.GetSorted()) 134 { 135 WriteType(" ", aType, true); 136 } 137 } 138 139 140 141 WriteSimpleTypes(final SchemaBase aSchema)142 private void WriteSimpleTypes (final SchemaBase aSchema) 143 { 144 WriteComment(" %d Simple Types.", aSchema.SimpleTypes.GetCount()); 145 for (final SimpleType aType : aSchema.SimpleTypes.GetSorted()) 146 { 147 WriteType(" ", aType, true); 148 } 149 } 150 151 152 153 WriteGroups(final SchemaBase aSchema)154 private void WriteGroups (final SchemaBase aSchema) 155 { 156 WriteComment(" %d Groups.", aSchema.Groups.GetCount()); 157 for (final Node aType : aSchema.Groups.GetSorted()) 158 { 159 WriteType(" ", aType, true); 160 } 161 } 162 163 164 165 WriteAttributeGroups(final SchemaBase aSchema)166 private void WriteAttributeGroups (final SchemaBase aSchema) 167 { 168 WriteComment(" %d Attribute Groups.", aSchema.AttributeGroups.GetCount()); 169 for (final Node aType : aSchema.AttributeGroups.GetSorted()) 170 { 171 WriteType(" ", aType, true); 172 } 173 } 174 175 176 177 WriteAttributes(final SchemaBase aSchema)178 private void WriteAttributes (final SchemaBase aSchema) 179 { 180 WriteComment(" %d Attributes.", aSchema.Attributes.GetCount()); 181 for (final Node aType : aSchema.Attributes.GetSorted()) 182 { 183 WriteType(" ", aType, true); 184 } 185 } 186 187 188 189 WriteType( final String sIndentation, final INode aType, final boolean bIsTopLevel)190 private void WriteType ( 191 final String sIndentation, 192 final INode aType, 193 final boolean bIsTopLevel) 194 { 195 maOut.printf("%s%s", sIndentation, aType.toString()); 196 197 if (bIsTopLevel) 198 { 199 final Node aNode = (Node)aType; 200 maOut.printf(" defined at %s", 201 aNode.GetLocation()); 202 } 203 if ( ! HasChild(aType)) 204 { 205 maOut.printf(" {}\n"); 206 } 207 else 208 { 209 maOut.printf(" {\n"); 210 211 // Write attributes. 212 switch(aType.GetNodeType()) 213 { 214 case ComplexType: 215 for (final INode aAttribute : ((ComplexType)aType).GetAttributes()) 216 WriteAttribute(sIndentation+" ", aAttribute); 217 break; 218 219 case SimpleTypeReference: 220 WriteType( 221 sIndentation+" ", 222 ((SimpleTypeReference)aType).GetReferencedSimpleType(maSchemaBase), 223 false); 224 break; 225 226 default: 227 break; 228 } 229 230 231 // Write child types. 232 for (final INode aChild : aType.GetChildren()) 233 WriteType(sIndentation+" ", aChild, false); 234 235 maOut.printf("%s}\n", sIndentation); 236 } 237 } 238 239 240 241 WriteAttribute( final String sIndentation, final INode aAttribute)242 private void WriteAttribute ( 243 final String sIndentation, 244 final INode aAttribute) 245 { 246 switch(aAttribute.GetNodeType()) 247 { 248 case Attribute: 249 maOut.printf( 250 "%sattribute %s of type %s\n", 251 sIndentation, 252 ((Attribute)aAttribute).GetName().GetDisplayName(), 253 ((Attribute)aAttribute).GetTypeName().GetDisplayName()); 254 break; 255 256 case AttributeGroup: 257 maOut.printf( 258 "%sattribute group %s {\n", 259 sIndentation, 260 ((AttributeGroup)aAttribute).GetName().GetDisplayName()); 261 for (final INode aChildAttribute : ((AttributeGroup)aAttribute).GetChildren()) 262 WriteAttribute(sIndentation+" ", aChildAttribute); 263 maOut.printf("%s}\n", sIndentation); 264 break; 265 case AttributeGroupReference: 266 maOut.printf( 267 "%sreference to attribute group %s {\n", 268 sIndentation, 269 ((AttributeGroupReference)aAttribute).GetReferencedName().GetDisplayName()); 270 WriteAttribute(sIndentation+" ", ((AttributeGroupReference)aAttribute).GetReferencedAttributeGroup(maSchemaBase)); 271 maOut.printf("%s}\n", sIndentation); 272 break; 273 274 case AttributeReference: 275 maOut.printf( 276 "%sreference to attribute %s {\n", 277 sIndentation, 278 ((AttributeReference)aAttribute).GetReferencedName().GetDisplayName()); 279 WriteAttribute(sIndentation+" ", ((AttributeReference)aAttribute).GetReferencedAttribute(maSchemaBase)); 280 maOut.printf("%s}\n", sIndentation); 281 break; 282 default: 283 throw new RuntimeException(); 284 } 285 } 286 287 288 289 HasChild(final INode aType)290 private boolean HasChild (final INode aType) 291 { 292 if (aType.GetChildren().iterator().hasNext()) 293 return true; 294 295 switch (aType.GetNodeType()) 296 { 297 case ComplexType: 298 return ((ComplexType)aType).GetAttributes().iterator().hasNext(); 299 300 case SimpleTypeReference: 301 return true; 302 303 default: 304 return false; 305 } 306 } 307 308 309 310 311 private final SchemaBase maSchemaBase; 312 private final PrintStream maOut; 313 } 314