Exemplo n.º 1
0
        } // ParsePblPath

        // ******************************************************************************
        /// <summary>
        /// Checks if the node addressed by the PBL-Path exists in a NewsML-G2 Package Item
        /// </summary>
        /// <param name="pkgItem">The Package Item</param>
        /// <param name="pblPath">The PBL-Path</param>
        /// <returns>True if the node exists</returns>
        public static bool PblNodeExists(PackageItemPwrXml pkgItem, string pblPath)
        {
            if (string.IsNullOrEmpty(pblPath))
            {
                return(false);
            }
            var pblPathParsed = ParsePblPath(pblPath);

            if ((pblPathParsed.EndNodeType == PblNodeType.ParseError) ||
                (pblPathParsed.EndNodeType == PblNodeType.Undefined))
            {
                return(false);
            }
            // first check for the groups by their roles
            var groupFound    = true;
            var lastGroupRole = string.Empty;

            foreach (var groupRole in pblPathParsed.GroupRoles)
            {
                lastGroupRole = groupRole;
                if (!pkgItem.ExistsRelXN("/nar:groupSet/nar:group[@role='" + groupRole + "']"))
                {
                    groupFound = false;
                }
                if (!groupFound)
                {
                    break;
                }
            }
            if (!groupFound)
            {
                return(false);
            }

            // second: check for itemRef and contentRef - if included into the PBL-Path
            string[] refIdParts = { "" };
            if (!string.IsNullOrEmpty(pblPathParsed.RefId))
            {
                refIdParts = pblPathParsed.RefId.Split('='); // part[0] = attribute name, [1] = value
            }
            if (pblPathParsed.EndNodeType == PblNodeType.ItemRef)
            {
                if (!pkgItem.ExistsRelXN("/nar:groupSet/nar:group[@role='" + lastGroupRole
                                         + "']/nar:itemRef[@" + refIdParts[0] + "='" + refIdParts[0] + "']"))
                {
                    return(false);
                }
            }
            if (pblPathParsed.EndNodeType == PblNodeType.ContentRef)
            {
                if (!pkgItem.ExistsRelXN("/nar:groupSet/nar:group[@role='" + lastGroupRole
                                         + "']/nar:conceptRef[@" + refIdParts[0] + "='" + refIdParts[0] + "']"))
                {
                    return(false);
                }
            }

            return(true);
        } // PblNodeExists
Exemplo n.º 2
0
        // ******************************************************************************
        /// <summary>
        /// Adds this instance of GroupStructProp as group element and its children
        /// to a NewsML-G2 Package Item. The location in the group structure is
        /// defined by the PblPath property if this instance.
        /// </summary>
        /// <param name="pkgItem">The Package Item to which the group should be added</param>
        /// <returns>A PropProcStatus</returns>
        public virtual PropProcStatus AddGroupToPackage(PackageItemPwrXml pkgItem)
        {
            if (string.IsNullOrEmpty(PblPath))
            {
                return(PropProcStatus.ErrPkgNoPblPath);
            }
            if (string.IsNullOrEmpty(CoreGroup.role))
            {
                return(PropProcStatus.ErrPkgNoGroupRole);
            }

            var pblPathParsed = PblTools.ParsePblPath(PblPath);

            if ((pblPathParsed.EndNodeType == PblNodeType.ParseError) ||
                (pblPathParsed.EndNodeType == PblNodeType.Undefined))
            {
                return(PropProcStatus.ErrPkgNoPblPath);
            }

            string pblPath4Testing = string.Empty;

            // check if the hierarchical parent group(s) exist, skipped for the root group
            for (var idx = 0; idx < pblPathParsed.GroupRoles.Count - 1; idx++)
            {
                pblPath4Testing += "/" + pblPathParsed.GroupRoles[idx];
                if (!PblTools.PblNodeExists(pkgItem, pblPath4Testing))
                {
                    return(PropProcStatus.ErrPkgNoPblParentNode);
                }
            }

            pkgItem.AddGroupSet();                  // try to add it, skips action if it already exists

            if (string.IsNullOrEmpty(CoreGroup.id)) // take care that the group@id is not empty.
            {
                CoreGroup.id = pkgItem.NewIdSequence.ToString();
            }
            // adds the new group element
            pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet", "", CoreGroup);

            ReadFromItemResultEnum readResult;

            if (pblPathParsed.GroupRoles.Count == 1) // this is the root group!
            {
                // apply the id of the root group to the root attribute of groupSet
                XmlElement groupSetXe = null;
                pkgItem.GetElemAsXE("/nar:packageItem/nar:groupSet", out groupSetXe, out readResult);
                if (readResult == ReadFromItemResultEnum.ok)
                {
                    groupSetXe.SetAttribute("root", CoreGroup.id);
                }
                // return PropProcStatus.ok; // exits here
            }
            else // ++2014-11-27
            {
                // for groups below the root in the hierarchy: add a groupRef element to the parent group
                var parentRole  = pblPathParsed.GroupRoles[pblPathParsed.GroupRoles.Count - 2];
                var newGroupRef = new GroupRef();
                newGroupRef.idref = CoreGroup.id;
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@role='" + parentRole + "']", "",
                                               newGroupRef);
            }

            // add all the child elements of the new group
            foreach (var itemRef in ItemRefs)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               itemRef);
            }
            foreach (var conceptRef in ConceptRefs)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               conceptRef);
            }
            foreach (var title in Titles)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               title);
            }
            foreach (var signal in Signals)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               signal);
            }
            foreach (var edNote in EdNotes)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               edNote);
            }
            foreach (var groupExtProperty in GroupExtProperties)
            {
                pkgItem.AddNarPropertyToParent("/nar:packageItem/nar:groupSet/nar:group[@id='" + CoreGroup.id + "']", "",
                                               groupExtProperty);
            }

            return(PropProcStatus.ok);
        } // AddGroupToPackage