-
Notifications
You must be signed in to change notification settings - Fork 33
Add true picaxml handler #530 #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
0d07192
beaab85
563f8b0
54ada12
a9c4816
c726cf9
daa9e5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright 2014 hbz, Pascal Christoph | ||
| * Copyright 2013, 2014 Deutsche Nationalbibliothek | ||
| * | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not hbz? Didin't you/we at hbz implement it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjusted as suggested
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you added M. Geipel as author. As he worked at the dnb we should than not delete dnb here, right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure. This is a reuse of the existing |
||
| * Licensed under the Apache License, Version 2.0 the "License"; | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -29,71 +29,121 @@ | |
|
|
||
| import java.text.Normalizer; | ||
|
|
||
|
|
||
| /** | ||
| * A pica xml handler. | ||
| * | ||
| * @author Pascal Christoph (dr0i) | ||
| * A Pica xml reader. To read marc data without namespace specification set option `namespace=""` or to null when using JAVA code. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better use capital letters "XML" like below.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be done. |
||
| * @author Tobias Bülte | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think ,as not little of the code was just a reuse of the former code, you just add you name as author , not deleting the first one.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are still the creator of |
||
| * | ||
| */ | ||
| @Description("A pica xml reader") | ||
| @Description("A Pica XML reader. To read pica data without namespace specification set option `namespace=\"\"`. To ignore namespace specification set option `ignorenamespace=\"true\".") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe here we should link to the new PpXmlHandler and hint when to use it? |
||
| @In(XmlReceiver.class) | ||
| @Out(StreamReceiver.class) | ||
| @FluxCommand("handle-picaxml") | ||
| public final class PicaXmlHandler extends DefaultXmlPipe<StreamReceiver> { | ||
|
|
||
| private static final String SUBFIELD = "subf"; | ||
| private static final String DATAFIELD = "tag"; | ||
| public static final String NAMESPACE = "info:srw/schema/5/picaXML-v1.0"; | ||
|
|
||
| private static final String SUBFIELD = "subfield"; | ||
| private static final String DATAFIELD = "datafield"; | ||
| private static final String CONTROLFIELD = "controlfield"; | ||
| private static final String RECORD = "record"; | ||
| private static final String NAMESPACE = | ||
| "http://www.oclcpica.org/xmlns/ppxml-1.0"; | ||
| private static final String LEADER = "global"; | ||
|
|
||
| private String attributeMarker = DEFAULT_ATTRIBUTE_MARKER; | ||
| private String currentTag = ""; | ||
| private String namespace = NAMESPACE; | ||
| private StringBuilder builder = new StringBuilder(); | ||
| private boolean ignoreNamespace; | ||
|
|
||
| /** | ||
| * Creates an instance of {@link PicaXmlHandler}. | ||
| */ | ||
| public PicaXmlHandler() { | ||
| } | ||
|
|
||
| /** | ||
| * Sets the namespace. | ||
| * | ||
| * <strong>Default value: {@value #NAMESPACE}</strong> | ||
| * | ||
| * @param namespace the namespace. Set to null if namespace shouldn't be checked. Set to empty string | ||
| * if the namespace is missing in the data. | ||
| */ | ||
| public void setNamespace(final String namespace) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is only one namespace valid (see below
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a reuse of the mechanism from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reused the code and the documentation |
||
| this.namespace = namespace; | ||
| } | ||
|
|
||
| /** | ||
| * Sets whether to ignore the namespace. | ||
| * | ||
| * <strong>Default value: false</strong> | ||
| * | ||
| * @param ignoreNamespace true if the namespace should be ignored | ||
| */ | ||
| public void setIgnoreNamespace(final boolean ignoreNamespace) { | ||
| this.ignoreNamespace = ignoreNamespace; | ||
| } | ||
|
|
||
| private boolean checkNamespace(final String uri) { | ||
| return namespace == null || ignoreNamespace || namespace.equals(uri); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the attribute marker. | ||
| * | ||
| * <strong>Default value: | ||
| * {@value org.metafacture.framework.helpers.DefaultXmlPipe#DEFAULT_ATTRIBUTE_MARKER}</strong> | ||
| * | ||
| * @param attributeMarker the attribute marker | ||
| */ | ||
| public void setAttributeMarker(final String attributeMarker) { | ||
| this.attributeMarker = attributeMarker; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the attribute marker. | ||
| * | ||
| * @return the attribute marker | ||
| */ | ||
| public String getAttributeMarker() { | ||
| return attributeMarker; | ||
| } | ||
|
|
||
| @Override | ||
| public void startElement(final String uri, final String localName, | ||
| final String qName, final Attributes attributes) throws SAXException { | ||
| public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { | ||
| if (SUBFIELD.equals(localName)) { | ||
| builder = new StringBuilder(); | ||
| currentTag = attributes.getValue("id"); | ||
| currentTag = attributes.getValue("code"); | ||
| } | ||
| else if (DATAFIELD.equals(localName)) { | ||
| getReceiver().startEntity( | ||
| attributes.getValue("id") + attributes.getValue("occ")); | ||
| } | ||
| else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { | ||
| getReceiver().startRecord(""); | ||
| getReceiver().startEntity(attributes.getValue("tag")); | ||
| } | ||
| else if (LEADER.equals(localName)) { | ||
| else if (CONTROLFIELD.equals(localName)) { | ||
| builder = new StringBuilder(); | ||
| currentTag = LEADER; | ||
| currentTag = attributes.getValue("tag"); | ||
| } | ||
| else if (RECORD.equals(localName) && checkNamespace(uri)) { | ||
| getReceiver().startRecord(""); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not tested - but what if the the namespace ist set to be ignored? It won't work then, won't start the record, no?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the tests. |
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void endElement(final String uri, final String localName, | ||
| final String qName) throws SAXException { | ||
| public void endElement(final String uri, final String localName, final String qName) throws SAXException { | ||
| if (SUBFIELD.equals(localName)) { | ||
| getReceiver().literal(currentTag, | ||
| Normalizer.normalize(builder.toString().trim(), Normalizer.Form.NFC)); | ||
| getReceiver().literal(currentTag, Normalizer.normalize(builder.toString().trim(), Normalizer.Form.NFC)); | ||
| } | ||
| else if (DATAFIELD.equals(localName)) { | ||
| getReceiver().endEntity(); | ||
| } | ||
| else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { | ||
| else if (CONTROLFIELD.equals(localName)) { | ||
| getReceiver().literal(currentTag, builder.toString()); | ||
| } | ||
| else if (RECORD.equals(localName) && checkNamespace(uri)) { | ||
| getReceiver().endRecord(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void characters(final char[] chars, final int start, final int length) | ||
| throws SAXException { | ||
| public void characters(final char[] chars, final int start, final int length) throws SAXException { | ||
| builder.append(chars, start, length); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright 2014 hbz, Pascal Christoph | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 the "License"; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.metafacture.biblio.pica; | ||
|
|
||
| import org.metafacture.framework.FluxCommand; | ||
| import org.metafacture.framework.StreamReceiver; | ||
| import org.metafacture.framework.XmlReceiver; | ||
| import org.metafacture.framework.annotations.Description; | ||
| import org.metafacture.framework.annotations.In; | ||
| import org.metafacture.framework.annotations.Out; | ||
| import org.metafacture.framework.helpers.DefaultXmlPipe; | ||
|
|
||
| import org.xml.sax.Attributes; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import java.text.Normalizer; | ||
|
|
||
| /** | ||
| * A pica ppxml handler. | ||
| * | ||
| * @author Pascal Christoph (dr0i) | ||
| * | ||
| */ | ||
| @Description("A pica ppxml reader") | ||
| @In(XmlReceiver.class) | ||
| @Out(StreamReceiver.class) | ||
| @FluxCommand("handle-ppxml") | ||
| public final class PpXmlHandler extends DefaultXmlPipe<StreamReceiver> { | ||
|
|
||
| private static final String SUBFIELD = "subf"; | ||
| private static final String DATAFIELD = "tag"; | ||
| private static final String RECORD = "record"; | ||
| private static final String NAMESPACE = | ||
| "http://www.oclcpica.org/xmlns/ppxml-1.0"; | ||
| private static final String LEADER = "global"; | ||
| private String currentTag = ""; | ||
| private StringBuilder builder = new StringBuilder(); | ||
|
|
||
| /** | ||
| * Creates an instance of {@link PpXmlHandler}. | ||
| */ | ||
| public PpXmlHandler() { | ||
| } | ||
|
|
||
| @Override | ||
| public void startElement(final String uri, final String localName, | ||
| final String qName, final Attributes attributes) throws SAXException { | ||
| if (SUBFIELD.equals(localName)) { | ||
| builder = new StringBuilder(); | ||
| currentTag = attributes.getValue("id"); | ||
| } | ||
| else if (DATAFIELD.equals(localName)) { | ||
| getReceiver().startEntity( | ||
| attributes.getValue("id") + attributes.getValue("occ")); | ||
| } | ||
| else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { | ||
| getReceiver().startRecord(""); | ||
| } | ||
| else if (LEADER.equals(localName)) { | ||
| builder = new StringBuilder(); | ||
| currentTag = LEADER; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void endElement(final String uri, final String localName, | ||
| final String qName) throws SAXException { | ||
| if (SUBFIELD.equals(localName)) { | ||
| getReceiver().literal(currentTag, | ||
| Normalizer.normalize(builder.toString().trim(), Normalizer.Form.NFC)); | ||
| } | ||
| else if (DATAFIELD.equals(localName)) { | ||
| getReceiver().endEntity(); | ||
| } | ||
| else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { | ||
| getReceiver().endRecord(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void characters(final char[] chars, final int start, final int length) | ||
| throws SAXException { | ||
| builder.append(chars, start, length); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.