1 package org.codehaus.modello.plugin.xsd;
2
3 import org.codehaus.modello.ModelloException;
4 import org.codehaus.modello.model.Model;
5 import org.codehaus.modello.model.Version;
6 import org.codehaus.modello.plugin.xsd.metadata.XsdModelMetadata;
7 import org.codehaus.modello.plugins.xml.metadata.XmlModelMetadata;
8 import org.codehaus.plexus.util.StringUtils;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class XsdModelHelper {
38 public static String getNamespace(Model model, Version version) throws ModelloException {
39 XmlModelMetadata xmlModelMetadata = (XmlModelMetadata) model.getMetadata(XmlModelMetadata.ID);
40
41 XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata(XsdModelMetadata.ID);
42
43 String namespace;
44 if (StringUtils.isNotEmpty(xsdModelMetadata.getNamespace())) {
45 namespace = xsdModelMetadata.getNamespace(version);
46 } else {
47
48 if (StringUtils.isEmpty(xmlModelMetadata.getNamespace())) {
49 throw new ModelloException("Cannot generate xsd without xmlns specification:"
50 + " <model xml.namespace='...'> or <model xsd.namespace='...'>");
51 }
52
53 namespace = xmlModelMetadata.getNamespace(version);
54 }
55
56 return namespace;
57 }
58
59 public static String getTargetNamespace(Model model, Version version, String namespace) {
60 XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata(XsdModelMetadata.ID);
61
62 String targetNamespace;
63 if (xsdModelMetadata.getTargetNamespace() == null) {
64
65 targetNamespace = namespace;
66 } else {
67 targetNamespace = xsdModelMetadata.getTargetNamespace(version);
68 }
69 return targetNamespace;
70 }
71
72 public static String getTargetNamespace(Model model, Version version) throws ModelloException {
73 return getTargetNamespace(model, version, getNamespace(model, version));
74 }
75 }