1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
|
/*
* $Id: XfaForm.java,v 1.2 2006/09/10 15:35:56 psoares33 Exp $
*
* Copyright 2006 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.pdf;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* Processes XFA forms.
* @author Paulo Soares (psoares@consiste.pt)
*/
public class XfaForm {
private Xml2SomTemplate templateSom;
private Xml2SomDatasets datasetsSom;
private AcroFieldsSearch acroFieldsSom;
private PdfReader reader;
private boolean xfaPresent;
private org.w3c.dom.Document domDocument;
private boolean changed;
private Node datasetsNode;
private Node templateNode;
/**
* An empty constructor to build on.
*/
public XfaForm() {
}
/**
* A constructor from a <CODE>PdfReader</CODE>. It basically does everything
* from finding the XFA stream to the XML parsing.
* @param reader the reader
* @throws java.io.IOException on error
* @throws javax.xml.parsers.ParserConfigurationException on error
* @throws org.xml.sax.SAXException on error
*/
public XfaForm(PdfReader reader) throws IOException, ParserConfigurationException, SAXException {
this.reader = reader;
PdfDictionary af = (PdfDictionary)PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
if (af == null) {
xfaPresent = false;
return;
}
PdfObject xfa = PdfReader.getPdfObjectRelease(af.get(PdfName.XFA));
if (xfa == null) {
xfaPresent = false;
return;
}
xfaPresent = true;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (xfa.isArray()) {
ArrayList ar = ((PdfArray)xfa).getArrayList();
for (int k = 1; k < ar.size(); k += 2) {
PdfObject ob = PdfReader.getPdfObject((PdfObject)ar.get(k));
if (ob != null && ob instanceof PRStream) {
byte[] b = PdfReader.getStreamBytes((PRStream)ob);
bout.write(b);
}
}
}
else if (xfa instanceof PRStream) {
byte[] b = PdfReader.getStreamBytes((PRStream)xfa);
bout.write(b);
}
bout.close();
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder db = fact.newDocumentBuilder();
domDocument = db.parse(new ByteArrayInputStream(bout.toByteArray()));
Node n = domDocument.getFirstChild();
n = n.getFirstChild();
while (n != null) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
String s = n.getLocalName();
if (s.equals("template")) {
templateNode = n;
templateSom = new Xml2SomTemplate(n);
}
else if (s.equals("datasets")) {
datasetsNode = n;
datasetsSom = new Xml2SomDatasets(n.getFirstChild());
}
}
n = n.getNextSibling();
}
}
/**
* Sets the XFA key from a byte array. The old XFA is erased.
* @param xfaData the data
* @param reader the reader
* @param writer the writer
* @throws java.io.IOException on error
*/
public static void setXfa(byte[] xfaData, PdfReader reader, PdfWriter writer) throws IOException {
PdfDictionary af = (PdfDictionary)PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
if (af == null) {
return;
}
reader.killXref(af.get(PdfName.XFA));
PdfStream str = new PdfStream(xfaData);
str.flateCompress();
PdfIndirectReference ref = writer.addToBody(str).getIndirectReference();
af.put(PdfName.XFA, ref);
}
/**
* Sets the XFA key from the instance data. The old XFA is erased.
* @param writer the writer
* @throws java.io.IOException on error
*/
public void setXfa(PdfWriter writer) throws IOException {
setXfa(serializeDoc(domDocument), reader, writer);
}
/**
* Serializes a XML document to a byte array.
* @param n the XML document
* @throws java.io.IOException on error
* @return the serialized XML document
*/
public static byte[] serializeDoc(Node n) throws IOException {
XmlDomWriter xw = new XmlDomWriter();
ByteArrayOutputStream fout = new ByteArrayOutputStream();
xw.setOutput(fout, null);
xw.setCanonical(false);
xw.write(n);
fout.close();
return fout.toByteArray();
}
/**
* Returns <CODE>true</CODE> if it is a XFA form.
* @return <CODE>true</CODE> if it is a XFA form
*/
public boolean isXfaPresent() {
return xfaPresent;
}
/**
* Gets the top level DOM document.
* @return the top level DOM document
*/
public org.w3c.dom.Document getDomDocument() {
return domDocument;
}
/**
* Finds the complete field name contained in the "classic" forms from a partial
* name.
* @param name the complete or partial name
* @param af the fields
* @return the complete name or <CODE>null</CODE> if not found
*/
public String findFieldName(String name, AcroFields af) {
HashMap items = af.getFields();
if (items.containsKey(name))
return name;
if (acroFieldsSom == null) {
acroFieldsSom = new AcroFieldsSearch(items.keySet());
}
if (acroFieldsSom.getAcroShort2LongName().containsKey(name))
return (String)acroFieldsSom.getAcroShort2LongName().get(name);
return acroFieldsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
}
/**
* Finds the complete SOM name contained in the datasets section from a
* possibly partial name.
* @param name the complete or partial name
* @return the complete name or <CODE>null</CODE> if not found
*/
public String findDatasetsName(String name) {
if (datasetsSom.getName2Node().containsKey(name))
return name;
return datasetsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
}
/**
* Finds the <CODE>Node</CODE> contained in the datasets section from a
* possibly partial name.
* @param name the complete or partial name
* @return the <CODE>Node</CODE> or <CODE>null</CODE> if not found
*/
public Node findDatasetsNode(String name) {
if (name == null)
return null;
name = findDatasetsName(name);
if (name == null)
return null;
return (Node)datasetsSom.getName2Node().get(name);
}
/**
* Gets all the text contained in the child nodes of this node.
* @param n the <CODE>Node</CODE>
* @return the text found or "" if no text was found
*/
public static String getNodeText(Node n) {
if (n == null)
return "";
return getNodeText(n, "");
}
private static String getNodeText(Node n, String name) {
Node n2 = n.getFirstChild();
while (n2 != null) {
if (n2.getNodeType() == Node.ELEMENT_NODE) {
name = getNodeText(n2, name);
}
else if (n2.getNodeType() == Node.TEXT_NODE) {
name += n2.getNodeValue();
}
n2 = n2.getNextSibling();
}
return name;
}
/**
* Sets the text of this node. All the child's node are deleted and a new
* child text node is created.
* @param n the <CODE>Node</CODE> to add the text to
* @param text the text to add
*/
public void setNodeText(Node n, String text) {
if (n == null)
return;
Node nc = null;
while ((nc = n.getFirstChild()) != null) {
n.removeChild(nc);
}
n.appendChild(domDocument.createTextNode(text));
changed = true;
}
/**
* Sets the XFA form flag signaling that this is a valid XFA form.
* @param xfaPresent the XFA form flag signaling that this is a valid XFA form
*/
public void setXfaPresent(boolean xfaPresent) {
this.xfaPresent = xfaPresent;
}
/**
* Sets the top DOM document.
* @param domDocument the top DOM document
*/
public void setDomDocument(org.w3c.dom.Document domDocument) {
this.domDocument = domDocument;
}
/**
* Gets the <CODE>PdfReader</CODE> used by this instance.
* @return the <CODE>PdfReader</CODE> used by this instance
*/
public PdfReader getReader() {
return reader;
}
/**
* Sets the <CODE>PdfReader</CODE> to be used by this instance.
* @param reader the <CODE>PdfReader</CODE> to be used by this instance
*/
public void setReader(PdfReader reader) {
this.reader = reader;
}
/**
* Checks if this XFA form was changed.
* @return <CODE>true</CODE> if this XFA form was changed
*/
public boolean isChanged() {
return changed;
}
/**
* Sets the changed status of this XFA instance.
* @param changed the changed status of this XFA instance
*/
public void setChanged(boolean changed) {
this.changed = changed;
}
/**
* A structure to store each part of a SOM name and link it to the next part
* beginning from the lower hierarchie.
*/
public static class InverseStore {
protected ArrayList part = new ArrayList();
protected ArrayList follow = new ArrayList();
/**
* Gets the full name by traversing the hiearchie using only the
* index 0.
* @return the full name
*/
public String getDefaultName() {
InverseStore store = this;
while (true) {
Object obj = store.follow.get(0);
if (obj instanceof String)
return (String)obj;
store = (InverseStore)obj;
}
}
/**
* Search the current node for a similar name. A similar name starts
* with the same name but has a differnt index. For example, "detail[3]"
* is similar to "detail[9]". The main use is to discard names that
* correspond to out of bounds records.
* @param name the name to search
* @return <CODE>true</CODE> if a similitude was found
*/
public boolean isSimilar(String name) {
int idx = name.indexOf('[');
name = name.substring(0, idx + 1);
for (int k = 0; k < part.size(); ++k) {
if (((String)part.get(k)).startsWith(name))
return true;
}
return false;
}
}
/**
* Another stack implementation. The main use is to facilitate
* the porting to other languages.
*/
public static class Stack2 extends ArrayList {
/**
* Looks at the object at the top of this stack without removing it from the stack.
* @return the object at the top of this stack
*/
public Object peek() {
if (size() == 0)
throw new EmptyStackException();
return get(size() - 1);
}
/**
* Removes the object at the top of this stack and returns that object as the value of this function.
* @return the object at the top of this stack
*/
public Object pop() {
if (size() == 0)
throw new EmptyStackException();
Object ret = get(size() - 1);
remove(size() - 1);
return ret;
}
/**
* Pushes an item onto the top of this stack.
* @param item the item to be pushed onto this stack
* @return the <CODE>item</CODE> argument
*/
public Object push(Object item) {
add(item);
return item;
}
/**
* Tests if this stack is empty.
* @return <CODE>true</CODE> if and only if this stack contains no items; <CODE>false</CODE> otherwise
*/
public boolean empty() {
return size() == 0;
}
}
/**
* A class for some basic SOM processing.
*/
public static class Xml2Som {
/**
* The order the names appear in the XML, depth first.
*/
protected ArrayList order;
/**
* The mapping of full names to nodes.
*/
protected HashMap name2Node;
/**
* The data to do a search from the bottom hierarchie.
*/
protected HashMap inverseSearch;
/**
* A stack to be used when parsing.
*/
protected Stack2 stack;
/**
* A temporary store for the repetition count.
*/
protected int anform;
/**
* Escapes a SOM string fragment replacing "." with "\.".
* @param s the unescaped string
* @return the escaped string
*/
public static String escapeSom(String s) {
int idx = s.indexOf('.');
if (idx < 0)
return s;
StringBuffer sb = new StringBuffer();
int last = 0;
while (idx >= 0) {
sb.append(s.substring(last, idx));
sb.append('\\');
last = idx;
idx = s.indexOf('.', idx + 1);
}
sb.append(s.substring(last));
return sb.toString();
}
/**
* Unescapes a SOM string fragment replacing "\." with ".".
* @param s the escaped string
* @return the unescaped string
*/
public static String unescapeSom(String s) {
int idx = s.indexOf('\\');
if (idx < 0)
return s;
StringBuffer sb = new StringBuffer();
int last = 0;
while (idx >= 0) {
sb.append(s.substring(last, idx));
last = idx + 1;
idx = s.indexOf('\\', idx + 1);
}
sb.append(s.substring(last));
return sb.toString();
}
/**
* Outputs the stack as the sequence of elements separated
* by '.'.
* @return the stack as the sequence of elements separated by '.'
*/
protected String printStack() {
if (stack.empty())
return "";
StringBuffer s = new StringBuffer();
for (int k = 0; k < stack.size(); ++k)
s.append('.').append((String)stack.get(k));
return s.substring(1);
}
/**
* Gets the name with the <CODE>#subform</CODE> removed.
* @param s the long name
* @return the short name
*/
public static String getShortName(String s) {
int idx = s.indexOf(".#subform[");
if (idx < 0)
return s;
int last = 0;
StringBuffer sb = new StringBuffer();
while (idx >= 0) {
sb.append(s.substring(last, idx));
idx = s.indexOf("]", idx + 10);
if (idx < 0)
return sb.toString();
last = idx + 1;
idx = s.indexOf(".#subform[", last);
}
sb.append(s.substring(last));
return sb.toString();
}
/**
* Adds a SOM name to the search node chain.
* @param unstack the SOM name
*/
public void inverseSearchAdd(String unstack) {
inverseSearchAdd(inverseSearch, stack, unstack);
}
/**
* Adds a SOM name to the search node chain.
* @param inverseSearch the start point
* @param stack the stack with the separeted SOM parts
* @param unstack the full name
*/
public static void inverseSearchAdd(HashMap inverseSearch, Stack2 stack, String unstack) {
String last = (String)stack.peek();
InverseStore store = (InverseStore)inverseSearch.get(last);
if (store == null) {
store = new InverseStore();
inverseSearch.put(last, store);
}
for (int k = stack.size() - 2; k >= 0; --k) {
last = (String)stack.get(k);
InverseStore store2;
int idx = store.part.indexOf(last);
if (idx < 0) {
store.part.add(last);
store2 = new InverseStore();
store.follow.add(store2);
}
else
store2 = (InverseStore)store.follow.get(idx);
store = store2;
}
store.part.add("");
store.follow.add(unstack);
}
/**
* Searchs the SOM hiearchie from the bottom.
* @param parts the SOM parts
* @return the full name or <CODE>null</CODE> if not found
*/
public String inverseSearchGlobal(ArrayList parts) {
if (parts.size() == 0)
return null;
if (parts.size() == 0)
return null;
InverseStore store = (InverseStore)inverseSearch.get(parts.get(parts.size() - 1));
if (store == null)
return null;
for (int k = parts.size() - 2; k >= 0; --k) {
String part = (String)parts.get(k);
int idx = store.part.indexOf(part);
if (idx < 0) {
if (store.isSimilar(part))
return null;
return store.getDefaultName();
}
store = (InverseStore)store.follow.get(idx);
}
return store.getDefaultName();
}
/**
* Splits a SOM name in the individual parts.
* @param name the full SOM name
* @return the split name
*/
public static Stack2 splitParts(String name) {
while (name.startsWith("."))
name = name.substring(1);
Stack2 parts = new Stack2();
int last = 0;
int pos = 0;
String part;
while (true) {
pos = last;
while (true) {
pos = name.indexOf('.', pos);
if (pos < 0)
break;
if (name.charAt(pos - 1) == '\\')
++pos;
else
break;
}
if (pos < 0)
break;
part = name.substring(last, pos);
if (!part.endsWith("]"))
part += "[0]";
parts.add(part);
last = pos + 1;
}
part = name.substring(last);
if (!part.endsWith("]"))
part += "[0]";
parts.add(part);
return parts;
}
/**
* Gets the order the names appear in the XML, depth first.
* @return the order the names appear in the XML, depth first
*/
public ArrayList getOrder() {
return order;
}
/**
* Sets the order the names appear in the XML, depth first
* @param order the order the names appear in the XML, depth first
*/
public void setOrder(ArrayList order) {
this.order = order;
}
/**
* Gets the mapping of full names to nodes.
* @return the mapping of full names to nodes
*/
public HashMap getName2Node() {
return name2Node;
}
/**
* Sets the mapping of full names to nodes.
* @param name2Node the mapping of full names to nodes
*/
public void setName2Node(HashMap name2Node) {
this.name2Node = name2Node;
}
/**
* Gets the data to do a search from the bottom hierarchie.
* @return the data to do a search from the bottom hierarchie
*/
public HashMap getInverseSearch() {
return inverseSearch;
}
/**
* Sets the data to do a search from the bottom hierarchie.
* @param inverseSearch the data to do a search from the bottom hierarchie
*/
public void setInverseSearch(HashMap inverseSearch) {
this.inverseSearch = inverseSearch;
}
}
/**
* Processes the datasets section in the XFA form.
*/
public static class Xml2SomDatasets extends Xml2Som {
/**
* Creates a new instance from the datasets node. This expects
* not the datasets but the data node that comes below.
* @param n the datasets node
*/
public Xml2SomDatasets(Node n) {
order = new ArrayList();
name2Node = new HashMap();
stack = new Stack2();
anform = 0;
inverseSearch = new HashMap();
processDatasetsInternal(n);
}
private static boolean hasChildren(Node n) {
Node dataNodeN = n.getAttributes().getNamedItemNS("http://www.xfa.org/schema/xfa-data/1.0/", "dataNode");
if (dataNodeN != null) {
String dataNode = dataNodeN.getNodeValue();
if ("dataGroup".equals(dataNode))
return true;
else if ("dataValue".equals(dataNode))
return false;
}
if (!n.hasChildNodes())
return false;
Node n2 = n.getFirstChild();
while (n2 != null) {
if (n2.getNodeType() == Node.ELEMENT_NODE) {
return true;
}
n2 = n2.getNextSibling();
}
return false;
}
private void processDatasetsInternal(Node n) {
HashMap ss = new HashMap();
Node n2 = n.getFirstChild();
while (n2 != null) {
if (n2.getNodeType() == Node.ELEMENT_NODE) {
String s = escapeSom(n2.getLocalName());
Integer i = (Integer)ss.get(s);
if (i == null)
i = new Integer(0);
else
i = new Integer(i.intValue() + 1);
ss.put(s, i);
if (hasChildren(n2)) {
stack.push(s + "[" + i.toString() + "]");
processDatasetsInternal(n2);
stack.pop();
}
else {
stack.push(s + "[" + i.toString() + "]");
String unstack = printStack();
order.add(unstack);
inverseSearchAdd(unstack);
name2Node.put(unstack, n2);
stack.pop();
}
}
n2 = n2.getNextSibling();
}
}
}
/**
* A class to process "classic" fields.
*/
public static class AcroFieldsSearch extends Xml2Som {
private HashMap acroShort2LongName;
/**
* Creates a new instance from a Collection with the full names.
* @param items the Collection
*/
public AcroFieldsSearch(Collection items) {
inverseSearch = new HashMap();
acroShort2LongName = new HashMap();
for (Iterator it = items.iterator(); it.hasNext();) {
String itemName = (String)it.next();
String itemShort = getShortName(itemName);
acroShort2LongName.put(itemShort, itemName);
inverseSearchAdd(inverseSearch, splitParts(itemShort), itemName);
}
}
/**
* Gets the mapping from short names to long names. A long
* name may contain the #subform name part.
* @return the mapping from short names to long names
*/
public HashMap getAcroShort2LongName() {
return acroShort2LongName;
}
/**
* Sets the mapping from short names to long names. A long
* name may contain the #subform name part.
* @param acroShort2LongName the mapping from short names to long names
*/
public void setAcroShort2LongName(HashMap acroShort2LongName) {
this.acroShort2LongName = acroShort2LongName;
}
}
/**
* Processes the template section in the XFA form.
*/
public static class Xml2SomTemplate extends Xml2Som {
private boolean dynamicForm;
private int templateLevel;
/**
* Creates a new instance from the datasets node.
* @param n the template node
*/
public Xml2SomTemplate(Node n) {
order = new ArrayList();
name2Node = new HashMap();
stack = new Stack2();
anform = 0;
templateLevel = 0;
inverseSearch = new HashMap();
processTemplate(n, null);
}
/**
* Gets the field type as described in the <CODE>template</CODE> section of the XFA.
* @param s the exact template name
* @return the field type or <CODE>null</CODE> if not found
*/
public String getFieldType(String s) {
Node n = (Node)name2Node.get(s);
if (n == null)
return null;
if (n.getLocalName().equals("exclGroup"))
return "exclGroup";
Node ui = n.getFirstChild();
while (ui != null) {
if (ui.getNodeType() == Node.ELEMENT_NODE && ui.getLocalName().equals("ui")) {
break;
}
ui = ui.getNextSibling();
}
if (ui == null)
return null;
Node type = ui.getFirstChild();
while (type != null) {
if (type.getNodeType() == Node.ELEMENT_NODE && !(type.getLocalName().equals("extras") && type.getLocalName().equals("picture"))) {
return type.getLocalName();
}
type = type.getNextSibling();
}
return null;
}
private void processTemplate(Node n, HashMap ff) {
if (ff == null)
ff = new HashMap();
HashMap ss = new HashMap();
Node n2 = n.getFirstChild();
while (n2 != null) {
if (n2.getNodeType() == Node.ELEMENT_NODE) {
String s = n2.getLocalName();
if (s.equals("subform")) {
Node name = n2.getAttributes().getNamedItem("name");
String nn = "#subform";
boolean annon = true;
if (name != null) {
nn = escapeSom(name.getNodeValue());
annon = false;
}
Integer i;
if (annon) {
i = new Integer(anform);
++anform;
}
else {
i = (Integer)ss.get(nn);
if (i == null)
i = new Integer(0);
else
i = new Integer(i.intValue() + 1);
ss.put(nn, i);
}
stack.push(nn + "[" + i.toString() + "]");
++templateLevel;
if (annon)
processTemplate(n2, ff);
else
processTemplate(n2, null);
--templateLevel;
stack.pop();
}
else if (s.equals("field") || s.equals("exclGroup")) {
Node name = n2.getAttributes().getNamedItem("name");
if (name != null) {
String nn = escapeSom(name.getNodeValue());
Integer i = (Integer)ff.get(nn);
if (i == null)
i = new Integer(0);
else
i = new Integer(i.intValue() + 1);
ff.put(nn, i);
stack.push(nn + "[" + i.toString() + "]");
String unstack = printStack();
order.add(unstack);
inverseSearchAdd(unstack);
name2Node.put(unstack, n2);
stack.pop();
}
}
else if (!dynamicForm && templateLevel > 0 && s.equals("occur")) {
int initial = 1;
int min = 1;
int max = 1;
Node a = n2.getAttributes().getNamedItem("initial");
if (a != null)
try{initial = Integer.parseInt(a.getNodeValue().trim());}catch(Exception e){};
a = n2.getAttributes().getNamedItem("min");
if (a != null)
try{min = Integer.parseInt(a.getNodeValue().trim());}catch(Exception e){};
a = n2.getAttributes().getNamedItem("max");
if (a != null)
try{max = Integer.parseInt(a.getNodeValue().trim());}catch(Exception e){};
if (initial != min || min != max)
dynamicForm = true;
}
}
n2 = n2.getNextSibling();
}
}
/**
* <CODE>true</CODE> if it's a dynamic form; <CODE>false</CODE>
* if it's a static form.
* @return <CODE>true</CODE> if it's a dynamic form; <CODE>false</CODE>
* if it's a static form
*/
public boolean isDynamicForm() {
return dynamicForm;
}
/**
* Sets the dynamic form flag. It doesn't change the template.
* @param dynamicForm the dynamic form flag
*/
public void setDynamicForm(boolean dynamicForm) {
this.dynamicForm = dynamicForm;
}
}
/**
* Gets the class that contains the template processing section of the XFA.
* @return the class that contains the template processing section of the XFA
*/
public Xml2SomTemplate getTemplateSom() {
return templateSom;
}
/**
* Sets the class that contains the template processing section of the XFA
* @param templateSom the class that contains the template processing section of the XFA
*/
public void setTemplateSom(Xml2SomTemplate templateSom) {
this.templateSom = templateSom;
}
/**
* Gets the class that contains the datasets processing section of the XFA.
* @return the class that contains the datasets processing section of the XFA
*/
public Xml2SomDatasets getDatasetsSom() {
return datasetsSom;
}
/**
* Sets the class that contains the datasets processing section of the XFA.
* @param datasetsSom the class that contains the datasets processing section of the XFA
*/
public void setDatasetsSom(Xml2SomDatasets datasetsSom) {
this.datasetsSom = datasetsSom;
}
/**
* Gets the class that contains the "classic" fields processing.
* @return the class that contains the "classic" fields processing
*/
public AcroFieldsSearch getAcroFieldsSom() {
return acroFieldsSom;
}
/**
* Sets the class that contains the "classic" fields processing.
* @param acroFieldsSom the class that contains the "classic" fields processing
*/
public void setAcroFieldsSom(AcroFieldsSearch acroFieldsSom) {
this.acroFieldsSom = acroFieldsSom;
}
}
|