Exemplo n.º 1
0
        } // fun

        private static void determineEncoding(string fileName, AuthorData aData)
        {
            using (StreamReader sr = new StreamReader(fileName)) {
                Encoding enc = sr.CurrentEncoding;
                //Console.Out.WriteLine( "got encoding from file: " + enc );
                //if ( enc != Encoding.UTF8 ) // utf8 is invalid encoding
                _encoding = enc;
                XmlDocument doc = new XmlDocument();
                doc.Load(sr);
                parseChild(null, doc.ChildNodes, 0, null, aData);
            } // end block

            // validate if name and last name are correct
            if (_encoding == Encoding.UTF8)
            {
                if ((aData.getName() != null && isCorrectEncoding(aData.getName()) == false) ||
                    (aData.getLastName() != null && isCorrectEncoding(aData.getLastName()) == false))
                {
                    _encoding = Encoding.GetEncoding(1251);
                    aData.clear();
                } // if
            }     // if
        }         // fun
Exemplo n.º 2
0
        }         // fun

        private static void processLeafElement(string parentName,
                                               XmlNode node, int level,
                                               BinaryWriter outBuf, AuthorData aData)
        {
            string k = node.Name;
            string v = node.ChildNodes.Item(0).Value;

            if (v == null ||
                v.Trim().Length < 1)
            {
                return;
            }

            // validate if author is already apeared
            switch (k)
            {
            case "genre":
                if (aData.getGenre() == null)
                {
                    aData.setGenre(v);
                }

                return;

            case "first-name":
                if (aData.getName() == null)      // does not appear
                {
                    aData.setName(v);
                }

                return;

            case "last-name":
                if (aData.getLastName() == null)      // does not  appear
                {
                    aData.setLastName(v);
                }

                return;

            case "middle-name":
                if (aData.getMiddleName() == null)
                {
                    aData.setMiddleName(v);
                }

                return;

            case "book-title":     //if ( _bookName ) // already appears
                if (aData.getBookTitle() == null)
                {
                    aData.setBookTitle(v);
                }

                return;

            case "date":
                if (_date != null)
                {
                    return;
                }

                _date = k;
                break;

            case "year":
                if (_date != null)
                {
                    return;
                }
                break;

            case "sequence":
                if (_sequence != null)
                {
                    return;
                }

                _sequence = k;
                break;
            } // end switch

            if (outBuf == null)
            {
                return; // we can't write
            }
            // write author data a head
            if (aData.isWroteData() == false &&
                aData.hasData())
            {
                outBuf.Write(_encoding.GetBytes(aData.ToString()));
                aData.isWroteData(true);
            } // if

            if (parentName == "annotation" &&
                _hadAnotation == false)
            {
                outBuf.Write(parentName + "\r\n");
            }

            if (k == "sequence" ||
                k == "date")
            {
                outBuf.Write(k + " - ");
            }

            v = divideLine(v.Trim());
            if (v.StartsWith("http://") || v.StartsWith("www."))
            {
                return;
            }

            outBuf.Write(_encoding.GetBytes(v));
            outBuf.Write("\r\n");

            if (parentName == "annotation" &&
                _hadAnotation == false)
            {
                outBuf.Write("\r\n");
                _hadAnotation = true;
            } // if
        }     // fun