/
Metadata.java
156 lines (137 loc) · 3.83 KB
/
Metadata.java
1
2
3
4
5
6
7
8
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.ui.util;
import java.net.URL;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.metadata.UICategory;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.furnace.util.Assert;
/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class Metadata implements UICommandMetadata
{
private static String[] VALID_DOC_EXTENSIONS = {
".txt.gzip",
".txt.gz",
".txt",
".asciidoc.gzip",
".asciidoc.gz",
".asciidoc" };
private String name;
private String description;
private UICategory category;
private URL docLocation;
private final Class<? extends UICommand> type;
/**
* Create a new {@link UICommandMetadata} implementation from the given {@link UICommandMetadata} origin, and the
* given {@link UICommand} type.
*/
public static Metadata from(UICommandMetadata origin, Class<? extends UICommand> type)
{
Assert.notNull(origin, "Parent UICommand must not be null.");
Assert.notNull(type, "UICommand type must not be null.");
Metadata metadata = new Metadata(type);
metadata.docLocation(origin.getDocLocation()).name(origin.getName()).description(origin.getDescription())
.category(origin.getCategory());
return metadata;
}
/**
* Create a new {@link UICommandMetadata} for the given {@link UICommand} type.
*/
public static Metadata forCommand(Class<? extends UICommand> type)
{
Assert.notNull(type, "UICommand type must not be null.");
return new Metadata(type);
}
private Metadata(Class<? extends UICommand> type)
{
this.type = type;
docLocation(getDocLocationFor(type)).name(type.getName()).category(Categories.create("Uncategorized"))
.description("No description");
}
/**
* Set the name for the corresponding {@link UICommand}.
*/
public Metadata name(String name)
{
this.name = name;
return this;
}
/**
* Set the description for the corresponding {@link UICommand}.
*/
public Metadata description(String description)
{
this.description = description;
return this;
}
/**
* Set the {@link UICategory} of the corresponding {@link UICommand}.
*/
public Metadata category(UICategory category)
{
this.category = category;
return this;
}
/**
* Set the {@link URL} document location of the corresponding {@link UICommand}.
*/
public Metadata docLocation(URL docLocation)
{
this.docLocation = docLocation;
return this;
}
private URL getDocLocationFor(Class<? extends UICommand> type)
{
URL url = null;
for (String extension : VALID_DOC_EXTENSIONS)
{
String docFileName = type.getSimpleName() + extension;
url = type.getResource(docFileName);
if (url != null)
{
break;
}
}
return url;
}
@Override
public String getName()
{
return name;
}
@Override
public String getDescription()
{
return description;
}
@Override
public UICategory getCategory()
{
return category;
}
@Override
public URL getDocLocation()
{
return docLocation;
}
@Override
public String toString()
{
return "UICommandMetadataBase {" +
"name: " + name +
", description: " + description +
", category: " + category +
", docLocation: " + docLocation + "]";
}
@Override
public Class<? extends UICommand> getType()
{
return type;
}
}