KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > junit > DumpUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-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  */

17 package org.apache.ws.jaxme.xs.junit;
18
19 import org.apache.ws.jaxme.xs.XSAttributable;
20 import org.apache.ws.jaxme.xs.XSAttribute;
21 import org.apache.ws.jaxme.xs.XSComplexType;
22 import org.apache.ws.jaxme.xs.XSElement;
23 import org.apache.ws.jaxme.xs.XSElementOrAttrRef;
24 import org.apache.ws.jaxme.xs.XSGroup;
25 import org.apache.ws.jaxme.xs.XSIdentityConstraint;
26 import org.apache.ws.jaxme.xs.XSKeyRef;
27 import org.apache.ws.jaxme.xs.XSParticle;
28 import org.apache.ws.jaxme.xs.XSType;
29 import org.xml.sax.SAXException JavaDoc;
30
31
32 /**
33  * Debug tool. Prints an XS data object to standard out.
34  *
35  * @author <a HREF="mailto:mrck1996@yahoo.co.uk">Chris Kirk</a>
36  */

37 public class DumpUtils {
38  private static void dumpElements( XSElement[] elements, String JavaDoc prefix )
39     throws SAXException JavaDoc
40   {
41     int numElements = elements.length;
42
43     for ( int i=0; i<numElements; i++ ) {
44       dumpElement( elements[i], prefix );
45     }
46   }
47
48   private static void dumpElement( XSElement element, String JavaDoc prefix )
49     throws SAXException JavaDoc
50   {
51     String JavaDoc indented = " " + prefix;
52     System.out.println(
53       prefix + "Element "+System.identityHashCode(element)
54       + ": " + element.getName()
55     );
56
57     dumpType( element.getType(), indented );
58     dumpIdentityConstraints( element.getIdentityConstraints(), indented );
59     dumpKeyRefs( element.getKeyRefs(), indented );
60    }
61
62   private static void dumpType( XSType type, String JavaDoc prefix )
63     throws SAXException JavaDoc
64   {
65     String JavaDoc indented = " " + prefix;
66     System.out.print( prefix + "Type: " );
67
68     if ( type.isSimple() ) {
69       System.out.println( "simple - " + type.getName() );
70     } else {
71       System.out.println( "complex - " + type.getName() );
72
73       dumpComplexType( type.getComplexType(), indented );
74     }
75   }
76
77   private static void dumpComplexType( XSComplexType type, String JavaDoc prefix )
78     throws SAXException JavaDoc
79   {
80     String JavaDoc indented = prefix;
81     XSAttributable[] attributables = type.getAttributes();
82     int numAttribables = attributables.length;
83
84     for ( int i=0; i<numAttribables; i++ ) {
85       dumpAttributable( attributables[i], indented );
86     }
87
88     if ( !type.isEmpty() ) {
89       dumpParticle( type.getParticle(), indented );
90     }
91   }
92
93   private static void dumpIdentityConstraints(
94     XSIdentityConstraint[] constraints,
95     String JavaDoc prefix
96   ) {
97     int numConstraints = constraints.length;
98
99     for ( int i=0; i<numConstraints; i++ ) {
100       dumpIdentityConstraint( constraints[i], prefix );
101     }
102   }
103
104   private static void dumpKeyRefs( XSKeyRef[] keyRefs, String JavaDoc prefix ) {
105     int numKeyRefs = keyRefs == null ? 0 : keyRefs.length;
106
107     for ( int i=0; i<numKeyRefs; i++ ) {
108       dumpKeyRef( keyRefs[i], prefix );
109     }
110   }
111
112   private static void dumpIdentityConstraint(
113     XSIdentityConstraint constraint,
114     String JavaDoc prefix
115   ) {
116     System.out.println(
117       prefix + "constraint: " + constraint.getName()
118       + (constraint.isUnique() ? " (unique)" : "")
119     );
120
121     dumpMatchCriteria( constraint.getMatchCriteria(), prefix + " " );
122   }
123
124   private static void dumpKeyRef( XSKeyRef keyRef, String JavaDoc prefix ) {
125     System.out.println(
126       prefix + "keyref: " + keyRef.getName() + ": refers "
127       + keyRef.getIdentityConstraint()
128     );
129
130     dumpMatchCriteria( keyRef.getMatchCriteria(), prefix + " " );
131   }
132
133   private static void dumpMatchCriteria(
134     XSElementOrAttrRef[][] criteria,
135     String JavaDoc prefix
136   ) {
137     int numKeyParts = criteria.length;
138
139     for ( int i=0; i<numKeyParts; i++ ) {
140       XSElementOrAttrRef[] keys = criteria[i];
141
142       int numOptions = keys.length;
143       for ( int j=0; j<numOptions; j++ ) {
144         dumpElementOrAttrRef( keys[j], i + ": " );
145       }
146     }
147   }
148
149   private static void dumpElementOrAttrRef(
150     XSElementOrAttrRef ref, String JavaDoc prefix
151   ) {
152     if ( ref.isAttributeRef() ) {
153       System.out.println( prefix + ref.getAttribute().getName() + " (attr) " );
154     } else {
155       System.out.println( prefix + ref.getElement().getName() + " (ele) " );
156     }
157   }
158
159   private static void dumpAttributable(
160     XSAttributable attributable,
161     String JavaDoc prefix
162   ) throws SAXException JavaDoc {
163     if ( attributable instanceof XSAttribute ) {
164       XSAttribute attr = (XSAttribute) attributable;
165       System.out.println(
166         prefix + "attribute " + System.identityHashCode(attr) + ": "
167         + attr.getName() + " "
168         + (attr.getType().isSimple() ? "simple" : "complex!!!")
169         + (attr.isOptional() ? " optional" : " required")
170       );
171     } else {
172       System.out.println( prefix + "??? attrributable " + attributable );
173     }
174   }
175
176   private static void dumpParticle( XSParticle particle, String JavaDoc prefix )
177     throws SAXException JavaDoc
178   {
179     String JavaDoc indented = " " + prefix;
180     System.out.print(
181       prefix + " particle: min=" + particle.getMinOccurs()
182       + " max=" + particle.getMaxOccurs() + " particle_type="
183       + particle.getType()
184     );
185
186     if ( particle.isElement() ) {
187       System.out.println( " element" );
188       dumpElement( particle.getElement(), indented );
189     } else if ( particle.isGroup() ) {
190       System.out.println( " group" );
191       dumpGroup( particle.getGroup(), indented );
192     } else if ( particle.isWildcard() ) {
193       System.out.println( " wildcard" );
194     }
195   }
196
197   private static void dumpGroup( XSGroup group, String JavaDoc prefix ) throws SAXException JavaDoc {
198     String JavaDoc indented = " " + prefix;
199     System.out.println( prefix + "group: name=" + group.getName() );
200
201     XSParticle[] particles = group.getParticles();
202     int numParticles = particles.length;
203     for ( int i=0; i<numParticles; i++ ) {
204       dumpParticle( particles[i], indented );
205     }
206   }
207 }
208
Popular Tags