Skip to content

Commit 5764a1a

Browse files
author
eberhardtj
committed
Add contrib sub-package
Moves everything under org.metafacture into org.metafacture.contrib
1 parent 85b2b97 commit 5764a1a

File tree

11 files changed

+680
-0
lines changed

11 files changed

+680
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.framework;
17+
18+
import org.metafacture.framework.Receiver;
19+
import org.metafacture.framework.Sender;
20+
21+
public interface XdmPipe<R extends Receiver> extends XdmReceiver, Sender<R> {
22+
// Just a combination of sender and receiver
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.framework;
17+
18+
import net.sf.saxon.s9api.XdmNode;
19+
import org.metafacture.framework.ObjectReceiver;
20+
21+
public interface XdmReceiver extends ObjectReceiver<XdmNode> {
22+
// Just a combination of LifeCycle and the corresponding ObjectReceiver
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.framework.helpers;
17+
18+
import net.sf.saxon.s9api.XdmNode;
19+
import org.metafacture.contrib.framework.XdmPipe;
20+
import org.metafacture.framework.Receiver;
21+
import org.metafacture.framework.helpers.DefaultSender;
22+
23+
public class DefaultXdmPipe<R extends Receiver> extends DefaultSender<R> implements XdmPipe<R> {
24+
25+
public void process(final XdmNode obj) {
26+
// Default implementation does nothing
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.framework.helpers;
17+
18+
import net.sf.saxon.s9api.XdmNode;
19+
import org.metafacture.framework.ObjectReceiver;
20+
import org.metafacture.framework.helpers.DefaultLifeCycle;
21+
22+
public class DefaultXdmReceiver extends DefaultLifeCycle implements ObjectReceiver<XdmNode> {
23+
24+
@Override
25+
public void process(final XdmNode obj) {
26+
// Default implementation does nothing
27+
}
28+
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.framework.helpers;
17+
18+
import net.sf.saxon.s9api.XdmNode;
19+
import org.metafacture.contrib.framework.XdmReceiver;
20+
21+
public class ForwardingXdmPipe extends DefaultXdmPipe<XdmReceiver> {
22+
23+
@Override
24+
public void process(XdmNode node) {
25+
getReceiver().process(node);
26+
}
27+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.xdm;
17+
18+
import net.sf.saxon.s9api.*;
19+
import org.metafacture.contrib.framework.XdmReceiver;
20+
import org.metafacture.framework.FluxCommand;
21+
import org.metafacture.framework.MetafactureException;
22+
import org.metafacture.framework.XmlReceiver;
23+
import org.metafacture.framework.annotations.Description;
24+
import org.metafacture.framework.annotations.In;
25+
import org.metafacture.framework.annotations.Out;
26+
import org.metafacture.framework.helpers.DefaultXmlPipe;
27+
import org.xml.sax.Attributes;
28+
import org.xml.sax.Locator;
29+
import org.xml.sax.SAXException;
30+
31+
@In(XmlReceiver.class)
32+
@Out(XdmReceiver.class)
33+
@Description("Transforms sax documents into xdm nodes.")
34+
@FluxCommand("sax-to-xdm")
35+
public class SaxToXdm extends DefaultXmlPipe<XdmReceiver> {
36+
private Processor processor;
37+
private DocumentBuilder documentBuilder;
38+
private BuildingContentHandler buildingContentHandler;
39+
40+
public SaxToXdm() {
41+
this.processor = new Processor(false);
42+
this.documentBuilder = processor.newDocumentBuilder();
43+
this.buildingContentHandler = newBuildingContentHandler();
44+
}
45+
46+
private BuildingContentHandler newBuildingContentHandler() {
47+
try {
48+
return documentBuilder.newBuildingContentHandler();
49+
} catch (SaxonApiException e) {
50+
throw new MetafactureException(e);
51+
}
52+
}
53+
54+
@Override
55+
public void startDocument() {
56+
buildingContentHandler = newBuildingContentHandler();
57+
58+
try {
59+
buildingContentHandler.startDocument();
60+
} catch (SAXException e) {
61+
throw new MetafactureException(e);
62+
}
63+
}
64+
65+
@Override
66+
public void endDocument() {
67+
try {
68+
buildingContentHandler.endDocument();
69+
XdmNode node = buildingContentHandler.getDocumentNode();
70+
getReceiver().process(node);
71+
} catch (SAXException | SaxonApiException e) {
72+
throw new MetafactureException(e);
73+
}
74+
}
75+
76+
@Override
77+
public void startElement(final String uri, final String localName,
78+
final String qName, final Attributes attributes) {
79+
try {
80+
buildingContentHandler.startElement(uri, localName, qName, attributes);
81+
} catch (SAXException e) {
82+
throw new MetafactureException(e);
83+
}
84+
}
85+
86+
@Override
87+
public void endElement(String uri, String localName, String qName) {
88+
try {
89+
buildingContentHandler.endElement(uri, localName, qName);
90+
} catch (SAXException e) {
91+
throw new MetafactureException(e);
92+
}
93+
}
94+
95+
@Override
96+
public void characters(char[] ch, int start, int length) {
97+
try {
98+
buildingContentHandler.characters(ch, start, length);
99+
} catch (SAXException e) {
100+
throw new MetafactureException(e);
101+
}
102+
}
103+
104+
@Override
105+
public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException {
106+
buildingContentHandler.ignorableWhitespace(ch, start, length);
107+
}
108+
109+
@Override
110+
public void processingInstruction(final String target, final String data) throws SAXException {
111+
buildingContentHandler.processingInstruction(target, data);
112+
}
113+
114+
@Override
115+
public void skippedEntity(String name) throws SAXException {
116+
buildingContentHandler.skippedEntity(name);
117+
}
118+
119+
@Override
120+
public void setDocumentLocator(final Locator locator) {
121+
buildingContentHandler.setDocumentLocator(locator);
122+
}
123+
124+
@Override
125+
public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
126+
buildingContentHandler.startPrefixMapping(prefix, uri);
127+
}
128+
129+
@Override
130+
public void endPrefixMapping(final String prefix) throws SAXException {
131+
buildingContentHandler.endPrefixMapping(prefix);
132+
}
133+
134+
135+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2018 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.metafacture.contrib.xdm;
17+
18+
import net.sf.saxon.s9api.Processor;
19+
import net.sf.saxon.s9api.SAXDestination;
20+
import net.sf.saxon.s9api.SaxonApiException;
21+
import net.sf.saxon.s9api.XdmNode;
22+
import org.metafacture.contrib.framework.XdmReceiver;
23+
import org.metafacture.contrib.framework.helpers.DefaultXdmPipe;
24+
import org.metafacture.contrib.xdm.helper.XmlFilterAdapter;
25+
import org.metafacture.framework.FluxCommand;
26+
import org.metafacture.framework.MetafactureException;
27+
import org.metafacture.framework.XmlReceiver;
28+
import org.metafacture.framework.annotations.Description;
29+
import org.metafacture.framework.annotations.In;
30+
import org.metafacture.framework.annotations.Out;
31+
32+
@In(XdmReceiver.class)
33+
@Out(XmlReceiver.class)
34+
@Description("Transforms xdm nodes into sax documents.")
35+
@FluxCommand("xdm-to-sax")
36+
public class XdmToSax extends DefaultXdmPipe<XmlReceiver> {
37+
private Processor processor;
38+
private SAXDestination destination;
39+
40+
public XdmToSax() {
41+
this.processor = new Processor(false);
42+
}
43+
44+
@Override
45+
public void process(XdmNode node) {
46+
try {
47+
processor.writeXdmValue(node, destination);
48+
} catch (SaxonApiException e) {
49+
throw new MetafactureException(e);
50+
}
51+
}
52+
53+
@Override
54+
public void onSetReceiver() {
55+
this.destination = new SAXDestination(new XmlFilterAdapter(getReceiver()));
56+
}
57+
}

0 commit comments

Comments
 (0)