Example #1
0
        public SplittingResult SplitOnPageBreak()
        {
            _result = new SplittingResult(XmlFile, OutputDirectory);

            bool splittingStarted = (StartingElement == null);

            ElementInfo startElement = null;

            ElementInfos elementStack = new ElementInfos();
            FileInfo     xmlFileInfo  = new FileInfo(XmlFile);



            string newFileFormat = xmlFileInfo.Name.Substring(0, xmlFileInfo.Name.Length - xmlFileInfo.Extension.Length) + NumberedXmlPattern;


            _outputManager = new OutputManager();
            _outputManager.OutputDirectory = OutputDirectory;
            _outputManager.FileNameFormat  = newFileFormat;

            string divId       = StartingElement ?? "body"; //set default value because xml:id^="." is invalid
            int    paragraphId = 0;

            try
            {
                using (XmlReader reader = XmlReader.Create(XmlFile))
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            ElementInfo element = ElementInfo.GetElementInfo(reader);

                            if (element.Name == TeiElementName)
                            {
                                string documentXmlId     = element.Attributes.GetAttributeByLocalName(String.Empty, NAttributeName).Value;
                                string documentVersionId = element.Attributes.GetAttributeByLocalName(String.Empty, ChangeAttributeName).Value;
                                _sourceDocumentInfo = new SourceDocumentInfo(documentXmlId, documentVersionId);
                            }

                            if (!splittingStarted)
                            {
                                if (!ShouldSplittingStart(element, startElement))
                                {
                                    continue;
                                }
                            }
                            if (!splittingStarted)
                            {
                                startElement      = element.Clone();
                                splittingStarted  = true;
                                _currentSplitInfo = new PageBreakSplitInfo(_sourceDocumentInfo);
                            }

                            if (element.Name == PbElementName || element.Name == TeiPrefix + PbElementName)
                            {
                                bool editionPagebreak = element.Attributes.AttributeExists(String.Empty, EdAttrubuteName);
                                //TODO načíst do stacku první element pb, i když je z jiného stránkování
                                if (!editionPagebreak)
                                {
                                    if (_currentSplitInfo != null && _currentSplitInfo.Number == null)
                                    {
                                        _currentSplitInfo.Id     = element.Attributes.GetAttributeByLocalName(XmlNamespacePrefix, IdAttributeName).Value;
                                        _currentSplitInfo.Number = element.Attributes.GetAttributeByLocalName(String.Empty, NAttributeName).Value;
                                    }

                                    ElementInfos tempQueue = null;
                                    if (_outputManager.CurrentChunk > 0)
                                    {
                                        tempQueue = CloseCurrentSplit(elementStack);
                                    }
                                    StartNewSplit(elementStack, tempQueue);
                                    _currentSplitInfo.Number = element.Attributes.GetAttributeByLocalName(String.Empty, NAttributeName).Value;
                                    _currentSplitInfo.Id     = element.Attributes.GetAttributeByLocalName(XmlNamespacePrefix, IdAttributeName).Value;

                                    Transformace.SerializeNode(reader, _currentWriter);
                                }
                                //goto Begin;
                            }
                            else      //(reader.Name == "pb")
                            {
                                if (_divElementNames.Contains(element.Name))
                                {
                                    foreach (AttributeInfo attribute in element.Attributes)
                                    {
                                        if (attribute.LocalName == IdAttributeName)
                                        {
                                            divId = attribute.Value;
                                        }
                                    }
                                }
                                if (_blockElementNames.Contains(element.Name))
                                {
                                    if (!element.Attributes.Exists(a => a.Prefix == XmlNamespacePrefix && a.LocalName == IdAttributeName))
                                    {
                                        string id = divId + "." + element.Name + ++paragraphId;
                                        element.Attributes.Add(new AttributeInfo(XmlNamespacePrefix, IdAttributeName, XmlNamespace, id));
                                    }
                                }
                                if (_currentWriter != null)
                                {
                                    if (!element.IsEmpty)
                                    {
                                        elementStack.Push(element);
                                    }
                                    //pokud je element prázdný, při jeho přečtení se XmlReader přesune na další prvek
                                    WriteElementInfo(element, _currentWriter);
                                }
                            }
                            break;

                        case XmlNodeType.EndElement:
                            ElementInfo endElementInfo = ElementInfo.GetElementInfo(reader);

                            if (!splittingStarted || startElement == null)
                            {
                                continue;
                            }

                            if (_divElementNames.Contains(reader.Name))
                            {
                                divId       = null;
                                paragraphId = 0;
                            }

                            if (ShouldSplittingEnd(endElementInfo, startElement))
                            {
                                CloseCurrentSplit(elementStack);
                                _result.IsSplitted = true;
                                return(_result);
                            }

                            if (elementStack.Count > 0)
                            {
                                ElementInfo elementPeak = elementStack.Peek();
                                if (elementPeak.Name != endElementInfo.Name)
                                {
                                    _result.Errors = String.Format(ErrorInfoFormat, elementPeak.Name, endElementInfo.Name);
                                    //Console.WriteLine("Chyba {0} × {1} (element × reader)", elementPeak.Name, name);
                                }
                                else
                                {
                                    Transformace.SerializeNode(reader, _currentWriter);
                                    elementStack.Pop();
                                }
                            }
                            break;

                        default:
                            if (splittingStarted && _currentWriter != null)
                            {
                                Transformace.SerializeNode(reader, _currentWriter);
                            }
                            break;
                        }
                    }
                }
                _result.IsSplitted = true;
            }
            catch (Exception exception)
            {
                _result.Errors = exception.Message;
            }
            finally
            {
                if (_currentWriter != null)
                {
                    _currentWriter.Close();
                }
            }

            return(_result);
        }
