@@ -144,10 +144,10 @@ public void startEntity(final String name) {
144144 currentEntity = name ;
145145 if (!name .equals (Marc21EventNames .LEADER_ENTITY )) {
146146 if (name .length () != 5 ) {
147- String message = String .format ("Entity too short." + "Got a string ('%s') of length %d."
148- + "Expected a length of 5 (field + indicators)." , name , name .length ());
149- throw new MetafactureException (message );
150- }
147+ String message = String .format ("Entity too short." + "Got a string ('%s') of length %d."
148+ + "Expected a length of 5 (field + indicators)." , name , name .length ());
149+ throw new MetafactureException (message );
150+ }
151151
152152 String tag = name .substring (0 , 3 );
153153 String ind1 = name .substring (3 , 4 );
@@ -156,7 +156,10 @@ public void startEntity(final String name) {
156156 writeRaw (String .format (DATAFIELD_OPEN_TEMPLATE , tag , ind1 , ind2 ));
157157 prettyPrintNewLine ();
158158 incrementIndentationLevel ();
159- }
159+ } else { prettyPrintIndentation ();
160+ writeRaw (LEADER_OPEN_TEMPLATE );
161+ }
162+
160163 }
161164
162165 @ Override
@@ -172,22 +175,23 @@ public void endEntity() {
172175
173176 @ Override
174177 public void literal (final String name , final String value ) {
178+ if (value == null || value .isEmpty ())
179+ return ;
175180 if (currentEntity .equals ("" )) {
176181 prettyPrintIndentation ();
177- writeRaw (String .format (CONTROLFIELD_OPEN_TEMPLATE , name ));
182+ writeRaw (String .format (CONTROLFIELD_OPEN_TEMPLATE , name . replaceFirst ( " \\ W" , "" ) ));
178183 writeEscaped (value .trim ());
179184 writeRaw (CONTROLFIELD_CLOSE );
180185 prettyPrintNewLine ();
181186 } else if (!currentEntity .equals (Marc21EventNames .LEADER_ENTITY )) {
182187 prettyPrintIndentation ();
183- writeRaw (String .format (SUBFIELD_OPEN_TEMPLATE , name ));
188+ writeRaw (String .format (SUBFIELD_OPEN_TEMPLATE , name . charAt ( name . indexOf ( '.' )+ 1 ) ));
184189 writeEscaped (value .trim ());
185190 writeRaw (SUBFIELD_CLOSE );
186191 prettyPrintNewLine ();
187192 } else {
188- if (name .equals (Marc21EventNames .LEADER_ENTITY )) {
189- prettyPrintIndentation ();
190- writeRaw (LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE );
193+ {
194+ writeRaw (value + LEADER_CLOSE_TEMPLATE );
191195 prettyPrintNewLine ();
192196 }
193197 }
@@ -241,8 +245,8 @@ private void writeEscaped(final String str) {
241245
242246 private void prettyPrintIndentation () {
243247 if (formatted ) {
244- String prefix = String .join ("" , Collections .nCopies (indentationLevel , INDENT ));
245- builder .append (prefix );
248+ String prefix = String .join ("" , Collections .nCopies (indentationLevel , INDENT ));
249+ builder .append (prefix );
246250 }
247251 }
248252
@@ -256,4 +260,4 @@ private void sendAndClearData() {
256260 getReceiver ().process (builder .toString ());
257261 builder .delete (0 , builder .length ());
258262 }
259- }
263+ }
0 commit comments