2 * Copyright (c) 2015. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.xml.xspf.bind;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.hedgecode.xml.xspf.PlaylistFactory;
23 import org.hedgecode.xml.xspf.rng.Annotation;
24 import org.hedgecode.xml.xspf.rng.Creator;
25 import org.hedgecode.xml.xspf.rng.Extension;
26 import org.hedgecode.xml.xspf.rng.Image;
27 import org.hedgecode.xml.xspf.rng.Info;
28 import org.hedgecode.xml.xspf.rng.Link;
29 import org.hedgecode.xml.xspf.rng.Meta;
30 import org.hedgecode.xml.xspf.rng.ObjectFactory;
31 import org.hedgecode.xml.xspf.rng.Title;
36 * @author Dmitry Samoshin aka gotty
38 public abstract class AbstractRNGBinder implements Binder {
40 protected static final ObjectFactory RNG_FACTORY = new ObjectFactory();
42 protected abstract Object getElement(Class elementClass);
44 protected abstract List<Object> getElements(Class elementClass);
46 protected abstract List<Object> getAllElements();
49 public String getTitle() {
50 Title titleElement = (Title) getElement(Title.class);
51 return titleElement != null ? titleElement.getContent() : null;
55 public void setTitle(String title) {
56 Title titleElement = (Title) getElement(Title.class);
57 if (titleElement != null)
58 getAllElements().remove(titleElement);
60 titleElement = RNG_FACTORY.createTitle();
61 titleElement.setContent(title);
62 getAllElements().add(titleElement);
67 public String getCreator() {
68 Creator creatorElement = (Creator) getElement(Creator.class);
69 return creatorElement != null ? creatorElement.getContent() : null;
73 public void setCreator(String creator) {
74 Creator creatorElement = (Creator) getElement(Creator.class);
75 if (creatorElement != null)
76 getAllElements().remove(creatorElement);
77 if (creator != null) {
78 creatorElement = RNG_FACTORY.createCreator();
79 creatorElement.setContent(creator);
80 getAllElements().add(creatorElement);
85 public String getAnnotation() {
86 Annotation annotationElement = (Annotation) getElement(Annotation.class);
87 return annotationElement != null ? annotationElement.getContent() : null;
91 public void setAnnotation(String annotation) {
92 Annotation annotationElement = (Annotation) getElement(Annotation.class);
93 if (annotationElement != null)
94 getAllElements().remove(annotationElement);
95 if (annotation != null) {
96 annotationElement = RNG_FACTORY.createAnnotation();
97 annotationElement.setContent(annotation);
98 getAllElements().add(annotationElement);
103 public String getInfo() {
104 Info infoElement = (Info) getElement(Info.class);
105 return infoElement != null ? infoElement.getValue() : null;
109 public void setInfo(String info) {
110 Info infoElement = (Info) getElement(Info.class);
111 if (infoElement != null)
112 getAllElements().remove(infoElement);
114 infoElement = RNG_FACTORY.createInfo();
115 infoElement.setValue(info);
116 getAllElements().add(infoElement);
121 public String getImage() {
122 Image imageElement = (Image) getElement(Image.class);
123 return imageElement != null ? imageElement.getValue() : null;
127 public void setImage(String image) {
128 Image imageElement = (Image) getElement(Image.class);
129 if (imageElement != null)
130 getAllElements().remove(imageElement);
132 imageElement = RNG_FACTORY.createImage();
133 imageElement.setValue(image);
134 getAllElements().add(imageElement);
139 public List<org.hedgecode.xml.xspf.Link> getLinks() {
140 List<org.hedgecode.xml.xspf.Link> links = new ArrayList<>();
141 for (Object element : getElements(Link.class)) {
142 Link linkElement = (Link) element;
144 PlaylistFactory.createLink(
145 linkElement.getRel(), linkElement.getValue()
153 public void addLink(org.hedgecode.xml.xspf.Link link) {
155 Link linkElement = RNG_FACTORY.createLink();
156 linkElement.setRel(link.getRel());
157 linkElement.setValue(link.getContent());
158 getAllElements().add(linkElement);
163 public List<org.hedgecode.xml.xspf.Meta> getMetas() {
164 List<org.hedgecode.xml.xspf.Meta> metas = new ArrayList<>();
165 for (Object element : getElements(Meta.class)) {
166 Meta metaElement = (Meta) element;
168 PlaylistFactory.createMeta(
169 metaElement.getRel(), metaElement.getContent()
177 public void addMeta(org.hedgecode.xml.xspf.Meta meta) {
179 Meta metaElement = RNG_FACTORY.createMeta();
180 metaElement.setRel(meta.getRel());
181 metaElement.setContent(meta.getContent());
182 getAllElements().add(metaElement);
187 public List<org.hedgecode.xml.xspf.Extension> getExtensions() {
188 List<org.hedgecode.xml.xspf.Extension> extensions = new ArrayList<>();
189 for (Object element : getElements(Extension.class)) {
190 Extension extensionElement = (Extension) element;
192 PlaylistFactory.createExtension(
193 extensionElement.getApplication(), extensionElement.getContent()
201 public void addExtension(org.hedgecode.xml.xspf.Extension extension) {
202 if (extension != null) {
203 Extension extensionElement = RNG_FACTORY.createExtension();
204 extensionElement.setApplication(extension.getApplication());
205 extensionElement.getContent().addAll(extension.getContent());
206 getAllElements().add(extensionElement);