新疆交投路损赔补偿系统 v1.0.2版本的 MD5 值为:5e336668c8c96e65d739f00d1fa2a3b9

以下内容为反编译后的 XfaForm.java 源代码,内容仅作参考


package com.itextpdf.text.pdf;

import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.xml.XmlDomWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XfaForm {
    public static final String XFA_DATA_SCHEMA = "http://www.xfa.org/schema/xfa-data/1.0/";
    private AcroFieldsSearch acroFieldsSom;
    private boolean changed;
    private Node datasetsNode;
    private Xml2SomDatasets datasetsSom;
    private Document domDocument;
    private PdfReader reader;
    private Node templateNode;
    private Xml2SomTemplate templateSom;
    private boolean xfaPresent;

    public XfaForm() {
    }

    public static PdfObject getXfaObject(PdfReader reader) {
        PdfDictionary af = (PdfDictionary) PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
        if (af == null) {
            return null;
        }
        return PdfReader.getPdfObjectRelease(af.get(PdfName.XFA));
    }

    public XfaForm(PdfReader reader) throws IOException, ParserConfigurationException, SAXException {
        this.reader = reader;
        PdfObject xfa = getXfaObject(reader);
        if (xfa == null) {
            this.xfaPresent = false;
            return;
        }
        this.xfaPresent = true;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        if (xfa.isArray()) {
            PdfArray ar = (PdfArray) xfa;
            for (int k = 1; k < ar.size(); k += 2) {
                PdfObject ob = ar.getDirectObject(k);
                if (ob instanceof PRStream) {
                    byte[] b = PdfReader.getStreamBytes((PRStream) ob);
                    bout.write(b);
                }
            }
        } else if (xfa instanceof PRStream) {
            byte[] b2 = PdfReader.getStreamBytes((PRStream) xfa);
            bout.write(b2);
        }
        bout.close();
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        fact.setNamespaceAware(true);
        DocumentBuilder db = fact.newDocumentBuilder();
        this.domDocument = db.parse(new ByteArrayInputStream(bout.toByteArray()));
        extractNodes();
    }

    private void extractNodes() {
        Map<String, Node> xfaNodes = extractXFANodes(this.domDocument);
        if (xfaNodes.containsKey("template")) {
            this.templateNode = xfaNodes.get("template");
            this.templateSom = new Xml2SomTemplate(this.templateNode);
        }
        if (xfaNodes.containsKey("datasets")) {
            this.datasetsNode = xfaNodes.get("datasets");
            this.datasetsSom = new Xml2SomDatasets(this.datasetsNode.getFirstChild());
        }
        if (this.datasetsNode == null) {
            createDatasetsNode(this.domDocument.getFirstChild());
        }
    }

    public static Map<String, Node> extractXFANodes(Document domDocument) {
        Map<String, Node> xfaNodes = new HashMap<>();
        Node n = domDocument.getFirstChild();
        while (n.getChildNodes().getLength() == 0) {
            n = n.getNextSibling();
        }
        for (Node n2 = n.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
            if (n2.getNodeType() == 1) {
                String s = n2.getLocalName();
                xfaNodes.put(s, n2);
            }
        }
        return xfaNodes;
    }

    private void createDatasetsNode(Node n) {
        while (n.getChildNodes().getLength() == 0) {
            n = n.getNextSibling();
        }
        if (n != null) {
            Element e = n.getOwnerDocument().createElement("xfa:datasets");
            e.setAttribute("xmlns:xfa", XFA_DATA_SCHEMA);
            this.datasetsNode = e;
            n.appendChild(this.datasetsNode);
        }
    }

    public static void setXfa(XfaForm form, PdfReader reader, PdfWriter writer) throws IOException {
        PdfDictionary af = (PdfDictionary) PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
        if (af != null) {
            PdfObject xfa = getXfaObject(reader);
            if (xfa.isArray()) {
                PdfArray ar = (PdfArray) xfa;
                int t = -1;
                int d = -1;
                for (int k = 0; k < ar.size(); k += 2) {
                    PdfString s = ar.getAsString(k);
                    if ("template".equals(s.toString())) {
                        t = k + 1;
                    }
                    if ("datasets".equals(s.toString())) {
                        d = k + 1;
                    }
                }
                if (t > -1 && d > -1) {
                    reader.killXref(ar.getAsIndirectObject(t));
                    reader.killXref(ar.getAsIndirectObject(d));
                    PdfStream tStream = new PdfStream(serializeDoc(form.templateNode));
                    tStream.flateCompress(writer.getCompressionLevel());
                    ar.set(t, writer.addToBody(tStream).getIndirectReference());
                    PdfStream dStream = new PdfStream(serializeDoc(form.datasetsNode));
                    dStream.flateCompress(writer.getCompressionLevel());
                    ar.set(d, writer.addToBody(dStream).getIndirectReference());
                    af.put(PdfName.XFA, new PdfArray(ar));
                    return;
                }
            }
            reader.killXref(af.get(PdfName.XFA));
            PdfStream str = new PdfStream(serializeDoc(form.domDocument));
            str.flateCompress(writer.getCompressionLevel());
            PdfIndirectReference ref = writer.addToBody(str).getIndirectReference();
            af.put(PdfName.XFA, ref);
        }
    }

    public void setXfa(PdfWriter writer) throws IOException {
        setXfa(this, this.reader, writer);
    }

    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();
    }

    public boolean isXfaPresent() {
        return this.xfaPresent;
    }

    public Document getDomDocument() {
        return this.domDocument;
    }

    public String findFieldName(String name, AcroFields af) {
        Map<String, AcroFields.Item> items = af.getFields();
        if (!items.containsKey(name)) {
            if (this.acroFieldsSom == null) {
                if (items.isEmpty() && this.xfaPresent) {
                    this.acroFieldsSom = new AcroFieldsSearch(this.datasetsSom.getName2Node().keySet());
                } else {
                    this.acroFieldsSom = new AcroFieldsSearch(items.keySet());
                }
            }
            if (this.acroFieldsSom.getAcroShort2LongName().containsKey(name)) {
                return this.acroFieldsSom.getAcroShort2LongName().get(name);
            }
            return this.acroFieldsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
        }
        return name;
    }

    public String findDatasetsName(String name) {
        return this.datasetsSom.getName2Node().containsKey(name) ? name : this.datasetsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
    }

    public Node findDatasetsNode(String name) {
        String name2;
        if (name == null || (name2 = findDatasetsName(name)) == null) {
            return null;
        }
        return this.datasetsSom.getName2Node().get(name2);
    }

    public static String getNodeText(Node n) {
        return n == null ? PdfObject.NOTHING : getNodeText(n, PdfObject.NOTHING);
    }

    private static String getNodeText(Node n, String name) {
        for (Node n2 = n.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
            if (n2.getNodeType() == 1) {
                name = getNodeText(n2, name);
            } else if (n2.getNodeType() == 3) {
                name = name + n2.getNodeValue();
            }
        }
        return name;
    }

    public void setNodeText(Node n, String text) {
        if (n != null) {
            while (true) {
                Node nc = n.getFirstChild();
                if (nc == null) {
                    break;
                }
                n.removeChild(nc);
            }
            if (n.getAttributes().getNamedItemNS(XFA_DATA_SCHEMA, "dataNode") != null) {
                n.getAttributes().removeNamedItemNS(XFA_DATA_SCHEMA, "dataNode");
            }
            n.appendChild(this.domDocument.createTextNode(text));
            this.changed = true;
        }
    }

    public void setXfaPresent(boolean xfaPresent) {
        this.xfaPresent = xfaPresent;
    }

    public void setDomDocument(Document domDocument) {
        this.domDocument = domDocument;
        extractNodes();
    }

    public PdfReader getReader() {
        return this.reader;
    }

    public void setReader(PdfReader reader) {
        this.reader = reader;
    }

    public boolean isChanged() {
        return this.changed;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    public static class InverseStore {
        protected ArrayList<String> part = new ArrayList<>();
        protected ArrayList<Object> follow = new ArrayList<>();

        public String getDefaultName() {
            InverseStore store = this;
            while (true) {
                Object obj = store.follow.get(0);
                if (obj instanceof String) {
                    return (String) obj;
                }
                store = (InverseStore) obj;
            }
        }

        public boolean isSimilar(String name) {
            int idx = name.indexOf(91);
            String name2 = name.substring(0, idx + 1);
            for (int k = 0; k < this.part.size(); k++) {
                if (this.part.get(k).startsWith(name2)) {
                    return true;
                }
            }
            return false;
        }
    }

    public static class Stack2<T> extends ArrayList<T> {
        private static final long serialVersionUID = -7451476576174095212L;

        public T peek() {
            if (size() == 0) {
                throw new EmptyStackException();
            }
            return get(size() - 1);
        }

        public T pop() {
            if (size() == 0) {
                throw new EmptyStackException();
            }
            T ret = get(size() - 1);
            remove(size() - 1);
            return ret;
        }

        public T push(T item) {
            add(item);
            return item;
        }

        public boolean empty() {
            return size() == 0;
        }
    }

    public static class Xml2Som {
        protected int anform;
        protected HashMap<String, InverseStore> inverseSearch;
        protected HashMap<String, Node> name2Node;
        protected ArrayList<String> order;
        protected Stack2<String> stack;

        public static String escapeSom(String s) {
            if (s == null) {
                return PdfObject.NOTHING;
            }
            int idx = s.indexOf(46);
            if (idx >= 0) {
                StringBuffer sb = new StringBuffer();
                int last = 0;
                while (idx >= 0) {
                    sb.append(s.substring(last, idx));
                    sb.append('\\');
                    last = idx;
                    idx = s.indexOf(46, idx + 1);
                }
                sb.append(s.substring(last));
                return sb.toString();
            }
            return s;
        }

        public static String unescapeSom(String s) {
            int idx = s.indexOf(92);
            if (idx >= 0) {
                StringBuffer sb = new StringBuffer();
                int last = 0;
                while (idx >= 0) {
                    sb.append(s.substring(last, idx));
                    last = idx + 1;
                    idx = s.indexOf(92, idx + 1);
                }
                sb.append(s.substring(last));
                return sb.toString();
            }
            return s;
        }

        protected String printStack() {
            if (this.stack.empty()) {
                return PdfObject.NOTHING;
            }
            StringBuffer s = new StringBuffer();
            for (int k = 0; k < this.stack.size(); k++) {
                s.append('.').append(this.stack.get(k));
            }
            return s.substring(1);
        }

        public static String getShortName(String s) {
            int idx = s.indexOf(".#subform[");
            if (idx >= 0) {
                int last = 0;
                StringBuffer sb = new StringBuffer();
                while (idx >= 0) {
                    sb.append(s.substring(last, idx));
                    int idx2 = s.indexOf("]", idx + 10);
                    if (idx2 < 0) {
                        return sb.toString();
                    }
                    last = idx2 + 1;
                    idx = s.indexOf(".#subform[", last);
                }
                sb.append(s.substring(last));
                return sb.toString();
            }
            return s;
        }

        public void inverseSearchAdd(String unstack) {
            inverseSearchAdd(this.inverseSearch, this.stack, unstack);
        }

        public static void inverseSearchAdd(HashMap<String, InverseStore> inverseSearch, Stack2<String> stack, String unstack) {
            InverseStore store2;
            String last = stack.peek();
            InverseStore store = inverseSearch.get(last);
            if (store == null) {
                store = new InverseStore();
                inverseSearch.put(last, store);
            }
            for (int k = stack.size() - 2; k >= 0; k--) {
                String last2 = stack.get(k);
                int idx = store.part.indexOf(last2);
                if (idx < 0) {
                    store.part.add(last2);
                    store2 = new InverseStore();
                    store.follow.add(store2);
                } else {
                    store2 = (InverseStore) store.follow.get(idx);
                }
                store = store2;
            }
            store.part.add(PdfObject.NOTHING);
            store.follow.add(unstack);
        }

        public String inverseSearchGlobal(ArrayList<String> parts) {
            InverseStore store;
            if (parts.isEmpty() || (store = this.inverseSearch.get(parts.get(parts.size() - 1))) == null) {
                return null;
            }
            for (int k = parts.size() - 2; k >= 0; k--) {
                String part = 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();
        }

        public static Stack2<String> splitParts(String name) {
            int pos;
            while (name.startsWith(".")) {
                name = name.substring(1);
            }
            Stack2<String> parts = new Stack2<>();
            int last = 0;
            while (true) {
                int pos2 = last;
                while (true) {
                    pos = name.indexOf(46, pos2);
                    if (pos >= 0 && name.charAt(pos - 1) == '\\') {
                        pos2 = pos + 1;
                    }
                }
                if (pos < 0) {
                    break;
                }
                String part = name.substring(last, pos);
                if (!part.endsWith("]")) {
                    part = part + "[0]";
                }
                parts.add(part);
                last = pos + 1;
            }
            String part2 = name.substring(last);
            if (!part2.endsWith("]")) {
                part2 = part2 + "[0]";
            }
            parts.add(part2);
            return parts;
        }

        public ArrayList<String> getOrder() {
            return this.order;
        }

        public void setOrder(ArrayList<String> order) {
            this.order = order;
        }

        public HashMap<String, Node> getName2Node() {
            return this.name2Node;
        }

        public void setName2Node(HashMap<String, Node> name2Node) {
            this.name2Node = name2Node;
        }

        public HashMap<String, InverseStore> getInverseSearch() {
            return this.inverseSearch;
        }

        public void setInverseSearch(HashMap<String, InverseStore> inverseSearch) {
            this.inverseSearch = inverseSearch;
        }
    }

    public static class Xml2SomDatasets extends Xml2Som {
        public Xml2SomDatasets(Node n) {
            this.order = new ArrayList<>();
            this.name2Node = new HashMap<>();
            this.stack = new Stack2<>();
            this.anform = 0;
            this.inverseSearch = new HashMap<>();
            processDatasetsInternal(n);
        }

        public Node insertNode(Node n, String shortName) {
            Stack2<String> stack = splitParts(shortName);
            Document doc = n.getOwnerDocument();
            Node n2 = null;
            Node n3 = n.getFirstChild();
            while (n3.getNodeType() != 1) {
                n3 = n3.getNextSibling();
            }
            for (int k = 0; k < stack.size(); k++) {
                String part = stack.get(k);
                int idx = part.lastIndexOf(91);
                String name = part.substring(0, idx);
                int idx2 = Integer.parseInt(part.substring(idx + 1, part.length() - 1));
                int found = -1;
                n2 = n3.getFirstChild();
                while (n2 != null) {
                    if (n2.getNodeType() == 1) {
                        String s = escapeSom(n2.getLocalName());
                        if (s.equals(name) && (found = found + 1) == idx2) {
                            break;
                        }
                    }
                    n2 = n2.getNextSibling();
                }
                while (found < idx2) {
                    Node n22 = doc.createElementNS(null, name);
                    n2 = n3.appendChild(n22);
                    Node attr = doc.createAttributeNS(XfaForm.XFA_DATA_SCHEMA, "dataNode");
                    attr.setNodeValue("dataGroup");
                    n2.getAttributes().setNamedItemNS(attr);
                    found++;
                }
                n3 = n2;
            }
            inverseSearchAdd(this.inverseSearch, stack, shortName);
            this.name2Node.put(shortName, n2);
            this.order.add(shortName);
            return n2;
        }

        private static boolean hasChildren(Node n) {
            Node dataNodeN = n.getAttributes().getNamedItemNS(XfaForm.XFA_DATA_SCHEMA, "dataNode");
            if (dataNodeN != null) {
                String dataNode = dataNodeN.getNodeValue();
                if ("dataGroup".equals(dataNode)) {
                    return true;
                }
                if ("dataValue".equals(dataNode)) {
                    return false;
                }
            }
            if (n.hasChildNodes()) {
                for (Node n2 = n.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
                    if (n2.getNodeType() == 1) {
                        return true;
                    }
                }
                return false;
            }
            return false;
        }

        private void processDatasetsInternal(Node n) {
            Integer i;
            if (n != null) {
                HashMap<String, Integer> ss = new HashMap<>();
                for (Node n2 = n.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
                    if (n2.getNodeType() == 1) {
                        String s = escapeSom(n2.getLocalName());
                        Integer i2 = ss.get(s);
                        if (i2 == null) {
                            i = 0;
                        } else {
                            i = Integer.valueOf(i2.intValue() + 1);
                        }
                        ss.put(s, i);
                        if (hasChildren(n2)) {
                            this.stack.push(s + "[" + i.toString() + "]");
                            processDatasetsInternal(n2);
                            this.stack.pop();
                        } else {
                            this.stack.push(s + "[" + i.toString() + "]");
                            String unstack = printStack();
                            this.order.add(unstack);
                            inverseSearchAdd(unstack);
                            this.name2Node.put(unstack, n2);
                            this.stack.pop();
                        }
                    }
                }
            }
        }
    }

    public static class AcroFieldsSearch extends Xml2Som {
        private HashMap<String, String> acroShort2LongName;

        public AcroFieldsSearch(Collection<String> items) {
            this.inverseSearch = new HashMap<>();
            this.acroShort2LongName = new HashMap<>();
            for (String string : items) {
                String itemShort = getShortName(string);
                this.acroShort2LongName.put(itemShort, string);
                inverseSearchAdd(this.inverseSearch, splitParts(itemShort), string);
            }
        }

        public HashMap<String, String> getAcroShort2LongName() {
            return this.acroShort2LongName;
        }

        public void setAcroShort2LongName(HashMap<String, String> acroShort2LongName) {
            this.acroShort2LongName = acroShort2LongName;
        }
    }

    public static class Xml2SomTemplate extends Xml2Som {
        private boolean dynamicForm;
        private int templateLevel;

        public Xml2SomTemplate(Node n) {
            this.order = new ArrayList<>();
            this.name2Node = new HashMap<>();
            this.stack = new Stack2<>();
            this.anform = 0;
            this.templateLevel = 0;
            this.inverseSearch = new HashMap<>();
            processTemplate(n, null);
        }

        public String getFieldType(String s) {
            Node n = this.name2Node.get(s);
            if (n == null) {
                return null;
            }
            if ("exclGroup".equals(n.getLocalName())) {
                return "exclGroup";
            }
            Node ui = n.getFirstChild();
            while (ui != null && (ui.getNodeType() != 1 || !"ui".equals(ui.getLocalName()))) {
                ui = ui.getNextSibling();
            }
            if (ui != null) {
                for (Node type = ui.getFirstChild(); type != null; type = type.getNextSibling()) {
                    if (type.getNodeType() == 1 && (!"extras".equals(type.getLocalName()) || !"picture".equals(type.getLocalName()))) {
                        return type.getLocalName();
                    }
                }
                return null;
            }
            return null;
        }

        private void processTemplate(Node n, HashMap<String, Integer> ff) {
            Integer i;
            Integer i2;
            if (ff == null) {
                ff = new HashMap<>();
            }
            HashMap<String, Integer> ss = new HashMap<>();
            for (Node n2 = n.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
                if (n2.getNodeType() == 1) {
                    String s = n2.getLocalName();
                    if ("subform".equals(s)) {
                        Node name = n2.getAttributes().getNamedItem("name");
                        String nn = "#subform";
                        boolean annon = true;
                        if (name != null) {
                            nn = escapeSom(name.getNodeValue());
                            annon = false;
                        }
                        if (annon) {
                            i2 = Integer.valueOf(this.anform);
                            this.anform++;
                        } else {
                            Integer i3 = ss.get(nn);
                            if (i3 == null) {
                                i2 = 0;
                            } else {
                                i2 = Integer.valueOf(i3.intValue() + 1);
                            }
                            ss.put(nn, i2);
                        }
                        this.stack.push(nn + "[" + i2.toString() + "]");
                        this.templateLevel++;
                        if (annon) {
                            processTemplate(n2, ff);
                        } else {
                            processTemplate(n2, null);
                        }
                        this.templateLevel--;
                        this.stack.pop();
                    } else if ("field".equals(s) || "exclGroup".equals(s)) {
                        Node name2 = n2.getAttributes().getNamedItem("name");
                        if (name2 != null) {
                            String nn2 = escapeSom(name2.getNodeValue());
                            Integer i4 = ff.get(nn2);
                            if (i4 == null) {
                                i = 0;
                            } else {
                                i = Integer.valueOf(i4.intValue() + 1);
                            }
                            ff.put(nn2, i);
                            this.stack.push(nn2 + "[" + i.toString() + "]");
                            String unstack = printStack();
                            this.order.add(unstack);
                            inverseSearchAdd(unstack);
                            this.name2Node.put(unstack, n2);
                            this.stack.pop();
                        }
                    } else if (!this.dynamicForm && this.templateLevel > 0 && "occur".equals(s)) {
                        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) {
                            }
                        }
                        Node a2 = n2.getAttributes().getNamedItem("min");
                        if (a2 != null) {
                            try {
                                min = Integer.parseInt(a2.getNodeValue().trim());
                            } catch (Exception e2) {
                            }
                        }
                        Node a3 = n2.getAttributes().getNamedItem("max");
                        if (a3 != null) {
                            try {
                                max = Integer.parseInt(a3.getNodeValue().trim());
                            } catch (Exception e3) {
                            }
                        }
                        if (initial != min || min != max) {
                            this.dynamicForm = true;
                        }
                    }
                }
            }
        }

        public boolean isDynamicForm() {
            return this.dynamicForm;
        }

        public void setDynamicForm(boolean dynamicForm) {
            this.dynamicForm = dynamicForm;
        }
    }

    public Xml2SomTemplate getTemplateSom() {
        return this.templateSom;
    }

    public void setTemplateSom(Xml2SomTemplate templateSom) {
        this.templateSom = templateSom;
    }

    public Xml2SomDatasets getDatasetsSom() {
        return this.datasetsSom;
    }

    public void setDatasetsSom(Xml2SomDatasets datasetsSom) {
        this.datasetsSom = datasetsSom;
    }

    public AcroFieldsSearch getAcroFieldsSom() {
        return this.acroFieldsSom;
    }

    public void setAcroFieldsSom(AcroFieldsSearch acroFieldsSom) {
        this.acroFieldsSom = acroFieldsSom;
    }

    public Node getDatasetsNode() {
        return this.datasetsNode;
    }

    public void fillXfaForm(File file) throws IOException {
        fillXfaForm(file, false);
    }

    public void fillXfaForm(File file, boolean readOnly) throws IOException {
        fillXfaForm(new FileInputStream(file), readOnly);
    }

    public void fillXfaForm(InputStream is) throws IOException {
        fillXfaForm(is, false);
    }

    public void fillXfaForm(InputStream is, boolean readOnly) throws IOException {
        fillXfaForm(new InputSource(is), readOnly);
    }

    public void fillXfaForm(InputSource is) throws IOException {
        fillXfaForm(is, false);
    }

    public void fillXfaForm(InputSource is, boolean readOnly) throws IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document newdoc = db.parse(is);
            fillXfaForm(newdoc.getDocumentElement(), readOnly);
        } catch (ParserConfigurationException e) {
            throw new ExceptionConverter(e);
        } catch (SAXException e2) {
            throw new ExceptionConverter(e2);
        }
    }

    public void fillXfaForm(Node node) {
        fillXfaForm(node, false);
    }

    public void fillXfaForm(Node node, boolean readOnly) {
        if (readOnly) {
            NodeList nodeList = this.domDocument.getElementsByTagName("field");
            for (int i = 0; i < nodeList.getLength(); i++) {
                ((Element) nodeList.item(i)).setAttribute("access", "readOnly");
            }
        }
        NodeList allChilds = this.datasetsNode.getChildNodes();
        int len = allChilds.getLength();
        Node data = null;
        int k = 0;
        while (true) {
            if (k >= len) {
                break;
            }
            Node n = allChilds.item(k);
            if (n.getNodeType() != 1 || !n.getLocalName().equals("data") || !XFA_DATA_SCHEMA.equals(n.getNamespaceURI())) {
                k++;
            } else {
                data = n;
                break;
            }
        }
        if (data == null) {
            data = this.datasetsNode.getOwnerDocument().createElementNS(XFA_DATA_SCHEMA, "xfa:data");
            this.datasetsNode.appendChild(data);
        }
        NodeList list = data.getChildNodes();
        if (list.getLength() == 0) {
            data.appendChild(this.domDocument.importNode(node, true));
        } else {
            Node firstNode = getFirstElementNode(data);
            if (firstNode != null) {
                data.replaceChild(this.domDocument.importNode(node, true), firstNode);
            }
        }
        extractNodes();
        setChanged(true);
    }

    private Node getFirstElementNode(Node src) {
        NodeList list = src.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeType() == 1) {
                Node result = list.item(i);
                return result;
            }
        }
        return null;
    }
}