Skip to content

Commit d31a200

Browse files
committed
PDFBOX-5660: refactor
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1924887 13f79535-47bb-0310-9956-ffa450edef68
1 parent a8b4ddf commit d31a200

File tree

1 file changed

+141
-140
lines changed

1 file changed

+141
-140
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java

Lines changed: 141 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,17 @@ private COSDictionary parseStampAnnotationAppearanceXML(Element appearanceXML) t
164164
if (node instanceof Element)
165165
{
166166
Element child = (Element) node;
167-
if ("STREAM".equalsIgnoreCase(child.getTagName()))
167+
String childTagName = child.getTagName();
168+
if ("STREAM".equalsIgnoreCase(childTagName))
168169
{
169170
LOG.debug("{} => Process {} item in the dictionary after processing the {}",
170-
parentAttrKey, child.getAttribute("KEY"), child.getTagName());
171+
parentAttrKey, child.getAttribute("KEY"), childTagName);
171172
dictionary.setItem(child.getAttribute("KEY"), parseStreamElement(child));
172173
LOG.debug("{} => Set {}", parentAttrKey, child.getAttribute("KEY"));
173174
}
174175
else
175176
{
176-
LOG.warn("{} => Not handling element: {}", parentAttrKey, child.getTagName());
177+
LOG.warn("{} => Not handling element: {}", parentAttrKey, childTagName);
177178
}
178179
}
179180
}
@@ -196,77 +197,75 @@ private COSStream parseStreamElement(Element streamEl) throws IOException
196197
Element child = (Element) node;
197198
String childAttrKey = child.getAttribute("KEY");
198199
String childAttrVal = child.getAttribute("VAL");
199-
LOG.debug("{} => reading child: {} with key: {}", () -> parentAttrKey, () -> child.getTagName(),
200-
() -> childAttrKey);
201-
if ("INT".equalsIgnoreCase(child.getTagName()))
200+
String childTagName = child.getTagName();
201+
LOG.debug("{} => reading child: {} with key: {}", parentAttrKey, childTagName, childAttrKey);
202+
if (childTagName == null)
202203
{
203-
if (!"Length".equals(childAttrKey))
204-
{
205-
stream.setInt(COSName.getPDFName(childAttrKey), Integer.parseInt(childAttrVal));
206-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
207-
}
208-
}
209-
else if ("FIXED".equalsIgnoreCase(child.getTagName()))
210-
{
211-
stream.setFloat(COSName.getPDFName(childAttrKey), Float.parseFloat(childAttrVal));
212-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
213-
}
214-
else if ("NAME".equalsIgnoreCase(child.getTagName()))
215-
{
216-
stream.setName(COSName.getPDFName(childAttrKey), childAttrVal);
217-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
218-
}
219-
else if ("BOOL".equalsIgnoreCase(child.getTagName()))
220-
{
221-
stream.setBoolean(COSName.getPDFName(childAttrKey), Boolean.parseBoolean(childAttrVal));
222-
LOG.debug("{} => Set {}", parentAttrKey, childAttrVal);
223-
}
224-
else if ("ARRAY".equalsIgnoreCase(child.getTagName()))
225-
{
226-
stream.setItem(COSName.getPDFName(childAttrKey), parseArrayElement(child));
227-
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
228-
}
229-
else if ("DICT".equalsIgnoreCase(child.getTagName()))
230-
{
231-
stream.setItem(COSName.getPDFName(childAttrKey), parseDictElement(child));
232-
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
233-
}
234-
else if ("STREAM".equalsIgnoreCase(child.getTagName()))
235-
{
236-
stream.setItem(COSName.getPDFName(childAttrKey), parseStreamElement(child));
237-
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
204+
LOG.warn("{} => Not handling child element: null", parentAttrKey);
205+
continue;
238206
}
239-
else if ("DATA".equalsIgnoreCase(child.getTagName()))
207+
switch (childTagName.toUpperCase())
240208
{
241-
LOG.debug("{} => Handling DATA with encoding: {}", parentAttrKey,
242-
child.getAttribute("ENCODING"));
243-
if ("HEX".equals(child.getAttribute("ENCODING")))
244-
{
245-
try (OutputStream os = stream.createRawOutputStream())
209+
case "INT":
210+
if (!"Length".equals(childAttrKey))
246211
{
247-
os.write(Hex.decodeHex(child.getTextContent()));
248-
LOG.debug("{} => Data was streamed", parentAttrKey);
212+
stream.setInt(COSName.getPDFName(childAttrKey), Integer.parseInt(childAttrVal));
213+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
249214
}
250-
}
251-
else if ("ASCII".equals(child.getAttribute("ENCODING")))
252-
{
253-
try (OutputStream os = stream.createOutputStream())
215+
break;
216+
case "FIXED":
217+
stream.setFloat(COSName.getPDFName(childAttrKey), Float.parseFloat(childAttrVal));
218+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
219+
break;
220+
case "NAME":
221+
stream.setName(COSName.getPDFName(childAttrKey), childAttrVal);
222+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
223+
break;
224+
case "BOOL":
225+
stream.setBoolean(COSName.getPDFName(childAttrKey), Boolean.parseBoolean(childAttrVal));
226+
LOG.debug("{} => Set {}", parentAttrKey, childAttrVal);
227+
break;
228+
case "ARRAY":
229+
stream.setItem(COSName.getPDFName(childAttrKey), parseArrayElement(child));
230+
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
231+
break;
232+
case "DICT":
233+
stream.setItem(COSName.getPDFName(childAttrKey), parseDictElement(child));
234+
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
235+
break;
236+
case "STREAM":
237+
stream.setItem(COSName.getPDFName(childAttrKey), parseStreamElement(child));
238+
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
239+
break;
240+
case "DATA":
241+
String childEncodingAttr = child.getAttribute("ENCODING");
242+
LOG.debug("{} => Handling DATA with encoding: {}", parentAttrKey, childEncodingAttr);
243+
if ("HEX".equals(childEncodingAttr))
254244
{
255-
// not sure about charset
256-
os.write(child.getTextContent().getBytes());
257-
LOG.debug("{} => Data was streamed", parentAttrKey);
245+
try (OutputStream os = stream.createRawOutputStream())
246+
{
247+
os.write(Hex.decodeHex(child.getTextContent()));
248+
LOG.debug("{} => Data was streamed", parentAttrKey);
249+
}
258250
}
259-
}
260-
else
261-
{
262-
LOG.warn("{} => Not handling element DATA encoding: {}", parentAttrKey,
263-
child.getAttribute("ENCODING"));
264-
}
265-
}
266-
else
267-
{
268-
LOG.warn("{} => Not handling child element: {}", parentAttrKey,
269-
child.getTagName());
251+
else if ("ASCII".equals(childEncodingAttr))
252+
{
253+
try (OutputStream os = stream.createOutputStream())
254+
{
255+
// not sure about charset
256+
os.write(child.getTextContent().getBytes());
257+
LOG.debug("{} => Data was streamed", parentAttrKey);
258+
}
259+
}
260+
else
261+
{
262+
LOG.warn("{} => Not handling element DATA encoding: {}", parentAttrKey,
263+
childEncodingAttr);
264+
}
265+
break;
266+
default:
267+
LOG.warn("{} => Not handling child element: {}", parentAttrKey, childTagName);
268+
break;
270269
}
271270
}
272271
}
@@ -301,42 +300,44 @@ else if ("Matrix".equals(parentAttrKey) && nodeList.getLength() < 6)
301300
Element child = (Element) node;
302301
String childAttrKey = child.getAttribute("KEY");
303302
String childAttrVal = child.getAttribute("VAL");
304-
LOG.debug("{} => reading child: {} with key: {}", parentAttrKey, child.getTagName(),
303+
String childTagName = child.getTagName();
304+
LOG.debug("{} => reading child: {} with key: {}", parentAttrKey, childTagName,
305305
childAttrKey);
306-
if ("INT".equalsIgnoreCase(child.getTagName()) || "FIXED".equalsIgnoreCase(child.getTagName()))
306+
if (null == childTagName)
307307
{
308-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
309-
array.add(COSNumber.get(childAttrVal));
308+
LOG.warn("{} => Not handling child element: null", parentAttrKey);
309+
continue;
310310
}
311-
else if ("NAME".equalsIgnoreCase(child.getTagName()))
311+
switch (childTagName.toUpperCase())
312312
{
313-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
314-
array.add(COSName.getPDFName(childAttrVal));
315-
}
316-
else if ("BOOL".equalsIgnoreCase(child.getTagName()))
317-
{
318-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
319-
array.add(COSBoolean.getBoolean(Boolean.parseBoolean(childAttrVal)));
320-
}
321-
else if ("DICT".equalsIgnoreCase(child.getTagName()))
322-
{
323-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
324-
array.add(parseDictElement(child));
325-
}
326-
else if ("STREAM".equalsIgnoreCase(child.getTagName()))
327-
{
328-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
329-
array.add(parseStreamElement(child));
330-
}
331-
else if ("ARRAY".equalsIgnoreCase(child.getTagName()))
332-
{
333-
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
334-
array.add(parseArrayElement(child));
335-
}
336-
else
337-
{
338-
LOG.warn("{} => Not handling child element: {}", parentAttrKey,
339-
child.getTagName());
313+
case "INT":
314+
case "FIXED":
315+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
316+
array.add(COSNumber.get(childAttrVal));
317+
break;
318+
case "NAME":
319+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
320+
array.add(COSName.getPDFName(childAttrVal));
321+
break;
322+
case "BOOL":
323+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
324+
array.add(COSBoolean.getBoolean(Boolean.parseBoolean(childAttrVal)));
325+
break;
326+
case "DICT":
327+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
328+
array.add(parseDictElement(child));
329+
break;
330+
case "STREAM":
331+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
332+
array.add(parseStreamElement(child));
333+
break;
334+
case "ARRAY":
335+
LOG.debug("{} value({}): {}", parentAttrKey, i, childAttrVal);
336+
array.add(parseArrayElement(child));
337+
break;
338+
default:
339+
LOG.warn("{} => Not handling child element: {}", parentAttrKey, childTagName);
340+
break;
340341
}
341342
}
342343
}
@@ -360,51 +361,51 @@ private COSDictionary parseDictElement(Element dictEl) throws IOException
360361
Element child = (Element) node;
361362
String childAttrKey = child.getAttribute("KEY");
362363
String childAttrVal = child.getAttribute("VAL");
364+
String childTagName = child.getTagName();
363365

