Package: ResourcePropertiesJaxbMarshallable
ResourcePropertiesJaxbMarshallable
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ResourcePropertiesJaxbMarshallable(ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
lambda$marshal$0(PropertyJaxb, PropertiesJaxb, Object) |
|
|
|
|
|
||||||||||||||||||||
marshal(OutputStream) |
|
|
|
|
|
||||||||||||||||||||
marshal(ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.core.impl.io;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.util.Collection;
32: import java.util.TreeSet;
33: import java.io.IOException;
34: import java.io.OutputStream;
35: import jakarta.xml.bind.JAXBException;
36: import jakarta.xml.bind.Marshaller;
37: import org.springframework.beans.factory.annotation.Configurable;
38: import it.tidalwave.role.io.Marshallable;
39: import it.tidalwave.dci.annotation.DciRole;
40: import it.tidalwave.northernwind.core.model.ResourceProperties;
41: import it.tidalwave.northernwind.core.impl.io.jaxb.ObjectFactory;
42: import it.tidalwave.northernwind.core.impl.io.jaxb.PropertiesJaxb;
43: import lombok.extern.slf4j.Slf4j;
44:
45: /***********************************************************************************************************************
46: *
47: * @author Fabrizio Giudici
48: *
49: **********************************************************************************************************************/
50: @DciRole(datumType = ResourceProperties.class) @Configurable @Slf4j
51: public class ResourcePropertiesJaxbMarshallable implements Marshallable
52: {
53: @Nonnull
54: private final ResourceProperties resourceProperties;
55:
56: @Inject
57: private ObjectFactory objectFactory;
58:
59: @Inject
60: private Marshaller marshaller;
61:
62: /*******************************************************************************************************************
63: *
64: *
65: ******************************************************************************************************************/
66: public ResourcePropertiesJaxbMarshallable (@Nonnull final ResourceProperties resourceProperties)
67:• {
68: this.resourceProperties = resourceProperties;
69:• }
70:
71: /*******************************************************************************************************************
72: *
73: * {@inheritDoc}
74: *
75: ******************************************************************************************************************/
76: @Override
77: public void marshal (@Nonnull final OutputStream os)
78: throws IOException
79: {
80: try
81: {
82: marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // FIXME: set in Spring
83: marshaller.marshal(objectFactory.createProperties(marshal(resourceProperties)), os);
84: }
85: catch (JAXBException e)
86: {
87: throw new IOException("", e);
88: }
89: }
90:
91: /*******************************************************************************************************************
92: *
93: *
94: ******************************************************************************************************************/
95: @Nonnull
96: private PropertiesJaxb marshal (@Nonnull final ResourceProperties properties)
97: {
98: final var propertiesJaxb = objectFactory.createPropertiesJaxb();
99: final var id = properties.getId();
100:
101:• if ("".equals(id.stringValue()))
102: {
103: propertiesJaxb.setVersion("1.0");
104: }
105: else
106: {
107: propertiesJaxb.setId(id.stringValue());
108: }
109:
110:• for (final var key : new TreeSet<>(properties.getKeys()))
111: {
112: final var propertyJaxb = objectFactory.createPropertyJaxb();
113: propertyJaxb.setName(key.stringValue());
114: properties.getProperty(key).ifPresent(value ->
115: {
116:• if (value instanceof Collection)
117: {
118: final var valuesJaxb = objectFactory.createValuesJaxb();
119: propertyJaxb.setValues(valuesJaxb);
120:
121:• for (final Object valueItem : (Collection<?>)value)
122: {
123: valuesJaxb.getValue().add(valueItem.toString());
124: }
125: }
126: else
127: {
128: propertyJaxb.setValue(value.toString());
129: }
130:
131: propertiesJaxb.getProperty().add(propertyJaxb);
132: });
133: }
134:
135:• for (final var groupId : new TreeSet<>(properties.getGroupIds()))
136: {
137: propertiesJaxb.getProperties().add(marshal(properties.getGroup(groupId)));
138: }
139:
140: return propertiesJaxb;
141: }
142: }