-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from com-pas/create-scd-with-header
Create scd with header and import ICD
- Loading branch information
Showing
137 changed files
with
10,168 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
sct-commons/src/main/java/org/lfenergy/compas/sct/commons/MarshallerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-FileCopyrightText: 2021 RTE FRANCE | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.lfenergy.compas.sct.commons; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.lfenergy.compas.core.commons.exception.CompasErrorCode; | ||
import org.lfenergy.compas.core.commons.exception.CompasException; | ||
|
||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.transform.Result; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.*; | ||
|
||
@Slf4j | ||
public class MarshallerWrapper { | ||
private final Unmarshaller unmarshaller; | ||
private final Marshaller marshaller; | ||
|
||
protected MarshallerWrapper(Unmarshaller unmarshaller, Marshaller marshaller) { | ||
this.unmarshaller = unmarshaller; | ||
this.marshaller = marshaller; | ||
} | ||
|
||
public static SclMarshallerBuilder builder(){ | ||
return new SclMarshallerBuilder(); | ||
} | ||
|
||
public <T> String marshall(final T obj) { | ||
try { | ||
StringWriter sw = new StringWriter(); | ||
Result result = new StreamResult(sw); | ||
marshaller.marshal(obj, result); | ||
|
||
return sw.toString(); | ||
} catch (JAXBException exp) { | ||
String message = String.format("Error marshalling the Class: %s", exp); | ||
log.error(message); | ||
throw new CompasException(CompasErrorCode.MARSHAL_ERROR_CODE, message); | ||
} | ||
} | ||
|
||
public <T> T unmarshall(final byte[] xml, Class<T> cls) { | ||
ByteArrayInputStream input = new ByteArrayInputStream(xml); | ||
return unmarshall(input, cls); | ||
} | ||
|
||
public <T> T unmarshall(final InputStream xml, Class<T> cls) { | ||
try { | ||
Object result = unmarshaller.unmarshal(new StreamSource(xml)); | ||
if (!result.getClass().isAssignableFrom(cls)) { | ||
throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, | ||
"Error unmarshalling to the Class. Invalid class"); | ||
} | ||
return cls.cast(result); | ||
} catch (JAXBException exp) { | ||
String message = String.format("Error unmarshalling to the Class: %s", exp.getLocalizedMessage()); | ||
log.error(message); | ||
throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, message); | ||
} | ||
} | ||
} |
Oops, something went wrong.