364-
if ("DICT".equals(child.getTagName()))
365-
{
366-
LOG.debug("{} => Handling DICT element with key: {}", parentAttrKey,
367-
childAttrKey);
368-
dict.setItem(COSName.getPDFName(childAttrKey), parseDictElement(child));
369-
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
370-
}
371-
else if ("STREAM".equals(child.getTagName()))
372-
{
373-
LOG.debug("{} => Handling STREAM element with key: {}", parentAttrKey,
374-
childAttrKey);
375-
dict.setItem(COSName.getPDFName(childAttrKey), parseStreamElement(child));
376-
}
377-
else if ("NAME".equals(child.getTagName()))
366+
if (childTagName == null)
378367
{
379-
LOG.debug("{} => Handling NAME element with key: {}", parentAttrKey,
380-
childAttrKey);
381-
dict.setName(COSName.getPDFName(childAttrKey), childAttrVal);
382-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
368+
LOG.warn("{} => NOT handling child element: null", parentAttrKey);
369+
continue;
383370
}
384-
else if ("INT".equalsIgnoreCase(child.getTagName()))
371+
switch (childTagName)
385372
{
386-
dict.setInt(COSName.getPDFName(childAttrKey), Integer.parseInt(childAttrVal));
387-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
388-
}
389-
else if ("FIXED".equalsIgnoreCase(child.getTagName()))
390-
{
391-
dict.setFloat(COSName.getPDFName(childAttrKey), Float.parseFloat(childAttrVal));
392-
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
393-
}
394-
else if ("BOOL".equalsIgnoreCase(child.getTagName()))
395-
{
396-
dict.setBoolean(COSName.getPDFName(childAttrKey), Boolean.parseBoolean(childAttrVal));
397-
LOG.debug("{} => Set {}", parentAttrKey, childAttrVal);
398-
}
399-
else if ("ARRAY".equalsIgnoreCase(child.getTagName()))
400-
{
401-
dict.setItem(COSName.getPDFName(childAttrKey), parseArrayElement(child));
402-
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
403-
}
404-
else
405-
{
406-
LOG.warn("{} => NOT handling child element: {}", parentAttrKey,
407-
child.getTagName());
373+
case "DICT":
374+
LOG.debug("{} => Handling DICT element with key: {}", parentAttrKey,
375+
childAttrKey);
376+
dict.setItem(COSName.getPDFName(childAttrKey), parseDictElement(child));
377+
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
378+
break;
379+
case "STREAM":
380+
LOG.debug("{} => Handling STREAM element with key: {}", parentAttrKey,
381+
childAttrKey);
382+
dict.setItem(COSName.getPDFName(childAttrKey), parseStreamElement(child));
383+
break;
384+
case "NAME":
385+
LOG.debug("{} => Handling NAME element with key: {}", parentAttrKey,
386+
childAttrKey);
387+
dict.setName(COSName.getPDFName(childAttrKey), childAttrVal);
388+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
389+
break;
390+
case "INT":
391+
dict.setInt(COSName.getPDFName(childAttrKey), Integer.parseInt(childAttrVal));
392+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
393+
break;
394+
case "FIXED":
395+
dict.setFloat(COSName.getPDFName(childAttrKey), Float.parseFloat(childAttrVal));
396+
LOG.debug("{} => Set {}: {}", parentAttrKey, childAttrKey, childAttrVal);
397+
break;
398+
case "BOOL":
399+
dict.setBoolean(COSName.getPDFName(childAttrKey), Boolean.parseBoolean(childAttrVal));
400+
LOG.debug("{} => Set {}", parentAttrKey, childAttrVal);
401+
break;
402+
case "ARRAY":
403+
dict.setItem(COSName.getPDFName(childAttrKey), parseArrayElement(child));
404+
LOG.debug("{} => Set {}", parentAttrKey, childAttrKey);
405+
break;
406+
default:
407+
LOG.warn("{} => NOT handling child element: {}", parentAttrKey, childTagName);
408+
break;
408409
}
409410
}
410411
}

0 commit comments

Comments
 (0)