|
1 | 1 | /* |
2 | | - * Copyright 2014 hbz, Pascal Christoph |
| 2 | + * Copyright 2026 hbz, Deutsche Nationalbibliothek |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 the "License"; |
5 | 5 | * you may not use this file except in compliance with the License. |
|
29 | 29 |
|
30 | 30 | import java.text.Normalizer; |
31 | 31 |
|
| 32 | + |
32 | 33 | /** |
33 | | - * A pica xml handler. |
34 | | - * |
35 | | - * @author Pascal Christoph (dr0i) |
| 34 | + * A Pica XML reader. To read marc data without namespace specification set option `namespace=""` or to null when using JAVA code. |
| 35 | + * The handler reuses some code from M. Geipels MarcXmlHandler |
| 36 | + * @author Tobias Bülte |
| 37 | + * @author Markus Michael Geipel |
36 | 38 | * |
37 | 39 | */ |
38 | | -@Description("A pica xml reader") |
| 40 | +@Description("A Pica XML reader. To read pica data without namespace specification set option `namespace=\"\"`. To ignore namespace specification set option `ignorenamespace=\"true\". For PPXML see `handle-ppxml`") |
39 | 41 | @In(XmlReceiver.class) |
40 | 42 | @Out(StreamReceiver.class) |
41 | 43 | @FluxCommand("handle-picaxml") |
42 | 44 | public final class PicaXmlHandler extends DefaultXmlPipe<StreamReceiver> { |
43 | 45 |
|
44 | | - private static final String SUBFIELD = "subf"; |
45 | | - private static final String DATAFIELD = "tag"; |
| 46 | + public static final String NAMESPACE = "info:srw/schema/5/picaXML-v1.0"; |
| 47 | + |
| 48 | + private static final String SUBFIELD = "subfield"; |
| 49 | + private static final String DATAFIELD = "datafield"; |
46 | 50 | private static final String RECORD = "record"; |
47 | | - private static final String NAMESPACE = |
48 | | - "http://www.oclcpica.org/xmlns/ppxml-1.0"; |
49 | | - private static final String LEADER = "global"; |
| 51 | + |
| 52 | + private String attributeMarker = DEFAULT_ATTRIBUTE_MARKER; |
50 | 53 | private String currentTag = ""; |
| 54 | + private String namespace = NAMESPACE; |
51 | 55 | private StringBuilder builder = new StringBuilder(); |
| 56 | + private boolean ignoreNamespace; |
52 | 57 |
|
53 | 58 | /** |
54 | 59 | * Creates an instance of {@link PicaXmlHandler}. |
55 | 60 | */ |
56 | 61 | public PicaXmlHandler() { |
57 | 62 | } |
58 | 63 |
|
| 64 | + /** |
| 65 | + * Sets the namespace. |
| 66 | + * |
| 67 | + * <strong>Default value: {@value #NAMESPACE}</strong> |
| 68 | + * |
| 69 | + * @param namespace the namespace. Set to null if namespace shouldn't be checked. Set to empty string |
| 70 | + * if the namespace is missing in the data. |
| 71 | + */ |
| 72 | + public void setNamespace(final String namespace) { |
| 73 | + this.namespace = namespace; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets whether to ignore the namespace. |
| 78 | + * |
| 79 | + * <strong>Default value: false</strong> |
| 80 | + * |
| 81 | + * @param ignoreNamespace true if the namespace should be ignored |
| 82 | + */ |
| 83 | + public void setIgnoreNamespace(final boolean ignoreNamespace) { |
| 84 | + this.ignoreNamespace = ignoreNamespace; |
| 85 | + } |
| 86 | + |
| 87 | + private boolean checkNamespace(final String uri) { |
| 88 | + return namespace == null || ignoreNamespace || namespace.equals(uri); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the attribute marker. |
| 93 | + * |
| 94 | + * <strong>Default value: |
| 95 | + * {@value org.metafacture.framework.helpers.DefaultXmlPipe#DEFAULT_ATTRIBUTE_MARKER}</strong> |
| 96 | + * |
| 97 | + * @param attributeMarker the attribute marker |
| 98 | + */ |
| 99 | + public void setAttributeMarker(final String attributeMarker) { |
| 100 | + this.attributeMarker = attributeMarker; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the attribute marker. |
| 105 | + * |
| 106 | + * @return the attribute marker |
| 107 | + */ |
| 108 | + public String getAttributeMarker() { |
| 109 | + return attributeMarker; |
| 110 | + } |
| 111 | + |
59 | 112 | @Override |
60 | | - public void startElement(final String uri, final String localName, |
61 | | - final String qName, final Attributes attributes) throws SAXException { |
| 113 | + public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { |
62 | 114 | if (SUBFIELD.equals(localName)) { |
63 | 115 | builder = new StringBuilder(); |
64 | | - currentTag = attributes.getValue("id"); |
| 116 | + currentTag = attributes.getValue("code"); |
65 | 117 | } |
66 | 118 | else if (DATAFIELD.equals(localName)) { |
67 | | - getReceiver().startEntity( |
68 | | - attributes.getValue("id") + attributes.getValue("occ")); |
| 119 | + final String tag = attributes.getValue("tag"); |
| 120 | + final String occurence = attributes.getValue("occurrence"); |
| 121 | + if (occurence != null) { |
| 122 | + getReceiver().startEntity(tag + "/" + occurence); |
| 123 | + } |
| 124 | + else { |
| 125 | + getReceiver().startEntity(tag); |
| 126 | + } |
69 | 127 | } |
70 | | - else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { |
| 128 | + else if (RECORD.equals(localName) && checkNamespace(uri)) { |
71 | 129 | getReceiver().startRecord(""); |
72 | 130 | } |
73 | | - else if (LEADER.equals(localName)) { |
74 | | - builder = new StringBuilder(); |
75 | | - currentTag = LEADER; |
76 | | - } |
77 | 131 | } |
78 | 132 |
|
79 | 133 | @Override |
80 | | - public void endElement(final String uri, final String localName, |
81 | | - final String qName) throws SAXException { |
| 134 | + public void endElement(final String uri, final String localName, final String qName) throws SAXException { |
82 | 135 | if (SUBFIELD.equals(localName)) { |
83 | | - getReceiver().literal(currentTag, |
84 | | - Normalizer.normalize(builder.toString().trim(), Normalizer.Form.NFC)); |
| 136 | + getReceiver().literal(currentTag, Normalizer.normalize(builder.toString().trim(), Normalizer.Form.NFC)); |
85 | 137 | } |
86 | 138 | else if (DATAFIELD.equals(localName)) { |
87 | 139 | getReceiver().endEntity(); |
88 | 140 | } |
89 | | - else if (RECORD.equals(localName) && NAMESPACE.equals(uri)) { |
| 141 | + else if (RECORD.equals(localName) && checkNamespace(uri)) { |
90 | 142 | getReceiver().endRecord(); |
91 | 143 | } |
92 | 144 | } |
93 | 145 |
|
94 | 146 | @Override |
95 | | - public void characters(final char[] chars, final int start, final int length) |
96 | | - throws SAXException { |
| 147 | + public void characters(final char[] chars, final int start, final int length) throws SAXException { |
97 | 148 | builder.append(chars, start, length); |
98 | 149 | } |
99 | 150 |
|
|
0 commit comments