Ejemplo n.º 1
0
 public void PropertyAddded_EventArgsAndSenderCorrect()
 {
     urakawa.property.xml.XmlProperty newXmlProp = mPresentation.PropertyFactory.CreateXmlProperty();
     mRootNode.AddProperty(newXmlProp);
     Assert.AreSame(
         newXmlProp, mLatestPropertyAddedEventArgs.m_AddedObject,
         "The PropertyAddedEventArgs.AddedProperty must be the Property instance that was added");
     Assert.AreSame(
         mRootNode, mLatestPropertyAddedEventArgs.SourceObject,
         "The PropertyAddedEventArgs.SourceTreeNode must be the TreeNode to which the property was added");
     Assert.AreSame(
         mRootNode, mLatestPropertyAddedSender,
         "The sender of the PropertyAdded event must be the the TreeNode to which the property was added");
 }
Ejemplo n.º 2
0
        public void PropertyRemoved_EventOccursAndBubble()
        {
            int beforeCount;
            int changedBeforeCount;

            urakawa.property.xml.XmlProperty newXmlProp = mPresentation.PropertyFactory.CreateXmlProperty();
            mRootNode.AddProperty(newXmlProp);
            beforeCount        = mPropertyRemovedEventCount;
            changedBeforeCount = mChangedEventCount;
            mRootNode.RemoveProperty(newXmlProp);
            AssertPropertyRemovedEventOccured(beforeCount, changedBeforeCount, 1);
            urakawa.property.channel.ChannelsProperty newChProp = mPresentation.PropertyFactory.CreateChannelsProperty();
            mRootNode.AddProperty(newXmlProp);
            mRootNode.AddProperty(newChProp);
            beforeCount        = mPropertyRemovedEventCount;
            changedBeforeCount = mChangedEventCount;
        }
Ejemplo n.º 3
0
 public void PropertyRemoved_EventArgsAndSenderCorrect()
 {
     urakawa.property.xml.XmlProperty newXmlProp = mPresentation.PropertyFactory.CreateXmlProperty();
     mRootNode.AddProperty(newXmlProp);
     newXmlProp.SetQName("dtbook", "");
     urakawa.property.channel.ChannelsProperty newChProp = mPresentation.PropertyFactory.CreateChannelsProperty();
     mRootNode.AddProperty(newChProp);
     mRootNode.RemoveProperty(newXmlProp);
     Assert.AreSame(
         newXmlProp, mLatestPropertyRemovedEventArgs.m_RemovedObject,
         "The PropertyRemovedEventArgs.RemovedProperty must be the Property that was removed");
     Assert.AreSame(
         mRootNode, mLatestPropertyRemovedEventArgs.SourceObject,
         "The PropertyRemovedEventArgs.SourceTreeNode must be the TreeNode from which the Property was removed");
     Assert.AreSame(
         mRootNode, mLatestPropertyRemovedSender,
         "The sender of the PropertyRemoved event must be the TreeNode from which the Property was removed");
 }
Ejemplo n.º 4
0
        public void Changed_BubblesfromProperties()
        {
            urakawa.property.xml.XmlProperty xmlProp = mPresentation.PropertyFactory.CreateXmlProperty();
            mRootNode.AddProperty(xmlProp);
            events.DataModelChangedEventArgs propChangedEventArgs = null;
            object propChangedSender = null;
            EventHandler <urakawa.events.DataModelChangedEventArgs> handler =
                new EventHandler <urakawa.events.DataModelChangedEventArgs>(
                    delegate(object sender, events.DataModelChangedEventArgs e)
            {
                propChangedEventArgs = e;
                propChangedSender    = sender;
            });

            xmlProp.Changed += handler;
            try
            {
                int beforeCount = mChangedEventCount;
                xmlProp.SetQName("dtbook", "");
                Assert.IsNotNull(propChangedEventArgs, "The changed event of the property does not seem to have occured");
                Assert.AreSame(
                    xmlProp, propChangedSender,
                    "The sender of the changed event on the property must be the property itself");
                Assert.AreEqual(
                    beforeCount + 1, mChangedEventCount,
                    "The mChangedEventCount did not increase by one, indicating that the changed event on the owning TreeNode "
                    + "did not occur as a result of the changed event on the owned Property");
                Assert.AreSame(
                    propChangedEventArgs, mLatestChangedEventArgs,
                    "The event args of the owning TreeNode changed event was not the same instance as thoose of the owned Property changed evnet");
                Assert.AreSame(
                    mRootNode, mLatestChangedSender,
                    "The sender of the owning TreeNode changed event must be the owning TreeNode node itself");
            }
            finally
            {
                xmlProp.Changed -= handler;
            }
        }
