5 Replies Latest reply: May 8, 2014 5:21 AM by 4everJang RSS

    How can I find out if a structured doc is valid?

    4everJang Community Member

      Hi all,

       

      I need to establish the validity of a Doc before my script transforms it to XML and applies an XSLT in the process. I have searched the FM12 scripting guide but cannot find any Doc property or method to figure out whether or not the Doc is valid. Am I looking in the wrong place?

       

      Kind regards

       

      Jang

        • 1. Re: How can I find out if a structured doc is valid?
          4everJang Community Member

          Some more digging showed the solution, although I do feel there should be an easier way:

           

          The highest level element of the main flow may show a red plus in the Structure View but this is not reflected in any of its validity properties. I would have expected the ContentIsStrictlyValid property to be False if any of the descendants is invalid, but that does not seem to be the case. The property only looks at the element's own children and the invalidity is not propagated to the top. Still, FM does show a red plus in the Structure View if any descendant element is invalid. It would have been so nice to see this reflected in the ContentIsStrictlyValid property. I do believe that descendants are part of the content, too.

           

          I have now written a small recursive piece of code to determine whether all elements in the flow are valid.

           

          function ValidateDoc ( oDoc )

          {

               var oRoot = oDoc.MainFlowInDoc.HighestLevelElement;

               if ( !oRoot.ObjectValid( ) || !oRoot.ContentIsStrictlyValid || oRoot.InvalidHighestLevel )

                                   return false;

           

               return ValidateChildren( oRoot );

          }

           

          function ValidateChildren ( oParent )

          {

                         var oChild = oParent.FirstChildElement;

                         while ( oChild.ObjectValid( ) )

                         {

                                        if ( !oChild.ContentIsStrictlyValid || !ValidateChildren( oChild ) )

                                                       return false;

                                        oChild = oChild.NextSiblingElement;

                         }

           

                 return true;

          }

          • 2. Re: How can I find out if a structured doc is valid?
            Russ Ward Community Member

            Hi Jang,

             

            This is the only way I've found do to it; that is, step through the entire tree and look at elements individually. It may just be that the granular nature of the API expects us to write routines like this ourselves, if we want there to be an easier way (at least after the first time!).

             

            Russ

            • 3. Re: How can I find out if a structured doc is valid?
              4everJang Community Member

              Hi Russ,

               

              What strikes me is that apparently the FM code already contains this routine - as visible in the Structure View - but it was not deemed important enough to share with scripters via an additional property. Whereas there are 100s of properties that do not seem so useful to most of us. Strange policy.

               

              Ciao

               

              Jang

              • 4. Re: How can I find out if a structured doc is valid?
                ASJonnyBe Community Member

                Jang,

                 

                Many thanks for posting your validation code.  It made my task a lot easier.  I have posted my code below which extends your code to run over a book and components, adds exception handling and looks at validation flags as I needed to also check attributes.

                 

                Jon

                 

                function ValidateBook(inBook)
                {
                    // based on source code provided by 4everJang
                    try
                    {
                        var functionResult = true;
                        var oRoot = inBook.HighestLevelElement;

                        if ( !oRoot.ObjectValid( ) || oRoot.InvalidHighestLevel || TestInvalidFlags(oRoot.ValidationFlags) )
                            functionResult = false;
                        if (functionResult)
                            functionResult = ValidateChildren(oRoot);
                        if (functionResult)
                            functionResult = ValidateComponents(inBook);
                        return functionResult;   
                    }
                    catch (err)
                    {
                        throw new Error ("ValidateBook - Unexpected exception: " + err);
                    }
                }

                function ValidateComponents(inBook)
                {   
                    try
                    {
                        var functionResult = false;
                       
                        var bookComp = inBook.FirstComponentInBook;      
                        while (bookComp.ObjectValid())
                        {
                            // skip unstructured TOC
                            if (bookComp.BookComponentType != Constants.FV_BK_TOC)
                            {
                                var doc = open(bookComp.Name); // implements Open (fileName: string , openParams: PropVals , openReturnParams: PropVals )
                                functionResult = ValidateDoc ( doc );
                                doc.Close (Constants.FF_CLOSE_MODIFIED);
                                if (!functionResult)
                                    break;
                            }
                            bookComp = bookComp.NextComponentInBook;
                        }
                        return functionResult;
                    }
                    catch (err)
                    {
                        throw new Error ("ValidateComponents - Unexpected exception: " + err);
                    }

                }

                function ValidateDoc ( inDoc )
                {
                    // based on source code provided by 4everJang
                    try
                    {
                         var oRoot = inDoc.MainFlowInDoc.HighestLevelElement;
                         if ( !oRoot.ObjectValid( ) || oRoot.InvalidHighestLevel || TestInvalidFlags(oRoot.ValidationFlags) )
                            return false;
                         return ValidateChildren( oRoot );
                    }
                    catch (err)
                    {
                        throw new Error ("ValidateDoc - Unexpected exception: " + err);
                    }
                }

                function ValidateChildren (inParent)
                {
                    // based on source code provided by 4everJang
                    try
                    {
                        var functionResult = true;
                        var oChild = inParent.FirstChildElement;
                        while ( oChild.ObjectValid( ) )
                        {
                              if (  TestInvalidFlags(oChild.ValidationFlags)  || !ValidateChildren( oChild ) )
                              {
                                    functionResult = false;
                                    break;
                               }
                              oChild = oChild.NextSiblingElement;
                        }
                        return functionResult;
                    }
                    catch (err)
                    {
                        throw new Error ("ValidateChildren - Unexpected exception: " + err);
                    }
                }

                function TestInvalidFlags(inFlags)
                {
                   if ( (inFlags & (Constants.FV_ELEM_UNDEFINED |
                                        Constants.FV_ELEM_TYPE_MISMATCH |
                                        Constants.FV_ELEM_EXCLUDED |
                                        Constants.FV_ELEM_INVALID_IN_PARENT |
                                        Constants.FV_ELEM_INVALID_AT_POSITION |
                                        Constants.FV_ELEM_HAS_TEXT_INVALID |
                                        Constants.FV_ELEM_CONTENT_MUST_BE_EMPTY |
                                        Constants.FV_ELEM_MISSING_CONTENT_BEFORE |
                                        Constants.FV_ELEM_MISSING_CONTENT_AT_BEG |
                                        Constants.FV_ELEM_MISSING_CONTENT_AT_END |
                                        Constants.FV_ELEM_NOT_VALID_AS_ROOT |
                                        Constants.FV_ELEM_BOOK_COMP_MISSING |
                                        Constants.FV_ELEM_BOOK_COMP_INVALID |
                                        Constants.FV_ELEM_ATTRVAL_REQUIRED |
                                        Constants.FV_ELEM_ATTRVAL_INVALID ) ) != 0 )
                        return true;
                    else
                        return false;
                }

                • 5. Re: How can I find out if a structured doc is valid?
                  4everJang Community Member

                  Hi Jon,

                   

                  Nice. When I delivered my scripts to the customer she was more or less happy. She got a lot happier when I made the script automatically scroll to the first invalid location. Setting the document's TextSelection to the textrange of the element with invalid children is easy enough.

                   

                  Ciao

                   

                  Jang