<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.pdmodel.fdf;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSNumber;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
import org.apache.pdfbox.pdmodel.interactive.action.PDAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.util.XMLUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This represents an FDF field that is part of the FDF document.
 *
 * @author <NAME>
 */
public class FDFField implements COSObjectable
{
    private final COSDictionary field;

    /**
     * Default constructor.
     */
    public FDFField()
    {
        field = new COSDictionary();
    }

    /**
     * Constructor.
     *
     * @param f The FDF field.
     */
    public FDFField(COSDictionary f)
    {
        field = f;
    }

    /**
     * This will create an FDF field from an XFDF XML document.
     *
     * @param fieldXML The XML document that contains the XFDF data.
     * @throws IOException If there is an error reading from the dom.
     */
    public FDFField(Element fieldXML) throws IOException
    {
        this();
        this.setPartialFieldName(fieldXML.getAttribute("name"));
        NodeList nodeList = fieldXML.getChildNodes();
        List<FDFField> kids = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++)
        {
            Node node = nodeList.item(i);
            if (node instanceof Element)
            {
                Element child = (Element) node;
                switch (child.getTagName())
                {
                    case "value":
                        setValue(XMLUtil.getNodeValue(child));
                        break;
                    case "value-richtext":
                        setRichText(new COSString(XMLUtil.getNodeValue(child)));
                        break;
                    case "field":
                        kids.add(new FDFField(child));
                        break;
                    default:
                        break;
                }
            }
        }
        if (kids.size() > 0)
        {
            setKids(kids);
        }

    }

    /**
     * This will write this element as an XML document.
     *
     * @param output The stream to write the xml to.
     *
     * @throws IOException If there is an error writing the XML.
     */
    public void writeXML(Writer output) throws IOException
    {
        output.write("<field name=\"");
        output.write(getPartialFieldName());
        output.write("\">\n");

        Object value = getValue();

        if (value instanceof String)
        {
            output.write("<value>");
            output.write(escapeXML((String) value));
            output.write("</value>\n");
        }
        else if (value instanceof List)
        {
            List<String> items = (List<String>) value;
            for (String item : items)
            {
                output.write("<value>");
                output.write(escapeXML(item));
                output.write("</value>\n");
            }
        }

        String rt = getRichText();
        if (rt != null)
        {
            output.write("<value-richtext>");
            output.write(escapeXML(rt));
            output.write("</value-richtext>\n");
        }
        List<FDFField> kids = getKids();
        if (kids != null)
        {
            for (FDFField kid : kids)
            {
                kid.writeXML(output);
            }
        }
        output.write("</field>\n");
    }

    /**
     * Convert this standard java object to a COS object.
     *
     * @return The cos object that matches this Java object.
     */
    @Override
    public COSDictionary getCOSObject()
    {
        return field;
    }

    /**
     * This will get the list of kids. This will return a list of FDFField objects. This will return null if the
     * underlying list is null.
     *
     * @return The list of kids.
     */
    public List<FDFField> getKids()
    {
        COSArray kids = field.getCOSArray(COSName.KIDS);
        if (kids != null)
        {
            List<FDFField> actuals = new ArrayList<>();
            for (int i = 0; i < kids.size(); i++)
            {
                actuals.add(new FDFField((COSDictionary) kids.getObject(i)));
            }
            return new COSArrayList<>(actuals, kids);
        }
        return null;
    }

    /**
     * This will set the list of kids.
     *
     * @param kids A list of FDFField objects.
     */
    public final void setKids(List<FDFField> kids)
    {
        field.setItem(COSName.KIDS, new COSArray(kids));
    }

    /**
     * This will get the "T" entry in the field dictionary. A partial field name. Where the fully qualified field name
     * is a concatenation of the parent's fully qualified field name and "." as a separator. For example<br>
     * Address.State<br>
     * Address.City<br>
     *
     * @return The partial field name.
     */<commit_msg>PDFBOX-4892: optimize, as suggested by <NAME><commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.pdmodel.fdf;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSNumber;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
