-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e33f7c
commit f8109ce
Showing
4 changed files
with
139 additions
and
75 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
46 changes: 46 additions & 0 deletions
46
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/DatastreamContentWrapper.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,46 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream; | ||
import org.w3c.dom.Document; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class DatastreamContentWrapper { | ||
private final RepositoryDatastream content; | ||
private final SupportedFormats supportedFormat; | ||
|
||
public DatastreamContentWrapper(RepositoryDatastream content, SupportedFormats supportedFormat) { | ||
this.content = content; | ||
this.supportedFormat = supportedFormat; | ||
} | ||
|
||
public String asString() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsString()) { | ||
throw new UnsupportedContentFormatException("String format not supported."); | ||
} | ||
return new String(content, StandardCharsets.UTF_8); | ||
} | ||
|
||
public InputStream asStream() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsStream()) { | ||
throw new UnsupportedContentFormatException("InputStream format not supported."); | ||
} | ||
return new ByteArrayInputStream(content); | ||
} | ||
|
||
public Document asXml() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsXml()) { | ||
throw new UnsupportedContentFormatException("XML format not supported."); | ||
} | ||
try { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content)); | ||
} catch (Exception e) { | ||
throw new IOException("Failed to parse XML", e); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/RepositoryObjectWrapper.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,47 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import com.qbizm.kramerius.imp.jaxb.DigitalObject; | ||
import cz.incad.kramerius.fedora.om.repository.RepositoryObject; | ||
import org.w3c.dom.Document; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class RepositoryObjectWrapper { | ||
private final RepositoryObject content; | ||
private final SupportedFormats supportedFormat; | ||
|
||
public RepositoryObjectWrapper(RepositoryObject content, SupportedFormats supportedFormat) { | ||
this.content = content; | ||
this.supportedFormat = supportedFormat; | ||
} | ||
|
||
public String asString() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsString()) { | ||
throw new UnsupportedContentFormatException("String format not supported."); | ||
} | ||
return new String(content, StandardCharsets.UTF_8); | ||
} | ||
|
||
public InputStream asStream() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsStream()) { | ||
throw new UnsupportedContentFormatException("InputStream format not supported."); | ||
} | ||
return new ByteArrayInputStream(content); | ||
} | ||
|
||
public Document asXml() throws UnsupportedContentFormatException { | ||
if (!supportedFormat.supportsXml()) { | ||
throw new UnsupportedContentFormatException("XML format not supported."); | ||
} | ||
try { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content)); | ||
} catch (Exception e) { | ||
throw new IOException("Failed to parse XML", e); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...merius/fedora/impl/tmp/ContentFormat.java → ...ius/fedora/impl/tmp/SupportedFormats.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