﻿<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesigner_V8.0" APIVersion="2.5.6154.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
   <template xmlns="http://www.xfa.org/schema/xfa-template/2.5/">
   <subform>
      <pageSet>
         <pageArea name="snippet">
            <desc>
               <text name="name">Papierformular-Barcode</text>
               <text name="description">Papierformular-Barcode - Fügt einen PDF 417-Strichcode hinzu, der Daten aus Formularfeldern erfasst.</text>
               <text name="standard">1</text>
                    <text name="version">8.0.1291.1.339988.325709</text>
            </desc>
            <field name="Papierformular-Barcode" y="0mm" x="0mm" w="82.55mm" h="44.45mm" access="readOnly">
               <ui>
                        <barcode type="pdf417" moduleWidth="0.338mm" moduleHeight="0.676mm" errorCorrectionLevel="5">
                            <?templateDesigner decodeMethod 2?>
                        </barcode>
               </ui>
               <calculate override="error">
                  <script contentType="application/x-javascript">
//{{Start Generated Content//
/*{{&lt;name&gt;delimCollection&lt;/name&gt;}}*/
var includeFieldNames = true;
var includeLabel = true;
var labelID = "Label";
var is705ViewerRequired = false;
var collection = null;
var delimiter = '\t';
//End Generated Content}}//

// Initialize the barcode content
this.rawValue = " ";

// Encoding data into the barcode

var fieldNames = new Array();
var fieldValues = new Array();

function encode(node)
{
    var barcodeLabel = this.caption.value.text.value;
    if (includeLabel == true &amp;&amp; barcodeLabel.length &gt; 0)
    {
        fieldNames.push(labelID);
        fieldValues.push(barcodeLabel);
    }

    // Create an array of all child nodes in the form
    var entireFormNodes = new Array();
    collectChildNodes(xfa.datasets.data, entireFormNodes);

    // Create an array of all nodes in the collection
    var collectionNodes = new Array();
    var nodes = collection.evaluate();
    for(var i = 0; i &lt; nodes.length; ++i)
    {
        collectionNodes.push(nodes.item(i));
    }

    // If the form has two or more fields sharing the same name, the names of the 
    // parents of these fields, as well as the subscript numbers of the fields and
    // their parents, will be used to differentiate them. However, since we want 
    // to take as little space in the barcode as possible, we only use such extra
    // data in the object names only when necessary.
    resolveDuplicates(collectionNodes, entireFormNodes, fieldNames);
        
    for(var j = 0; j &lt; nodes.length; ++j)
    {
        // Go through the collection and accumulate the value of each form object
        accumulateFieldValues(nodes.item(j));
    }

    var encodedContent = new String();

    var values = fieldValues.join(delimiter);
    values += delimiter;

    if (includeFieldNames == true)
    { 
        if(delimiter != '\n')
        {
            var fields = fieldNames.join(delimiter);
            fields += delimiter;

            encodedContent = fields + "\n" + values;
        }
    }
    else
    {
        encodedContent = values;
    }

    return encodedContent;
}


// If there are two or more nodes sharing the same name, differentiate those
// nodes by using subscripts and/or parent name(s).
function resolveDuplicates(encodingNodes, entireFormNodes, fieldNameArray)
{
    for(var i = 0; i &lt; encodingNodes.length; ++i)
    {
        fieldNameArray.push(makeUniqueName(encodingNodes[i], 
            entireFormNodes, encodingNodes[i].name));
    }

    // Remove subscript 0's, if there's no other field sharing the same name at the
    // same level
    compact(fieldNameArray);
}


