KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > ant > RegistrySerializer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.ant;
16
17 import java.util.Collection JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.apache.hivemind.ApplicationRuntimeException;
28 import org.apache.hivemind.Attribute;
29 import org.apache.hivemind.ErrorHandler;
30 import org.apache.hivemind.definition.ConfigurationPointDefinition;
31 import org.apache.hivemind.definition.ContributionDefinition;
32 import org.apache.hivemind.definition.ImplementationConstructor;
33 import org.apache.hivemind.definition.ModuleDefinition;
34 import org.apache.hivemind.definition.Occurances;
35 import org.apache.hivemind.definition.RegistryDefinition;
36 import org.apache.hivemind.definition.ImplementationDefinition;
37 import org.apache.hivemind.definition.InterceptorDefinition;
38 import org.apache.hivemind.definition.ServicePointDefinition;
39 import org.apache.hivemind.definition.Visibility;
40 import org.apache.hivemind.impl.CreateClassServiceConstructor;
41 import org.apache.hivemind.impl.DefaultErrorHandler;
42 import org.apache.hivemind.impl.ExtensionResolver;
43 import org.apache.hivemind.impl.InvokeFactoryServiceConstructor;
44 import org.apache.hivemind.impl.RegistryBuilder;
45 import org.apache.hivemind.parse.AttributeMappingDescriptor;
46 import org.apache.hivemind.parse.ConversionDescriptor;
47 import org.apache.hivemind.schema.AttributeModel;
48 import org.apache.hivemind.schema.ElementModel;
49 import org.apache.hivemind.schema.Rule;
50 import org.apache.hivemind.schema.impl.SchemaImpl;
51 import org.apache.hivemind.schema.rules.CreateObjectRule;
52 import org.apache.hivemind.schema.rules.InvokeParentRule;
53 import org.apache.hivemind.schema.rules.PushAttributeRule;
54 import org.apache.hivemind.schema.rules.PushContentRule;
55 import org.apache.hivemind.schema.rules.ReadAttributeRule;
56 import org.apache.hivemind.schema.rules.ReadContentRule;
57 import org.apache.hivemind.schema.rules.SetModuleRule;
58 import org.apache.hivemind.schema.rules.SetParentRule;
59 import org.apache.hivemind.schema.rules.SetPropertyRule;
60 import org.apache.hivemind.util.IdUtils;
61 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
62 import org.w3c.dom.Document JavaDoc;
63 import org.w3c.dom.Element JavaDoc;
64
65 /**
66  * This class serializes a set of {@link ModuleDefinition module descriptors} into a
67  * {@link Document XML document}. The set of module descriptors to process is specified indirectly
68  * by supplying one or several {@link ModuleDefinitionProvider} (see
69  * {@link #addModuleDefinitionProvider(ModuleDefinitionProvider)}). In this respect this class is
70  * used the same way as {@link org.apache.hivemind.impl.RegistryBuilder}. There is even a
71  * corresponding {@link #createDefaultRegistryDocument() static method} to serialize the modules of
72  * the default registry.
73  * <p>
74  * The resulting XML file does not conform to the hivemind module deployment descriptor schema. The
75  * following changes occur:
76  * <ul>
77  * <li>The outermost element is &lt;registry&gt; (which contains a list of &lt;module&gt;)
78  * <li>A unique id (unique within the file) is assigned to each &lt;module&gt;,
79  * &lt;configuration-point&gt;, &lt;service-point&gt;, &lt;contribution&gt;, &tl;schema&gt; and
80  * &lt;implementation&gt; (this is to make it easier to generate links and anchors)
81  * <li>Unqualified ids are converted to qualified ids (whereever possible).
82  * </ul>
83  *
84  * HINT: In Version 2.0 this class is broken.
85  * Since registry definitions are no longer xml centric and a lot of other concepts are generalized too
86  * (for example schemas are hidden by ConfigurationParsers now) it's much more difficult to
87  * build a visualization of a registry.
88  * A possible solution: Build up a registry of renderers. Each renderer is responsible for
89  * rendering a certain type of definition object (ConfigurationPointDefinition, COnfigurationParserDefinition)
90  * The renderers are contributed by the core modules (framework, xml, annotation) for example
91  * via the manifest.
92  * The serializer then traverses a registry definition and searches for the renderer best
93  * fitting the current element. The XmlContributionImpl could be rendered by a standard
94  * contribution renderer or if registered by a specialized xml contribution renderer.
95  * All renderers must adhere to some general xml structur, they can't introduce new elements
96  * because the xslt wouldn't know how to deal with them.
97  *
98  *
99  * @author Knut Wannheden
100  * @since 1.1
101  */

