Package: ResourcePropertiesJaxbUnmarshallable
ResourcePropertiesJaxbUnmarshallable
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ResourcePropertiesJaxbUnmarshallable(ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
unmarshal(InputStream) |
|
|
|
|
|
||||||||||||||||||||
unmarshal(PropertiesJaxb) |
|
|
|
|
|
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.io.IOException;
32: import java.io.InputStream;
33: import jakarta.xml.bind.JAXBElement;
34: import jakarta.xml.bind.JAXBException;
35: import jakarta.xml.bind.Unmarshaller;
36: import org.springframework.beans.factory.annotation.Configurable;
37: import it.tidalwave.util.Id;
38: import it.tidalwave.util.Key;
39: import it.tidalwave.role.io.Unmarshallable;
40: import it.tidalwave.dci.annotation.DciRole;
41: import it.tidalwave.northernwind.core.model.ModelFactory;
42: import it.tidalwave.northernwind.core.model.ResourceProperties;
43: import it.tidalwave.northernwind.core.impl.io.jaxb.PropertiesJaxb;
44: import lombok.RequiredArgsConstructor;
45: import lombok.extern.slf4j.Slf4j;
46:
47: /***********************************************************************************************************************
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52:•@DciRole(datumType = ResourceProperties.class) @Configurable @RequiredArgsConstructor @Slf4j
53: public class ResourcePropertiesJaxbUnmarshallable implements Unmarshallable
54: {
55: @Nonnull
56: private final ResourceProperties owner;
57:
58: @Inject
59: private ModelFactory modelFactory;
60:
61: @Inject
62: private Unmarshaller unmarshaller;
63:
64: /*******************************************************************************************************************
65: *
66: * {@inheritDoc}
67: *
68: ******************************************************************************************************************/
69: @Override @Nonnull @SuppressWarnings("unchecked")
70: public ResourceProperties unmarshal (@Nonnull final InputStream is)
71: throws IOException
72: {
73: try
74: {
75: final var propertiesJaxb = ((JAXBElement<PropertiesJaxb>)unmarshaller.unmarshal(is)).getValue();
76:
77:• if (!"1.0".equals(propertiesJaxb.getVersion()))
78: {
79: throw new IOException("Unexpected version: " + propertiesJaxb.getVersion());
80: }
81:
82: return unmarshal(propertiesJaxb);
83: }
84: catch (JAXBException e)
85: {
86: throw new IOException("", e);
87: }
88: }
89:
90: /*******************************************************************************************************************
91: *
92: *
93: ******************************************************************************************************************/
94: @Nonnull
95: private ResourceProperties unmarshal (@Nonnull final PropertiesJaxb propertiesJaxb)
96: {
97:• final var id = new Id((propertiesJaxb.getId() != null) ? propertiesJaxb.getId() : "");
98: var properties = modelFactory.createProperties().withId(id).build();
99:
100:• for (final var propertyJaxb : propertiesJaxb.getProperty())
101: {
102: final var values = propertyJaxb.getValues();
103: properties = properties.withProperty(Key.of(propertyJaxb.getName()),
104:• (values != null) ? values.getValue() : propertyJaxb.getValue());
105: }
106:
107:• for (final var propertiesJaxb2 : propertiesJaxb.getProperties())
108: {
109: properties = properties.withProperties(unmarshal(propertiesJaxb2));
110: }
111:
112: return properties;
113: }
114: }