File Source: ThemeMetadataParser.java
/*
P/P * Method: org.apache.roller.weblogger.business.themes.ThemeMetadataParser__static_init
*/
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.business.themes;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Iterator;
24 import java.util.List;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.roller.weblogger.pojos.WeblogTemplate;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.JDOMException;
30 import org.jdom.input.SAXBuilder;
31
32
33 /**
34 * The parser for theme xml descriptors.
35 *
36 * This class unmarshalls a theme descriptor into a set of objects.
37 */
/*
P/P * Method: void org.apache.roller.weblogger.business.themes.ThemeMetadataParser()
*/
38 public class ThemeMetadataParser {
39
40
41 /**
42 * Unmarshall the given input stream into our defined
43 * set of Java objects.
44 **/
45 public ThemeMetadata unmarshall(InputStream instream)
46 throws ThemeParsingException, IOException, JDOMException {
47
/*
P/P * Method: ThemeMetadata unmarshall(InputStream)
*
* Preconditions:
* instream != null
*
* Presumptions:
* java.util.Iterator:next(...)@85 != null
* java.util.Iterator:next(...)@94 != null
* org.apache.commons.lang.StringUtils:isEmpty(...)@63 == 0
* org.jdom.Document:getRootElement(...)@57 != null
* org.jdom.Element:getChild(...)@68 != null
* ...
*
* Postconditions:
* return_value == &new ThemeMetadata(unmarshall#1)
* new HashSet(ThemeMetadata#1) num objects == 1
* new HashSet(ThemeMetadata#2) num objects == 1
* new ThemeMetadata(unmarshall#1) num objects == 1
* init'ed(return_value.author)
* init'ed(return_value.id)
* init'ed(return_value.name)
* init'ed(return_value.previewImage)
* return_value.resources == &new HashSet(ThemeMetadata#2)
* return_value.stylesheet in Addr_Set{null,&new ThemeMetadataTemplate(elementToStylesheet#1)}
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@98: {0}, {1}
* java.util.Iterator:hasNext(...)@84: {0}, {1}
* java.util.Iterator:hasNext(...)@93: {0}, {1}
* org.jdom.Element:getChild(...)@76: Addr_Set{null}, Inverse{null}
*/
48 if(instream == null)
49 throw new IOException("InputStream is null!");
50
51 ThemeMetadata theme = new ThemeMetadata();
52
53 SAXBuilder builder = new SAXBuilder();
54 Document doc = builder.build(instream);
55
56 // start at root and get theme id, name, and author
57 Element root = doc.getRootElement();
58 theme.setId(root.getChildText("id"));
59 theme.setName(root.getChildText("name"));
60 theme.setAuthor(root.getChildText("author"));
61
62 // if either id or name is null then throw a parsing exception
63 if(StringUtils.isEmpty(theme.getId()) || StringUtils.isEmpty(theme.getName())) {
64 throw new ThemeParsingException("'id' and 'name' are required theme elements");
65 }
66
67 // now grab the preview image path
68 Element previewImage = root.getChild("preview-image");
69 if(previewImage != null) {
70 theme.setPreviewImage(previewImage.getAttributeValue("path"));
71 } else {
72 throw new ThemeParsingException("No preview image specified");
73 }
74
75 // grab the stylesheet if it exists
76 Element stylesheet = root.getChild("stylesheet");
77 if(stylesheet != null) {
78 theme.setStylesheet(elementToStylesheet(stylesheet));
79 }
80
81 // now grab the static resources
82 List resources = root.getChildren("resource");
83 Iterator resourcesIter = resources.iterator();
84 while (resourcesIter.hasNext()) {
85 Element resource = (Element) resourcesIter.next();
86 theme.addResource(resource.getAttributeValue("path"));
87 }
88
89 // now grab the templates
90 boolean weblogActionTemplate = false;
91 List templates = root.getChildren("template");
92 Iterator templatesIter = templates.iterator();
93 while (templatesIter.hasNext()) {
94 Element template = (Element) templatesIter.next();
95 ThemeMetadataTemplate tmpl = elementToTemplateMetadata(template);
96 theme.addTemplate(tmpl);
97
98 if(WeblogTemplate.ACTION_WEBLOG.equals(tmpl.getAction())) {
99 weblogActionTemplate = true;
100 }
101 }
102
103 // make sure all required elements are present and values are valid
104 // check that there is a template with action='weblog'
+ 105 if(!weblogActionTemplate) {
106 throw new ThemeParsingException("did not find a template of action = 'weblog'");
107 }
108
109 return theme;
110 }
111
112
113 private ThemeMetadataTemplate elementToTemplateMetadata(Element element)
114 throws ThemeParsingException {
115
/*
P/P * Method: ThemeMetadataTemplate elementToTemplateMetadata(Element)
*
* Preconditions:
* element != null
*
* Presumptions:
* org.apache.commons.lang.StringUtils:isEmpty(...)@137 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@140 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@143 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@146 == 0
*
* Postconditions:
* return_value == &new ThemeMetadataTemplate(elementToTemplateMetadata#1)
* new ThemeMetadataTemplate(elementToTemplateMetadata#1) num objects == 1
* init'ed(return_value.action)
* init'ed(return_value.contentType)
* init'ed(return_value.contentsFile)
* init'ed(return_value.description)
* init'ed(return_value.hidden)
* init'ed(return_value.link)
* init'ed(return_value.name)
* init'ed(return_value.navbar)
* ...
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@127: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@132: {0}, {1}
*/
116 ThemeMetadataTemplate template = new ThemeMetadataTemplate();
117
118 template.setAction(element.getAttributeValue("action"));
119 template.setName(element.getChildText("name"));
120 template.setDescription(element.getChildText("description"));
121 template.setLink(element.getChildText("link"));
122 template.setTemplateLanguage(element.getChildText("templateLanguage"));
123 template.setContentType(element.getChildText("contentType"));
124 template.setContentsFile(element.getChildText("contentsFile"));
125
126 String navbar = element.getChildText("navbar");
127 if("true".equalsIgnoreCase(navbar)) {
128 template.setNavbar(true);
129 }
130
131 String hidden = element.getChildText("hidden");
132 if("true".equalsIgnoreCase(hidden)) {
133 template.setHidden(true);
134 }
135
136 // validate template
137 if(StringUtils.isEmpty(template.getAction())) {
138 throw new ThemeParsingException("templates must contain an 'action' attribute");
139 }
140 if(StringUtils.isEmpty(template.getName())) {
141 throw new ThemeParsingException("templates must contain a 'name' element");
142 }
143 if(StringUtils.isEmpty(template.getTemplateLanguage())) {
144 throw new ThemeParsingException("templates must contain a 'templateLanguage' element");
145 }
146 if(StringUtils.isEmpty(template.getContentsFile())) {
147 throw new ThemeParsingException("templates must contain a 'contentsFile' element");
148 }
149
150 return template;
151 }
152
153
154 private ThemeMetadataTemplate elementToStylesheet(Element element)
155 throws ThemeParsingException {
156
/*
P/P * Method: ThemeMetadataTemplate elementToStylesheet(Element)
*
* Preconditions:
* element != null
*
* Presumptions:
* org.apache.commons.lang.StringUtils:isEmpty(...)@166 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@169 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@172 == 0
* org.apache.commons.lang.StringUtils:isEmpty(...)@175 == 0
*
* Postconditions:
* return_value == &new ThemeMetadataTemplate(elementToStylesheet#1)
* new ThemeMetadataTemplate(elementToStylesheet#1) num objects == 1
* return_value.action == null
* return_value.contentType == null
* init'ed(return_value.contentsFile)
* init'ed(return_value.description)
* return_value.hidden == 0
* return_value.navbar == 0
* init'ed(return_value.link)
* init'ed(return_value.name)
* ...
*/
157 ThemeMetadataTemplate template = new ThemeMetadataTemplate();
158
159 template.setName(element.getChildText("name"));
160 template.setDescription(element.getChildText("description"));
161 template.setLink(element.getChildText("link"));
162 template.setTemplateLanguage(element.getChildText("templateLanguage"));
163 template.setContentsFile(element.getChildText("contentsFile"));
164
165 // validate template
166 if(StringUtils.isEmpty(template.getName())) {
167 throw new ThemeParsingException("stylesheet must contain a 'name' element");
168 }
169 if(StringUtils.isEmpty(template.getLink())) {
170 throw new ThemeParsingException("stylesheet must contain a 'link' element");
171 }
172 if(StringUtils.isEmpty(template.getTemplateLanguage())) {
173 throw new ThemeParsingException("stylesheet must contain a 'templateLanguage' element");
174 }
175 if(StringUtils.isEmpty(template.getContentsFile())) {
176 throw new ThemeParsingException("stylesheet must contain a 'contentsFile' element");
177 }
178
179 return template;
180 }
181
182 }
SofCheck Inspector Build Version : 2.18479
| ThemeMetadataParser.java |
2009-Jan-02 14:24:46 |
| ThemeMetadataParser.class |
2009-Sep-04 03:12:31 |