import org.apache.pdfbox.pdmodel.interactive.action.PDAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.util.XMLUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This represents an FDF field that is part of the FDF document.
 *
 * @author <NAME>
 */
public class FDFField implements COSObjectable
{
    private final COSDictionary field;

    /**
     * Default constructor.
     */
    public FDFField()
    {
        field = new COSDictionary();
    }

    /**
     * Constructor.
     *
     * @param f The FDF field.
     */
    public FDFField(COSDictionary f)
    {
        field = f;
    }

    /**
     * This will create an FDF field from an XFDF XML document.
     *
     * @param fieldXML The XML document that contains the XFDF data.
     * @throws IOException If there is an error reading from the dom.
     */
    public FDFField(Element fieldXML) throws IOException
    {
        this();
        this.setPartialFieldName(fieldXML.getAttribute("name"));
        NodeList nodeList = fieldXML.getChildNodes();
        List<FDFField> kids = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++)
        {
            Node node = nodeList.item(i);
            if (node instanceof Element)
            {
                Element child = (Element) node;
                switch (child.getTagName())
                {
                    case "value":
                        setValue(XMLUtil.getNodeValue(child));
                        break;
                    case "value-richtext":
                        setRichText(new COSString(XMLUtil.getNodeValue(child)));
                        break;
                    case "field":
                        kids.add(new FDFField(child));
                        break;
                    default:
                        break;
                }
            }
        }
        if (kids.size() > 0)
        {
            setKids(kids);
        }

    }

    /**
     * This will write this element as an XML document.
     *
     * @param output The stream to write the xml to.
     *
     * @throws IOException If there is an error writing the XML.
     */
    public void writeXML(Writer output) throws IOException
    {
        output.write("<field name=\"");
        output.write(getPartialFieldName());
        output.write("\">\n");

        Object value = getValue();

        if (value instanceof String)
        {
            output.write("<value>");
            output.write(escapeXML((String) value));
            output.write("</value>\n");
        }
        else if (value instanceof List)
        {
            List<String> items = (List<String>) value;
            for (String item : items)
            {
                output.write("<value>");
                output.write(escapeXML(item));
                output.write("</value>\n");
            }
        }

        String rt = getRichText();
        if (rt != null)
        {
            output.write("<value-richtext>");
            output.write(escapeXML(rt));
            output.write("</value-richtext>\n");
        }
        List<FDFField> kids = getKids();
        if (kids != null)
        {
            for (FDFField kid : kids)
            {
                kid.writeXML(output);
            }
        }
        output.write("</field>\n");
    }

    /**
     * Convert this standard java object to a COS object.
     *
     * @return The cos object that matches this Java object.
     */
    @Override
    public COSDictionary getCOSObject()
    {
        return field;
    }

    /**
     * This will get the list of kids. This will return a list of FDFField objects. This will return null if the
     * underlying list is null.
     *
     * @return The list of kids.
     */
    public List<FDFField> getKids()
    {
        COSArray kids = field.getCOSArray(COSName.KIDS);
        if (kids != null)
        {
            List<FDFField> actuals = new ArrayList<>(kids.size());
            for (int i = 0; i < kids.size(); i++)
            {
                actuals.add(new FDFField((COSDictionary) kids.getObject(i)));
            }
            return new COSArrayList<>(actuals, kids);
        }
        return null;
    }

    /**
     * This will set the list of kids.
     *
     * @param kids A list of FDFField objects.
     */
    public final void setKids(List<FDFField> kids)
    {
        field.setItem(COSName.KIDS, new COSArray(kids));
    }

    /**
     * This will get the "T" entry in the field dictionary. A partial field name. Where the fully qualified field name
     * is a concatenation of the parent's fully qualified field name and "." as a separator. For example<br>
     * Address.State<br>
     * Address.City<br>
     *
     * @return The partial field name.
     */
{'fraction_non_alphanumeric': 0.10363905555988467, 'fraction_numerical': 0.0018701784461934076, 'mean_word_length': 1.9626038781163435, 'pattern_counts': {'":': 6, '<': 52, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2142583', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3531, 'n_words': 1045}