102 public class RegistrySerializer
103 {
104     private Set JavaDoc _processedSchemas = new HashSet JavaDoc();
105
106     private RegistryDefinition _registryDefinition;
107
108     private ErrorHandler _handler;
109
110     private Document JavaDoc _document;
111
112     private ModuleDefinition _md;
113
114     public RegistrySerializer()
115     {
116         _handler = new DefaultErrorHandler();
117     }
118
119     public Document JavaDoc createRegistryDocument(RegistryDefinition registryDefinition)
120     {
121         _registryDefinition = registryDefinition;
122         ExtensionResolver resolver = new ExtensionResolver(_registryDefinition, _handler);
123         resolver.resolveExtensions();
124         
125         DocumentBuilder JavaDoc builder = getBuilder();
126
127         _document = builder.newDocument();
128
129         Element JavaDoc registry = _document.createElement("registry");
130
131 // processRegistryNatures(registry);
132

133         _document.appendChild(registry);
134
135         for (Iterator JavaDoc i = _registryDefinition.getModules().iterator(); i.hasNext();)
136         {
137             ModuleDefinition module = (ModuleDefinition) i.next();
138
139             Element JavaDoc moduleElement = getModuleElement(module);
140             registry.appendChild(moduleElement);
141             
142         }
143
144         return _document;
145     }
146
147     private Element JavaDoc getModuleElement(ModuleDefinition md)
148     {
149         _md = md;
150         Element JavaDoc module = _document.createElement("module");
151
152         module.setAttribute("id", md.getId());
153         module.setAttribute("package", md.getPackageName());
154
155 // module.appendChild(_document.createTextNode(md.getAnnotation()));
156

157         addDependencies(module);
158
159         addServicePoints(module);
160
161         addConfigurationPoints(module);
162
163         // No unresolved extensions left, since resolveExtensions was called on RegistryDefinition
164
// addContributions(module);
165
//
166
// addImplementations(module);
167

168 // addSubModules(module);
169

170         return module;
171     }
172
173     private void addDependencies(Element JavaDoc module)
174     {
175         Collection JavaDoc dependencies = _md.getDependencies();
176
177         if (dependencies != null)
178         {
179             for (Iterator JavaDoc i = dependencies.iterator(); i.hasNext();)
180             {
181                 String JavaDoc dd = (String JavaDoc) i.next();
182
183                 Element JavaDoc dependency = getDependencyElement(dd);
184
185                 module.appendChild(dependency);
186             }
187         }
188     }
189
190     private void addServicePoints(Element JavaDoc module)
191     {
192         Collection JavaDoc servicePoints = _md.getServicePoints();
193
194         if (servicePoints != null)
195         {
196             for (Iterator JavaDoc i = servicePoints.iterator(); i.hasNext();)
197             {
198                 ServicePointDefinition spd = (ServicePointDefinition) i.next();
199
200                 Element JavaDoc servicePoint = getServicePointElement(spd);
201
202                 module.appendChild(servicePoint);
203
204             }
205         }
206     }
207
208     private void addConfigurationPoints(Element JavaDoc module)
209     {
210         Collection JavaDoc configurationPoints = _md.getConfigurationPoints();
211
212         if (configurationPoints != null)
213         {
214             for (Iterator JavaDoc i = configurationPoints.iterator(); i.hasNext();)
215             {
216                 ConfigurationPointDefinition cpd = (ConfigurationPointDefinition) i.next();
217
218                 Element JavaDoc configurationPoint = getConfigurationPointElement(cpd);
219
220                 module.appendChild(configurationPoint);
221             }
222         }
223     }
224
225 // private void addContributions(Element module)
226
// {
227
// List contributions = _md.getContributions();
228
//
229
// if (contributions != null)
230
// {
231
// for (Iterator i = contributions.iterator(); i.hasNext();)
232
// {
233
// ContributionDefinition cd = (ContributionDefinition) i.next();
234
//
235
// Element contribution = getContributionElement(cd);
236
//
237
// module.appendChild(contribution);
238
// }
239
// }
240
// }
241

242 // private void addImplementations(Element module)
243
// {
244
// List implementations = _md.getImplementations();
245
//
246
// if (implementations != null)
247
// {
248
// for (Iterator i = implementations.iterator(); i.hasNext();)
249
// {
250
// ImplementationDefinition id = (ImplementationDefinition) i.next();
251
//
252
// Element implementation = getImplementationElement(id);
253
//
254
// module.appendChild(implementation);
255
// }
256
// }
257
// }
258

259 // private void processRegistryNatures(Element module)
260
// {
261
// XmlRegistryNature xmlNature = (XmlRegistryNature) _registryDefinition.getNature(XmlRegistryNature.class);
262
// if (xmlNature != null) {
263
// addSchemas(module, xmlNature);
264
// }
265
// }
266
//
267
// private void addSchemas(Element module, XmlRegistryNature xmlNature)
268
// {
269
// Collection schemas = xmlNature.getSchemas();
270
//
271
// for (Iterator i = schemas.iterator(); i.hasNext();)
272
// {
273
// SchemaImpl s = (SchemaImpl) i.next();
274
//
275
// addSchema(module, s, "schema");
276
// }
277
// }
278

279 // private void addSubModules(Element module)
280
// {
281
// List subModules = _md.getSubModules();
282
//
283
// if (subModules != null)
284
// {
285
// for (Iterator i = subModules.iterator(); i.hasNext();)
286
// {
287
// SubModuleDefinition smd = (SubModuleDefinition) i.next();
288
//
289
// Element subModule = getSubModuleElement(smd);
290
//
291
// module.appendChild(subModule);
292
// }
293
// }
294
// }
295

296     private Element JavaDoc getDependencyElement(String JavaDoc dependsOn)
297     {
298         Element JavaDoc dependency = _document.createElement("dependency");
299
300         dependency.setAttribute("module-id", dependsOn);
301
302         return dependency;
303     }
304
305     private Element JavaDoc getServicePointElement(ServicePointDefinition spd)
306     {
307         Element JavaDoc servicePoint = _document.createElement("service-point");
308
309         servicePoint.setAttribute("id", qualify(spd.getId()));
310         servicePoint.setAttribute("interface", spd.getInterfaceClassName());
311 // servicePoint.appendChild(_document.createTextNode(spd.getAnnotation()));
312

313         if (spd.getVisibility() == Visibility.PRIVATE)
314             servicePoint.setAttribute("visibility", "private");
315         
316         if (spd instanceof XmlServicePointDefinitionImpl)
317             processXmlServicePoint(servicePoint, (XmlServicePointDefinitionImpl) spd);
318         
319         ImplementationDefinition ib = spd.getDefaultImplementation();
320
321         if (ib != null)
322         {
323             Element JavaDoc instanceBuilder = getInstanceBuilderElement(ib, ib.getServiceConstructor());
324
325             servicePoint.appendChild(instanceBuilder);
326         }
327
328         Collection JavaDoc interceptors = spd.getInterceptors();
329
330         if (interceptors != null)
331         {
332             for (Iterator JavaDoc i = interceptors.iterator(); i.hasNext();)
333             {
334                 InterceptorDefinition icd = (InterceptorDefinition) i.next();
335
336                 Element JavaDoc interceptor = getInterceptorElement(icd);
337
338                 servicePoint.appendChild(interceptor);
339             }
340         }
341
342         return servicePoint;
343     }
344
345     private void processXmlServicePoint(Element JavaDoc servicePointElement, XmlServicePointDefinitionImpl xmlServicePoint)
346     {
347         if (xmlServicePoint.getParametersCount() != Occurances.REQUIRED)
348             servicePointElement.setAttribute("parameters-occurs", xmlServicePoint.getParametersCount().getName()
349                     .toLowerCase());
350         if (xmlServicePoint.getParametersSchema() != null)
351             addSchema(servicePointElement, (SchemaImpl) xmlServicePoint.getParametersSchema(), "parameters-schema");
352 // else if (xmlNature.getParametersSchemaId() != null)
353
// servicePoint.setAttribute("parameters-schema-id", qualify(spd.getParametersSchemaId()));
354

355     }
356
357     private Element JavaDoc getConfigurationPointElement(ConfigurationPointDefinition cpd)
358     {
359         Element JavaDoc configurationPoint = _document.createElement("configuration-point");
360
361         configurationPoint.setAttribute("id", qualify(cpd.getId()));
362         if (cpd.getVisibility() == Visibility.PRIVATE)
363             configurationPoint.setAttribute("visibility", "private");
364
365 // configurationPoint.appendChild(_document.createTextNode(cpd.getAnnotation()));
366

367 // processConfigurationPointNatures(configurationPoint, cpd);
368

369         Collection JavaDoc contributions = cpd.getContributions();
370         for (Iterator JavaDoc iter = contributions.iterator(); iter.hasNext();)
371         {
372             ContributionDefinition contribution = (ContributionDefinition) iter.next();
373             Element JavaDoc contributionElement = getContributionElement(contribution, cpd.getId());
374
375             configurationPoint.appendChild(contributionElement);
376             
377         }
378         
379         return configurationPoint;
380     }
381     
382 // private void addConfigurationPointSchemas(Element configurationPoint, XmlConfigurationPointNature xmlNature)
383
// {
384
// if (xmlNature.getSchema() != null)
385
// addSchema(configurationPoint, (SchemaImpl) xmlNature.getSchema(), "schema");
386
//// else if (cpd.getContributionsSchemaId() != null)
387
//// configurationPoint.setAttribute("schema-id", qualify(cpd.getContributionsSchemaId()));
388
// }
389

390     private Element JavaDoc getContributionElement(ContributionDefinition cd, String JavaDoc configurationPointId)
391     {
392         Element JavaDoc contribution = _document.createElement("contribution");
393
394         contribution.setAttribute("configuration-id", qualify(configurationPointId));
395
396 // if (cd.getConditionalExpression() != null)
397
// contribution.setAttribute("if", cd.getConditionalExpression());
398

399 // List parameters = cd.getElements();
400
//
401
// if (parameters != null)
402
// {
403
// for (Iterator i = parameters.iterator(); i.hasNext();)
404
// {
405
// org.apache.hivemind.Element parameter = (org.apache.hivemind.Element) i.next();
406
//
407
// Element element = getParamterElement(parameter);
408
//
409
// contribution.appendChild(element);
410
// }
411
// }
412

413 // contribution.appendChild(_document.createTextNode(cd.getAnnotation()));
414

415         return contribution;
416     }
417
418 // private Element getImplementationElement(ImplementationDefinition id)
419
// {
420
// Element implementation = _document.createElement("implementation");
421
//
422
// implementation.setAttribute("service-id", qualify(id.getServiceId()));
423
//
424
// if (id.getConditionalExpression() != null)
425
// implementation.setAttribute("if", id.getConditionalExpression());
426
//
427
// implementation.appendChild(_document.createTextNode(id.getAnnotation()));
428
//
429
// ServiceImplementationConstructor ib = id.getServiceConstructor();
430
//
431
// if (ib != null)
432
// {
433
// Element instanceBuilder = getInstanceBuilderElement(id, ib);
434
//
435
// implementation.appendChild(instanceBuilder);
436
// }
437
//
438
// return implementation;
439
// }
440

441 // private Element getSubModuleElement(SubModuleDefinition smd)
442
// {
443
// Element subModule = _document.createElement("sub-module");
444
//
445
// subModule.setAttribute("descriptor", smd.getDefinition().getPath());
446
//
447
// return subModule;
448
// }
449

450     private Element JavaDoc getInstanceBuilderElement(ImplementationDefinition id, ImplementationConstructor ib)
451     {
452         Element JavaDoc instanceBuilder;
453         
454         if (ib instanceof CreateClassServiceConstructor)
455         {
456             CreateClassServiceConstructor cid = (CreateClassServiceConstructor) ib;
457             instanceBuilder = _document.createElement("create-instance");
458
459             instanceBuilder.setAttribute("class", cid.getInstanceClassName());
460             if (!id.getServiceModel().equals("singleton"))
461                 instanceBuilder.setAttribute("model", id.getServiceModel());
462         }
463         else
464         {
465             InvokeFactoryServiceConstructor ifd = (InvokeFactoryServiceConstructor) ib;
466             instanceBuilder = _document.createElement("invoke-factory");
467
468             if (!ifd.getFactoryServiceId().equals("hivemind.BuilderFactory"))
469                 instanceBuilder.setAttribute("service-id", qualify(ifd.getFactoryServiceId()));
470             if (id.getServiceModel() != null)
471                 instanceBuilder.setAttribute("model", id.getServiceModel());
472
473             List JavaDoc parameters = ifd.getParameters();
474
475             if (parameters != null)
476             {
477                 for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
478                 {
479                     org.apache.hivemind.Element parameter = (org.apache.hivemind.Element) i.next();
480
481                     Element JavaDoc element = getParamterElement(parameter);
482
483                     instanceBuilder.appendChild(element);
484                 }
485             }
486         }
487
488         return instanceBuilder;
489     }
490
491     private Element JavaDoc getInterceptorElement(InterceptorDefinition icd)
492     {
493         Element JavaDoc interceptor = _document.createElement("interceptor");
494
495 // interceptor.setAttribute("service-id", qualify(icd.getName()));
496
// if (icd.getBefore() != null)
497
// interceptor.setAttribute("before", icd.getBefore());
498
// if (icd.getAfter() != null)
499
// interceptor.setAttribute("after", icd.getAfter());
500
return interceptor;
501     }
502
503     private Element JavaDoc getParamterElement(org.apache.hivemind.Element parameter)
504     {
505         Element JavaDoc element = _document.createElement(parameter.getElementName());
506
507         List JavaDoc attributes = parameter.getAttributes();
508
509         for (Iterator JavaDoc i = attributes.iterator(); i.hasNext();)
510         {
511             Attribute attribute = (Attribute) i.next();
512
513             element.setAttribute(attribute.getName(), attribute.getValue());
514         }
515
516         List JavaDoc elements = parameter.getElements();
517
518         for (Iterator JavaDoc i = elements.iterator(); i.hasNext();)
519         {
520             org.apache.hivemind.Element nestedParameter = (org.apache.hivemind.Element) i.next();
521
522             element.appendChild(getParamterElement(nestedParameter));
523         }
524
525         return element;
526     }
527
528     private void addSchema(Element JavaDoc container, SchemaImpl s, String JavaDoc elementName)
529     {
530         if (_processedSchemas.contains(s))
531             return;
532
533         Element JavaDoc schema = _document.createElement(elementName);
534
535         if (s.getId() != null)
536             schema.setAttribute("id", qualify(s.getId()));
537
538         if (s.getVisibility() == Visibility.PRIVATE)
539             schema.setAttribute("visibility", "private");
540
541         schema.appendChild(_document.createTextNode(s.getAnnotation()));
542
543         for (Iterator JavaDoc j = s.getElementModel().iterator(); j.hasNext();)
544         {
545             ElementModel em = (ElementModel) j.next();
546
547             Element JavaDoc element = getElementElement(em);
548
549             schema.appendChild(element);
550         }
551
552         container.appendChild(schema);
553
554         _processedSchemas.add(s);
555     }
556
557     private Element JavaDoc getRulesElement(ElementModel em)
558     {
559         Element JavaDoc rules = _document.createElement("rules");
560
561         for (Iterator JavaDoc i = em.getRules().iterator(); i.hasNext();)
562         {
563             Rule r = (Rule) i.next();
564
565             Element JavaDoc rule = null;
566
567             if (r instanceof CreateObjectRule)
568             {
569                 CreateObjectRule cor = (CreateObjectRule) r;
570                 rule = _document.createElement("create-object");
571
572                 rule.setAttribute("class", cor.getClassName());
573             }
574             else if (r instanceof InvokeParentRule)
575             {
576                 InvokeParentRule ipr = (InvokeParentRule) r;
577                 rule = _document.createElement("invoke-parent");
578
579                 rule.setAttribute("method", ipr.getMethodName());
580                 if (ipr.getDepth() != 1)
581                     rule.setAttribute("depth", Integer.toString(ipr.getDepth()));
582             }
583             else if (r instanceof PushAttributeRule)
584             {
585                 PushAttributeRule par = (PushAttributeRule) r;
586                 rule = _document.createElement("push-attribute");
587
588                 rule.setAttribute("attribute", par.getAttributeName());
589             }
590             else if (r instanceof PushContentRule)
591             {
592                 rule = _document.createElement("push-content");
593             }
594             else if (r instanceof ReadAttributeRule)
595             {
596                 ReadAttributeRule rar = (ReadAttributeRule) r;
597                 rule = _document.createElement("read-attribute");
598
599                 rule.setAttribute("property", rar.getPropertyName());
600                 rule.setAttribute("attribute", rar.getAttributeName());
601                 if (!rar.getSkipIfNull())
602                     rule.setAttribute("skip-if-null", "false");
603                 if (rar.getTranslator() != null)
604                     rule.setAttribute("translator", rar.getTranslator());
605             }
606             else if (r instanceof ReadContentRule)
607             {
608                 ReadContentRule rcr = (ReadContentRule) r;
609                 rule = _document.createElement("read-content");
610
611                 rule.setAttribute("property", rcr.getPropertyName());
612             }
613             else if (r instanceof SetModuleRule)
614             {
615                 SetModuleRule smr = (SetModuleRule) r;
616                 rule = _document.createElement("set-module");
617
618                 rule.setAttribute("property", smr.getPropertyName());
619             }
620             else if (r instanceof SetParentRule)
621             {
622                 SetParentRule spr = (SetParentRule) r;
623                 rule = _document.createElement("set-parent");
624
625                 rule.setAttribute("property", spr.getPropertyName());
626             }
627             else if (r instanceof SetPropertyRule)
628             {
629                 SetPropertyRule spr = (SetPropertyRule) r;
630                 rule = _document.createElement("set-property");
631
632                 rule.setAttribute("property", spr.getPropertyName());
633                 rule.setAttribute("value", spr.getValue());
634             }
635             else if (r instanceof ConversionDescriptor)
636             {
637                 ConversionDescriptor cd = (ConversionDescriptor) r;
638                 rule = _document.createElement("conversion");
639
640                 rule.setAttribute("class", cd.getClassName());
641                 if (!cd.getParentMethodName().equals("addElement"))
642                     rule.setAttribute("parent-method", cd.getParentMethodName());
643
644                 for (Iterator JavaDoc j = cd.getAttributeMappings().iterator(); j.hasNext();)
645                 {
646                     AttributeMappingDescriptor amd = (AttributeMappingDescriptor) j.next();
647
648                     Element JavaDoc map = _document.createElement("map");
649
650                     map.setAttribute("attribute", amd.getAttributeName());
651                     map.setAttribute("property", amd.getPropertyName());
652
653                     rule.appendChild(map);
654                 }
655             }
656             else
657             {
658                 rule = _document.createElement("custom");
659
660                 rule.setAttribute("class", r.getClass().getName());
661             }
662
663             if (rule != null)
664                 rules.appendChild(rule);
665         }
666         return rules;
667     }
668
669     private Element JavaDoc getElementElement(ElementModel em)
670     {
671         Element JavaDoc element = _document.createElement("element");
672         element.setAttribute("name", em.getElementName());
673
674         element.appendChild(_document.createTextNode(em.getAnnotation()));
675
676         for (Iterator JavaDoc i = em.getAttributeModels().iterator(); i.hasNext();)
677         {
678             AttributeModel am = (AttributeModel) i.next();
679
680             Element JavaDoc attribute = getAttributeElement(am);
681
682             element.appendChild(attribute);
683         }
684
685         for (Iterator JavaDoc i = em.getElementModel().iterator(); i.hasNext();)
686         {
687             ElementModel nestedEm = (ElementModel) i.next();
688
689             Element JavaDoc nestedElement = getElementElement(nestedEm);
690
691             element.appendChild(nestedElement);
692         }
693
694         if (!em.getRules().isEmpty())
695         {
696             Element JavaDoc rules = getRulesElement(em);
697
698             element.appendChild(rules);
699         }
700
701         return element;
702     }
703
704     private Element JavaDoc getAttributeElement(AttributeModel am)
705     {
706         Element JavaDoc attribute = _document.createElement("attribute");
707
708         attribute.setAttribute("name", am.getName());
709         if (am.isRequired())
710             attribute.setAttribute("required", "true");
711         if (am.isUnique())
712             attribute.setAttribute("unique", "true");
713         if (am.getDefault() != null)
714             attribute.setAttribute("default", am.getDefault());
715         if (!am.getTranslator().equals("smart"))
716             attribute.setAttribute("translator", am.getTranslator());
717
718         attribute.appendChild(_document.createTextNode(am.getAnnotation()));
719
720         return attribute;
721     }
722
723     private String JavaDoc qualify(String JavaDoc id)
724     {
725         return IdUtils.qualify(_md.getId(), id);
726     }
727
728     private DocumentBuilder JavaDoc getBuilder()
729     {
730         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
731
732         factory.setIgnoringComments(true);
733
734         try
735         {
736             return factory.newDocumentBuilder();
737         }
738         catch (ParserConfigurationException JavaDoc e)
739         {
740             throw new ApplicationRuntimeException(e);
741         }
742     }
743
744     public static Document JavaDoc createDefaultRegistryDocument()
745     {
746         RegistryBuilder builder = new RegistryBuilder();
747         builder.autoDetectModules();
748
749         RegistrySerializer serializer = new RegistrySerializer();
750
751         return serializer.createRegistryDocument(builder.getRegistryDefinition());
752     }
753 }
Popular Tags