Skip to content

Commit 3445e5e

Browse files
lili
authored andcommitted
The source code is optimized.
The changes make the code more readable. Some unnecessary checks are deleted.
1 parent ff1b28e commit 3445e5e

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

src/main/java/org/culturegraph/mf/stream/converter/bib/PicaEncoder.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public final class PicaEncoder extends DefaultStreamPipe<ObjectReceiver<String>>
5656
private static final String FIELD_DELIMITER = "\u001e";
5757
private static final String SUB_DELIMITER = "\u001f";
5858
private boolean idnControlSubField;
59-
private boolean recordOpen;
6059
private boolean entityOpen;
6160
private StringBuilder builder = new StringBuilder();
6261
private String id="";
@@ -65,25 +64,14 @@ public final class PicaEncoder extends DefaultStreamPipe<ObjectReceiver<String>>
6564
private static final Pattern FIELD_NAME_PATTERN = Pattern.compile(FIELD_NAME_PATTERN_STRING);
6665
private boolean ignoreRecordId;
6766

68-
/**
69-
* For each field in the stream the method calls:
70-
* <ol>
71-
* <li>receiver.startEntity</li>
72-
* <li>receiver.literal for each subfield of the field</li>
73-
* <li>receiver.endEntity</li>
74-
* </ol>
75-
* Fields without any subfield will be skipped.<br>
76-
*
77-
* @param record
78-
*/
67+
7968
@Override
8069
public void startRecord(final String recordId) {
8170
// the name is a idn, which should be found in the encoded data under 003@.
8271
//any rest of the previous record is cleared before the new begins.
8372
builder.setLength(0);
8473
this.id = recordId;
8574
//Now an entity can be opened. But no literal is allowed.
86-
this.recordOpen = true;
8775
this.entityOpen = false;
8876
}
8977

@@ -95,50 +83,37 @@ public boolean getIgnoreRecordId() {
9583
return this.ignoreRecordId;
9684
}
9785

98-
protected void compareIdFromRecord(final String recordId) {
99-
if (this.id.equals(recordId)) {
100-
idnControlSubField = false; //only test this context.
101-
return;
102-
}
103-
throw new MissingIdException(recordId);
104-
}
105-
106-
10786
@Override
10887
public void startEntity(final String name) {
10988
// Here begins a field (i.e. "028A ", which is given in the name.
11089
// It is unknown, whether there are any subfields in the field.
11190
final Matcher fieldNameMatcher = FIELD_NAME_PATTERN.matcher(name);
112-
if (fieldNameMatcher.find()) {
113-
builder.append(name.trim()+ " ");
114-
}
115-
else {
91+
if (!fieldNameMatcher.matches()) {
11692
throw new FormatException(name);
11793
}
118-
if (name.trim().equals("003@") && !getIgnoreRecordId()) {
119-
//Time to check record Id in the following subfield.
120-
idnControlSubField = true;
121-
}else {
122-
//No check is necessary.
123-
idnControlSubField = false;
124-
}
125-
//Now literals can be opened. But no entities are allowed.
126-
if (recordOpen)
127-
this.entityOpen = true;
94+
builder.append(name.trim()+ " ");
95+
96+
idnControlSubField = !ignoreRecordId && name.trim().equals("003@");
97+
//Now literals can be opened.
98+
this.entityOpen = true;
12899
}
129100

130101
@Override
131102
public void literal(final String name, final String value) {
132103
//A Subfield has one character or digit exactly.
133104
if (name.length()!=1){
134105
throw new FormatException(name);
135-
} else if (!entityOpen){
136-
throw new FormatException(name); //new exceptions define!!!! tODo
106+
}
107+
if (!entityOpen){
108+
throw new FormatException(name); //new exceptions definition for literal out of entity
137109
}
138110
final String valueNew = Normalizer.normalize(value, Form.NFD);
139111
if (idnControlSubField){
140112
// it is a 003@ field, the same record id delivered with record should follow
141-
compareIdFromRecord(value);
113+
if (!this.id.equals(value)) {
114+
throw new MissingIdException(value);
115+
}
116+
idnControlSubField = false; //only one record Id will be checked.
142117
}
143118
builder.append(SUB_DELIMITER);
144119
builder.append(name);
@@ -155,9 +130,7 @@ public void endEntity() {
155130
@Override
156131
public void endRecord() {
157132
getReceiver().process(builder.toString());
158-
builder.setLength(0);
159-
//Now a record can be opened. But no literal and entity are allowed.
160-
this.recordOpen = false;
133+
//No literal is allowed.
161134
this.entityOpen = false;
162135
}
163136
@Override

src/test/java/org/culturegraph/mf/stream/converter/bib/PicaEncoderTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,23 @@ public void testShouldWriteFieldAndSubfield() {
6565

6666
verify(receiver).process("003@ \u001f017709958X\u001e028@ \u001fPAbla\u0308o\u0308u\u0308bolo\u001fnVIX\u001flBapst\u001e");
6767
}
68-
68+
69+
@Test
70+
public void testShouldWriteFieldAndSubfield2() {
71+
picaEncoder.startRecord("17709958X");
72+
picaEncoder.startEntity("003@");
73+
picaEncoder.literal("0", "17709958X");
74+
picaEncoder.endEntity();
75+
picaEncoder.startEntity("028@/30"); // pattern allowed
76+
picaEncoder.literal("P", "Abläöübolo");
77+
picaEncoder.literal("n", "VIX");
78+
picaEncoder.literal("l", "Bapst");
79+
picaEncoder.endEntity();
80+
picaEncoder.endRecord();
81+
82+
verify(receiver).process("003@ \u001f017709958X\u001e028@/30 \u001fPAbla\u0308o\u0308u\u0308bolo\u001fnVIX\u001flBapst\u001e");
83+
}
84+
6985
@Test(expected=FormatException.class)
7086
public void testShouldFailOnIlligalFieldName() {
7187
picaEncoder.startRecord("17709958X");
@@ -82,6 +98,22 @@ public void testShouldFailOnIlligalFieldName() {
8298
verify(receiver).process("003@ \u001f017709958X\u001e@028 \u001fPAbla\u0308o\u0308u\u0308bolo\u001fnVIX\u001flBapst\u001e");
8399
}
84100

101+
@Test(expected=FormatException.class)
102+
public void testShouldFailOnIlligalFieldName2() {
103+
picaEncoder.startRecord("17709958X");
104+
picaEncoder.startEntity("003@");
105+
picaEncoder.literal("0", "17709958X");
106+
picaEncoder.endEntity();
107+
picaEncoder.startEntity("028@/301"); //the fieldname pattern not match!
108+
picaEncoder.literal("P", "Abläöübolo");
109+
picaEncoder.literal("n", "VIX");
110+
picaEncoder.literal("l", "Bapst");
111+
picaEncoder.endEntity();
112+
picaEncoder.endRecord();
113+
114+
verify(receiver).process("003@ \u001f017709958X\u001e@028 \u001fPAbla\u0308o\u0308u\u0308bolo\u001fnVIX\u001flBapst\u001e");
115+
}
116+
85117
@Test(expected=FormatException.class)
86118
public void testShouldFailOnIlligalSubfieldName() {
87119
picaEncoder.startRecord("17709958X");

0 commit comments

Comments
 (0)