// Given a node, look for duplicates (in the whole form), and come up with a 
// name that differentiates the current node from the rest, using a combination
// of subscript numbers and parent name(s).
function makeUniqueName(node, formNodeArray, nodeName)
{
    for(var i = 0; i &lt; formNodeArray.length; ++i)
    {
        var compNode = formNodeArray[i];

        var nodeSOM = node.somExpression;
        var compNodeSOM = compNode.somExpression;

        if((nodeSOM != compNodeSOM) &amp;&amp; (nodeName == compNode.name))
{
            if(nodeSOM.substring(0, nodeSOM.lastIndexOf(".")) == 
                compNodeSOM.substring(0, compNodeSOM.lastIndexOf(".")))
    {
                var diffLevelNodeFound = false;
                for(var ii = i; !diffLevelNodeFound  &amp;&amp; ii &lt; formNodeArray.length; ++ii)
                {
                    compNode = formNodeArray[ii];
                    compNodeSOM = compNode.somExpression;

                    if((nodeSOM != compNodeSOM) &amp;&amp; (nodeName == compNode.name) &amp;&amp;
                        (nodeSOM.substring(0, nodeSOM.lastIndexOf(".")) != 
                        compNodeSOM.substring(0, compNodeSOM.lastIndexOf("."))))
                    {
                        diffLevelNodeFound = true;
                    }
                }

                if(!diffLevelNodeFound )
        {
                    // The two nodes are at the same level. There is no need to append the 
                    // current node's parent names to differentiate. Instead, just use
                    // the suffix
                    nodeName += "[" + node.index + "]";
        }
    }
    else
    {
                if(node.parent != null)
        {
                    var parentArray = new Array();
                    for(var j = 0; j &lt; formNodeArray.length; ++j)
                    {
                        var parentNode = formNodeArray[j].parent;
                        if(parentNode != null)
                        {
                            parentArray.push(parentNode);
                        }
                    }

                    nodeName = makeUniqueName(node.parent, parentArray, node.parent.name).concat(
                        ".", nodeName, "[", node.index, "]");
                }
            }
        }
    }

    return nodeName;
}


// Remove subscript 0's, if there's no other field sharing the same name at the
// same level
function compact(nameArray)
{
    for(var i = 0; i &lt; nameArray.length; ++i)
    {
        var fieldName = nameArray[i];
        if(fieldName.substring(fieldName.lastIndexOf("[") + 1, fieldName.lastIndexOf("]")) == "0")
        {
            var fieldNameNoSubscript = fieldName.substring(0, fieldName.lastIndexOf("["));
            var isFound = false;

            for(var j = 0; !isFound &amp;&amp; j &lt; nameArray.length; ++j)
            {
                if(nameArray[j] != nameArray[i])
                {
                    var comparedFieldName = nameArray[j];

                    if(fieldNameNoSubscript ==
                        comparedFieldName.substring(0, comparedFieldName.lastIndexOf("[")))
                    {
                        isFound = true;
                    }
                }
            }

            if(!isFound)
            {
                nameArray.splice(i, 1, fieldNameNoSubscript);
            }
        }
    }
        }


// Add object values into a local container
function accumulateFieldValues(node)
        {
    if(node.value != null)
    {
        fieldValues.push(node.value);
        }
        else
        {
        fieldValues.push("");
    }
}


// Dependency function. Note that we'll pass in just the resolved form model node list,
// not the entire form model (xfa.form)
function depends(node)
{
    if(node.className != "variables")
    {
        var stack = new Array();

        var currentNode = node;
        stack.push(currentNode);

        while (stack.length &gt; 0)
	{
            currentNode = stack.pop();
            if(currentNode == null) continue;

            for(var i = 0; i &lt; currentNode.nodes.length; ++i)
            {
                stack.push(currentNode.nodes.item(i));
            }
        }
    }
}


// Convert an XFA node list into a JavaScript node array
function collectChildNodes(node, childNodeArray)
{
    if(node.className == "dataGroup")
        {
        var itemCount = node.nodes.length;
        for (var i = 0; i &lt; itemCount; ++i)
        {
            collectChildNodes(node.nodes.item(i), childNodeArray);
	}
}
    else
    {
        childNodeArray.push(node);
}
}


// Create a collection of the selected form objects, but in the form model context
var formModelNodeList = new Array();
var dataNodes = collection.evaluate();

for(var i = 0; i &lt; dataNodes.length; ++i)
{
    formModelNodeList.push(xfa.form.formNodes(dataNodes.item(i)));
}

// Now, force a dependency between the barcode and its form model nodes in its
// collection
while(formModelNodeList.length &gt; 0)
{
    var nodeList = formModelNodeList.pop();
    for(var i = 0; i &lt; nodeList.length; ++i)
    {
        depends(nodeList.item(i));
    }
}

if(collection != null)
{
// Encode the data node's content in tab-delimited format
    this.rawValue = encode(collection);
}

// Final sanity check, located here not for efficiency but for consistency and correctness
if (is705ViewerRequired &amp;&amp; xfa.host.version &lt; 7.05)
{
    this.rawValue = " ";
}
                  </script>
               </calculate>
               <bind match="none"/>
                    <caption placement="bottom" reserve="0mm">
                        <font typeface="Myriad Pro" size="6pt"/>
                        <para vAlign="middle" hAlign="center"/>
	  	<value>
	                  <text>00000000-0000-0000-0000-000000000000</text>
		</value>
	  </caption>
            </field>
         </pageArea>
      </pageSet>
   </subform>
</template>
</xdp:xdp>
