Skip to content

Method: createLangStringNode(String, String, String)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.serialization;
14:
15: import cz.cvut.kbss.jsonld.common.CollectionType;
16: import cz.cvut.kbss.jsonld.serialization.model.*;
17:
18: import java.util.Collection;
19: import java.util.Date;
20: import java.util.List;
21: import java.util.Set;
22:
23: /**
24: * Factory for constructing {@link JsonNode} instances.
25: */
26: public class JsonNodeFactory {
27:
28: private JsonNodeFactory() {
29: throw new AssertionError();
30: }
31:
32: private enum LiteralType {
33: BOOLEAN, NUMBER, STRING, TEMPORAL
34: }
35:
36: public static LiteralNode createLiteralNode(Object value) {
37: return createLiteralNode(null, value);
38: }
39:
40: public static LiteralNode createLiteralNode(String name, Object value) {
41: final LiteralType type = determineLiteralType(value);
42: LiteralNode node = null;
43: switch (type) {
44: case BOOLEAN:
45: node = name != null ? new BooleanLiteralNode(name, (Boolean) value) : new BooleanLiteralNode(
46: (Boolean) value);
47: break;
48: case NUMBER:
49: node = name != null ? new NumericLiteralNode<>(name, (Number) value) :
50: new NumericLiteralNode<>((Number) value);
51: break;
52: case STRING:
53: node = name != null ? new StringLiteralNode(name, value.toString()) :
54: new StringLiteralNode(value.toString());
55: break;
56: case TEMPORAL:
57: node = TemporalNodeFactory.createLiteralNode(name, value);
58: break;
59: }
60: return node;
61: }
62:
63: private static LiteralType determineLiteralType(Object value) {
64: if (value instanceof Boolean) {
65: return LiteralType.BOOLEAN;
66: } else if (value instanceof Number) {
67: return LiteralType.NUMBER;
68: } else if (value instanceof Date) {
69: return LiteralType.TEMPORAL;
70: }
71: return LiteralType.STRING;
72: }
73:
74: /**
75: * Creates a node for representing a single translation of a string value.
76: *
77: * @param value String value
78: * @param language Language tag for the value
79: * @return A node representing the language tagged value
80: */
81: public static LangStringNode createLangStringNode(String value, String language) {
82: return new LangStringNode(value, language);
83: }
84:
85: /**
86: * Creates a node for representing a single translation of a string value.
87: * <p>
88: * Usually, multiple translations are expected which are put into a collection. But if there is only one translation
89: * in the multilingual string, this method may be used to directly serialize the attribute.
90: *
91: * @param name Attribute name
92: * @param value String value
93: * @param language Language tag for the value
94: * @return A node representing the language tagged value
95: */
96: public static LangStringNode createLangStringNode(String name, String value, String language) {
97: return new LangStringNode(name, value, language);
98: }
99:
100: /**
101: * Creates collection node for the specified collection.
102: * <p>
103: * The node is without name, so it cannot be used as attribute.
104: *
105: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
106: * result
107: * @return An empty collection node
108: */
109: public static CollectionNode createCollectionNode(Collection<?> value) {
110: return createCollectionNode(null, value);
111: }
112:
113: /**
114: * Creates collection node with the specified name, for the specified collection.
115: *
116: * @param name Name of the node (attribute)
117: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
118: * result
119: * @return An empty collection node
120: */
121: public static CollectionNode createCollectionNode(String name, Collection<?> value) {
122: final CollectionType type = determineCollectionType(value);
123: CollectionNode n = null;
124: switch (type) {
125: case LIST:
126: n = name != null ? new ListNode(name) : new ListNode();
127: break;
128: case SET:
129: n = name != null ? new SetNode(name) : new SetNode();
130: break;
131: }
132: return n;
133: }
134:
135: private static CollectionType determineCollectionType(Collection<?> collection) {
136: if (collection instanceof List) {
137: return CollectionType.LIST;
138: } else if (collection instanceof Set) {
139: return CollectionType.SET;
140: } else {
141: throw new IllegalArgumentException("Unsupported collection type " + collection.getClass());
142: }
143: }
144:
145: public static CollectionNode createCollectionNodeFromArray() {
146: return new SetNode();
147: }
148:
149: public static CollectionNode createCollectionNodeFromArray(String name) {
150: return new SetNode(name);
151: }
152:
153: public static ObjectNode createObjectNode() {
154: return new ObjectNode();
155: }
156:
157: public static ObjectNode createObjectNode(String name) {
158: return new ObjectNode(name);
159: }
160:
161: public static ObjectIdNode createObjectIdNode(Object id) {
162: return new ObjectIdNode(id.toString());
163: }
164:
165: public static ObjectIdNode createObjectIdNode(String name, Object id) {
166: return new ObjectIdNode(name, id.toString());
167: }
168: }