Ejemplo n.º 5
0
        public void Copy_ValueEqualsAfter()
        {
            urakawa.property.xml.XmlProperty newXmlProp = mPresentation.PropertyFactory.CreateXmlProperty();
            newXmlProp.SetQName("p", "");
            mRootNode.AddProperty(newXmlProp);
            mRootNode.AddProperty(mPresentation.PropertyFactory.CreateChannelsProperty());
            TreeNode mRootCopy = mRootNode.Copy(true);
            bool     equal     = mRootNode.ValueEquals(mRootCopy);

            Assert.IsTrue(equal, "The copy is not the same as the original");
            Assert.AreNotSame(mRootNode, mRootCopy, "The copy is just a reference of the original itself");
            foreach (Type propType in mRootCopy.UsedPropertyTypes)
            {
                Assert.AreNotEqual(
                    mRootNode.GetProperty(propType), mRootCopy.GetProperty(propType),
                    "Property of copy is just a reference to the property of the original");
            }
            for (int i = 0; i < mRootCopy.Children.Count; i++)
            {
                Assert.AreNotEqual(
                    mRootNode.Children.Get(i), mRootCopy.Children.Get(i),
                    "Child of copy is just a reference of the child of the original");
            }
        }
Ejemplo n.º 6
0
        // to do regenerate ids
        private void CreateDTBookDocument()
        {
            // check if there is preserved internal DTD
            string [] dtbFilesList   = Directory.GetFiles(m_Presentation.DataProviderManager.DataFileDirectoryFullPath, "DTBookLocalDTD.dtd", SearchOption.AllDirectories);
            string    strInternalDTD = null;

            if (dtbFilesList.Length > 0)
            {
                strInternalDTD = File.ReadAllText(dtbFilesList[0]);
                if (strInternalDTD.Trim() == "")
                {
                    strInternalDTD = null;
                }
            }

            XmlDocument DTBookDocument = CreateStub_DTBDocument(strInternalDTD);

            m_ListOfLevels = new List <urakawa.core.TreeNode>();
            Dictionary <string, string> old_New_IDMap             = new Dictionary <string, string> ();
            List <XmlAttribute>         referencingAttributesList = new List <XmlAttribute> ();

            m_FilesList_Image = new List <string>();

            // add metadata
            XmlNode headNode = getFirstChildElementsWithName(DTBookDocument, true, "head", null); //DTBookDocument.GetElementsByTagName("head")[0]

            Metadata mdId = AddMetadata_DtbUid(false, DTBookDocument, headNode);

            AddMetadata_Generator(DTBookDocument, headNode);

            // todo: filter-out unecessary metadata for DTBOOK (e.g. dtb:multimediatype)
            foreach (urakawa.metadata.Metadata m in m_Presentation.Metadatas.ContentsAs_YieldEnumerable)
            {
                if (mdId == m)
                {
                    continue;
                }

                XmlNode metaNode = DTBookDocument.CreateElement(null, "meta", headNode.NamespaceURI);
                headNode.AppendChild(metaNode);

                string name = m.NameContentAttribute.Name;

                if (name.Contains(":"))
                {
                    // split the metadata name and make first alphabet upper, required for daisy 3.0
                    string splittedName = name.Split(':')[1];
                    splittedName = splittedName.Substring(0, 1).ToUpper() + splittedName.Remove(0, 1);

                    name = name.Split(':')[0] + ":" + splittedName;
                }

                CommonFunctions.CreateAppendXmlAttribute(DTBookDocument, metaNode, "name", name);
                CommonFunctions.CreateAppendXmlAttribute(DTBookDocument, metaNode, "content", m.NameContentAttribute.Value);

                // add metadata optional attributes if any
                foreach (urakawa.metadata.MetadataAttribute ma in m.OtherAttributes.ContentsAs_YieldEnumerable)
                {
                    if (ma.Name == "id")
                    {
                        continue;
                    }

                    CommonFunctions.CreateAppendXmlAttribute(DTBookDocument, metaNode, ma.Name, ma.Value);
                }
            }

            // add elements to book body
            m_TreeNode_XmlNodeMap = new Dictionary <urakawa.core.TreeNode, XmlNode>();


            urakawa.core.TreeNode rNode = m_Presentation.RootNode;
            XmlNode bookNode            = getFirstChildElementsWithName(DTBookDocument, true, "book", null); //DTBookDocument.GetElementsByTagName("book")[0];

            m_ListOfLevels.Add(m_Presentation.RootNode);

            m_TreeNode_XmlNodeMap.Add(rNode, bookNode);
            XmlNode currentXmlNode = null;

            rNode.AcceptDepthFirst(
                delegate(urakawa.core.TreeNode n)
            {
                // add to list of levels if xml property has level string
                //QualifiedName qName = n.GetXmlElementQName();
                //if (qName != null &&
                //    (qName.LocalName.StartsWith("level") || qName.LocalName == "doctitle" || qName.LocalName == "docauthor"))
                //{
                //    m_ListOfLevels.Add(n);
                //}

                if (doesTreeNodeTriggerNewSmil(n))
                {
                    m_ListOfLevels.Add(n);
                }


                urakawa.property.xml.XmlProperty xmlProp = n.GetProperty <urakawa.property.xml.XmlProperty>();

                if (xmlProp != null && xmlProp.LocalName == "book")
                {
                    return(true);
                }

                if (xmlProp == null)
                {
                    string txtx = n.GetTextMedia() != null ? n.GetTextMedia().Text : null;
                    if (txtx != null)
                    {
                        XmlNode textNode = DTBookDocument.CreateTextNode(txtx);


                        ExternalAudioMedia extAudio = GetExternalAudioMedia(n);

                        if (extAudio == null)
                        {
                            m_TreeNode_XmlNodeMap[n.Parent].AppendChild(textNode);
                            m_TreeNode_XmlNodeMap.Add(n, textNode);
                        }
                        else
                        {
                            Debug.Fail("TreeNode without XmlProperty but with TextMedia cannot have Audio attached to it ! (reason: at authoring time, an XmlProperty should have been added when audio was recorded for the pure-text TreeNode) => " + txtx);

                            //XmlNode textParent = DTBookDocument.CreateElement(null, "sent", bookNode.NamespaceURI);
                            //textParent.AppendChild(textNode);

                            //m_TreeNode_XmlNodeMap[n.Parent].AppendChild(textParent);
                            //m_TreeNode_XmlNodeMap.Add(n, textParent);
                        }
                    }

                    return(true);
                }

                // create sml node in dtbook document

                // code removed because XmlProperty stores proper namespaces, useful for inline MathML, SVG, whatever...
                //string name = xmlProp.LocalName;
                //string prefix = name.Contains(":") ? name.Split(':')[0] : null;
                //string elementName = name.Contains(":") ? name.Split(':')[1] : name;
                //currentXmlNode = DTBookDocument.CreateElement(prefix, elementName, bookNode.NamespaceURI);

                currentXmlNode = DTBookDocument.CreateElement(null, xmlProp.LocalName, (string.IsNullOrEmpty(xmlProp.NamespaceUri) ? bookNode.NamespaceURI : xmlProp.NamespaceUri));

                // add attributes
                if (xmlProp.Attributes != null && xmlProp.Attributes.Count > 0)
                {
                    for (int i = 0; i < xmlProp.Attributes.Count; i++)
                    {
                        //todo: check ID attribute, normalize with fresh new list of IDs
                        // (warning: be careful maintaining ID REFS, such as idref attributes for annotation/annoref and prodnote/noteref
                        // (be careful because idref contain URIs with hash character),
                        // and also the special imgref and headers attributes which contain space-separated list of IDs, not URIs)

                        if (xmlProp.Attributes[i].LocalName == "id")
                        {
                            string id_New = GetNextID(ID_DTBPrefix);
                            CommonFunctions.CreateAppendXmlAttribute(DTBookDocument,
                                                                     currentXmlNode,
                                                                     "id", id_New);

                            if (!old_New_IDMap.ContainsKey(xmlProp.Attributes[i].Value))
                            {
                                old_New_IDMap.Add(xmlProp.Attributes[i].Value, id_New);
                            }
                            else
                            {
                                System.Diagnostics.Debug.Fail("Duplicate ID found in original DTBook document", "Original DTBook document has duplicate ID: " + xmlProp.Attributes[i].Value);
                            }
                        }
                        else
                        {
                            CommonFunctions.CreateAppendXmlAttribute(DTBookDocument,
                                                                     currentXmlNode,
                                                                     xmlProp.Attributes[i].LocalName, xmlProp.Attributes[i].Value);
                        }
                    }     // for loop ends
                }         // attribute nodes created

                // add id attribute in case it do not exists and it is required
                if (currentXmlNode.Attributes.GetNamedItem("id") == null && IsIDRequired(currentXmlNode.LocalName))
                {
                    CommonFunctions.CreateAppendXmlAttribute(DTBookDocument, currentXmlNode, "id", GetNextID(ID_DTBPrefix));
                }
                // add text from text property

                string txt = n.GetTextMedia() != null ? n.GetTextMedia().Text : null;
                if (txt != null)
                {
                    XmlNode textNode = DTBookDocument.CreateTextNode(txt);
                    currentXmlNode.AppendChild(textNode);

                    Debug.Assert(n.Children.Count == 0);
                }

                // add current node to its parent
                m_TreeNode_XmlNodeMap[n.Parent].AppendChild(currentXmlNode);

                // add nodes to dictionary
                m_TreeNode_XmlNodeMap.Add(n, currentXmlNode);

                // if current xmlnode is referencing node, add its referencing attribute to referencingAttributesList
                AddReferencingNodeToReferencedAttributesList(currentXmlNode, referencingAttributesList);

                // if QName is img and img src is on disk, copy it to output dir
                if (currentXmlNode.LocalName == "img")
                {
                    XmlAttribute imgSrcAttribute = (XmlAttribute)currentXmlNode.Attributes.GetNamedItem("src");
                    if (imgSrcAttribute != null &&
                        (imgSrcAttribute.Value.StartsWith(m_Presentation.DataProviderManager.DataFileDirectory)))
                    {
                        string imgFileName = Path.GetFileName(imgSrcAttribute.Value);
                        string sourcePath  = Path.Combine(m_Presentation.DataProviderManager.DataFileDirectoryFullPath,
                                                          Uri.UnescapeDataString(imgFileName));
                        string destPath = Path.Combine(m_OutputDirectory, Uri.UnescapeDataString(imgFileName));
                        if (File.Exists(sourcePath))
                        {
                            if (!File.Exists(destPath))
                            {
                                File.Copy(sourcePath, destPath);
                            }
                            imgSrcAttribute.Value = imgFileName;

                            if (!m_FilesList_Image.Contains(imgFileName))
                            {
                                m_FilesList_Image.Add(imgFileName);
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("source image not found", sourcePath);
                        }
                    }
                }

                return(true);
            },
                delegate(urakawa.core.TreeNode n) { });

            // set references to new ids
            foreach (XmlAttribute attr in referencingAttributesList)
            {
                string strIDToFind = attr.Value;
                if (strIDToFind.Contains("#"))
                {
                    strIDToFind = strIDToFind.Split('#')[1];
                }
                if (old_New_IDMap.ContainsKey(strIDToFind))
                {
                    string id_New = old_New_IDMap[strIDToFind];

                    attr.Value = "#" + id_New;
                }
            }

            m_DTBDocument = DTBookDocument;
            //CommonFunctions.WriteXmlDocumentToFile(DTBookDocument,
            //  Path.Combine(m_OutputDirectory, m_Filename_Content));
        }