forked from imyousuf/smart-cms
/
XMLContentTypeDefinitionParser.java
106 lines (96 loc) · 4.18 KB
/
XMLContentTypeDefinitionParser.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
/*
*
* This is a simple Content Management System (CMS)
* Copyright (C) 2010 Imran M Yousuf (imyousuf@smartitengineering.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.smartitengineering.cms.spi.impl.type.validator;
import com.smartitengineering.cms.api.exception.InvalidReferenceException;
import com.smartitengineering.cms.api.workspace.WorkspaceId;
import com.smartitengineering.cms.api.common.MediaType;
import com.smartitengineering.cms.api.factory.type.WritableContentType;
import com.smartitengineering.cms.api.type.FieldDef;
import com.smartitengineering.cms.api.type.MutableContentType;
import com.smartitengineering.cms.api.type.MutableFieldDef;
import com.smartitengineering.cms.spi.SmartContentSPI;
import com.smartitengineering.cms.spi.type.ContentTypeDefinitionParser;
import com.smartitengineering.cms.spi.type.PersistableContentType;
import com.smartitengineering.cms.type.xml.XMLParserIntrospector;
import com.smartitengineering.cms.type.xml.XmlConstants;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Element;
import nu.xom.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author kaisar
*/
public class XMLContentTypeDefinitionParser implements ContentTypeDefinitionParser {
private final transient Logger logger = LoggerFactory.getLogger(getClass());
@Override
public Collection<WritableContentType> parseStream(WorkspaceId workspaceId, InputStream inputStream) throws
InvalidReferenceException {
ContentTypeDefinitionValidator parser = new ContentTypeDefinitionValidator(workspaceId, inputStream, new XMLParserIntrospector() {
@Override
public MutableContentType createMutableContentType() {
return SmartContentSPI.getInstance().getPersistableDomainFactory().createPersistableContentType();
}
@Override
public void processMutableContentType(MutableContentType type, Element element) {
if(type.getPrimaryFieldDef() != null) {
FieldDef fieldDef = type.getPrimaryFieldDef();
if(fieldDef instanceof MutableFieldDef) {
MutableFieldDef mutableFieldDef = (MutableFieldDef) fieldDef;
mutableFieldDef.setRequired(true);
}
}
if (type instanceof PersistableContentType) {
PersistableContentType contentType = (PersistableContentType) type;
contentType.setRepresentations(Collections.singletonMap(MediaType.APPLICATION_XML, createRootNodeAndAddChild(
element.copy()).toXML()));
}
}
});
Collection<MutableContentType> parse = new ArrayList<MutableContentType>();
parse = parser.getIfValid();
final List<WritableContentType> list = new ArrayList<WritableContentType>(parse.size());
for (MutableContentType type : parse) {
list.add((WritableContentType) type);
}
return list;
}
@Override
public Collection<MediaType> getSupportedTypes() {
return Collections.singletonList(MediaType.APPLICATION_XML);
}
protected Element createRootNodeAndAddChild(Node childNode) {
Element root = new Element(XmlConstants.CONTENT_TYPES, XmlConstants.NAMESPACE);
Attribute attr = new Attribute("xsi:schemaLocation", XmlConstants.XSI_NAMESPACE, new StringBuilder(
XmlConstants.NAMESPACE).append(' ').append(
SmartContentSPI.getInstance().getSchemaLocationForContentTypeXml()).toString());
root.addAttribute(attr);
root.appendChild(childNode);
if (logger.isDebugEnabled()) {
logger.debug(root.toXML());
}
return root;
}
}