Example #2
0
        public void SplitOnStartingElement()
        {
            bool splittingStarted = (StartingElement == null);

            ElementInfos elementStack = new ElementInfos();
            FileInfo     xmlFileInfo  = new FileInfo(XmlFile);


            string newFileFormat = xmlFileInfo.Name.Substring(0, xmlFileInfo.Name.Length - xmlFileInfo.Extension.Length) + NumberedXmlPattern;


            _outputManager = new OutputManager();
            _outputManager.OutputDirectory = OutputDirectory;
            _outputManager.FileNameFormat  = newFileFormat;



            using (XmlReader reader = XmlReader.Create(XmlFile))
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        bool isEmpty = reader.IsEmptyElement;

                        if (!splittingStarted)
                        {
                            if (reader.Name == StartingElement)
                            {
                                splittingStarted = true;
                                _currentWriter   = _outputManager.GetXmlWriter();
                                _currentWriter.WriteStartDocument();
                                _currentWriter.WriteStartElement(FragmentElementlName, TeiNamespace);
                            }
                        }

                        if (!splittingStarted)
                        {
                            continue;
                        }

                        ElementInfo element = ElementInfo.GetElementInfo(reader); //GetElementInfo(reader);
                        elementStack.Push(element);
                        WriteElementInfo(element, _currentWriter);

                        if (!isEmpty)
                        {
                            continue;
                        }
                        elementStack.Pop();
                    }

                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (!splittingStarted)
                        {
                            continue;
                        }
                        string      name        = reader.Name;
                        ElementInfo elementPeak = elementStack.Peek();
                        if (elementPeak.Name != name)
                        {
                            Console.WriteLine("Chyba element × reader ({0} × {1})", elementPeak.Name, name);
                        }
                        else
                        {
                            Transformace.SerializeNode(reader, _currentWriter);
                            elementStack.Pop();
                        }
                        if (name == StartingElement)
                        {
                            elementStack.CloneReverse();

                            while (elementStack.Count > 0)
                            {
                                _currentWriter.WriteEndElement();
                                elementStack.Pop();
                            }
                            _currentWriter.WriteFullEndElement();
                            _currentWriter.Close();

                            splittingStarted = false;
                            //currentWriter = outputManager.GetXmlWriter();
                        }
                    }
                    else
                    {
                        if (splittingStarted)
                        {
                            Transformace.SerializeNode(reader, _currentWriter);
                        }
                    }
                }
            }
        }