Ejemplo n.º 1
0
		private void ReadLdml(XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(reader != null);
			Debug.Assert(ws != null);
			if (reader.MoveToContent() != XmlNodeType.Element || reader.Name != "ldml")
			{
				throw new ApplicationException("Unable to load writing system definition: Missing <ldml> tag.");
			}
			reader.Read();
			if (FindElement(reader, "identity"))
			{
				ReadIdentityElement(reader, ws);
			}
			if (FindElement(reader, "layout"))
			{
				ReadLayoutElement(reader, ws);
			}
			if (FindElement(reader, "collations"))
			{
				ReadCollationsElement(reader, ws);
			}
			while (FindElement(reader, "special"))
			{
				ReadTopLevelSpecialElement(reader, ws);
			}
			ws.StoreID = "";
		}
Ejemplo n.º 2
0
 private void ReadCollationsElement(XmlReader reader, WritingSystemDefinitionV0 ws)
 {
     Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "collations");
     using (XmlReader collationsReader = reader.ReadSubtree())
     {
         collationsReader.MoveToContent();
         collationsReader.ReadStartElement("collations");
         bool found = false;
         while (FindElement(collationsReader, "collation"))
         {
             // having no type is the same as type=standard, and is the only one we're interested in
             string typeValue = collationsReader.GetAttribute("type");
             if (string.IsNullOrEmpty(typeValue) || typeValue == "standard")
             {
                 found = true;
                 break;
             }
             reader.Skip();
         }
         if (found)
         {
             reader.MoveToElement();
             string collationXml = reader.ReadInnerXml();
             ReadCollationElement(collationXml, ws);
         }
         while (collationsReader.Read())
         {
         }
     }
     if (!reader.IsEmptyElement)
     {
         reader.ReadEndElement();
     }
 }
Ejemplo n.º 3
0
 private void WriteLdml(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
 {
     Debug.Assert(writer != null);
     Debug.Assert(ws != null);
     writer.WriteStartElement("ldml");
     if (reader != null)
     {
         reader.MoveToContent();
         reader.ReadStartElement("ldml");
         CopyUntilElement(writer, reader, "identity");
     }
     WriteIdentityElement(writer, reader, ws);
     if (reader != null)
     {
         CopyUntilElement(writer, reader, "layout");
     }
     WriteLayoutElement(writer, reader, ws);
     if (reader != null)
     {
         CopyUntilElement(writer, reader, "collations");
     }
     WriteCollationsElement(writer, reader, ws);
     if (reader != null)
     {
         CopyUntilElement(writer, reader, "special");
     }
     WriteTopLevelSpecialElements(writer, ws);
     if (reader != null)
     {
         CopyOtherSpecialElements(writer, reader);
         CopyToEndElement(writer, reader);
     }
     writer.WriteEndElement();
 }
Ejemplo n.º 4
0
        protected virtual void ReadTopLevelSpecialElement(XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            if (reader.GetAttribute("xmlns:palaso") != null)
            {
                reader.ReadStartElement("special");
                ws.Abbreviation    = GetSpecialValue(reader, "palaso", "abbreviation");
                ws.DefaultFontName = GetSpecialValue(reader, "palaso", "defaultFontFamily");
                float fontSize;
                if (float.TryParse(GetSpecialValue(reader, "palaso", "defaultFontSize"), out fontSize))
                {
                    ws.DefaultFontSize = fontSize;
                }
                ws.Keyboard = GetSpecialValue(reader, "palaso", "defaultKeyboard");
                string isLegacyEncoded = GetSpecialValue(reader, "palaso", "isLegacyEncoded");
                if (!String.IsNullOrEmpty(isLegacyEncoded))
                {
                    ws.IsLegacyEncoded = Convert.ToBoolean(isLegacyEncoded);
                }
                ws.LanguageName    = GetSpecialValue(reader, "palaso", "languageName");
                ws.SpellCheckingId = GetSpecialValue(reader, "palaso", "spellCheckingId");

                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    reader.Read();
                }
                reader.ReadEndElement();
            }
            else
            {
                reader.Skip();
            }
        }
Ejemplo n.º 5
0
 private void ReadLayoutElement(XmlReader reader, WritingSystemDefinitionV0 ws)
 {
     // The orientation node has two attributes, "lines" and "characters" which define direction of writing.
     // The valid values are: "top-to-bottom", "bottom-to-top", "left-to-right", and "right-to-left"
     // Currently we only handle horizontal character orders with top-to-bottom line order, so
     // any value other than characters right-to-left, we treat as our default left-to-right order.
     // This probably works for many scripts such as various East Asian scripts which traditionally
     // are top-to-bottom characters and right-to-left lines, but can also be written with
     // left-to-right characters and top-to-bottom lines.
     Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "layout");
     using (XmlReader layoutReader = reader.ReadSubtree())
     {
         layoutReader.MoveToContent();
         layoutReader.ReadStartElement("layout");
         ws.RightToLeftScript = GetSubNodeAttributeValue(layoutReader, "orientation", "characters") ==
                                "right-to-left";
         while (layoutReader.Read())
         {
         }
     }
     if (!reader.IsEmptyElement)
     {
         reader.ReadEndElement();
     }
 }
Ejemplo n.º 6
0
		//WS-33992
		public void Read_LdmlContainsEmptyCollationElement_SortUsingIsSetToSameAsIfNoCollationElementExisted()
		{
			string ldmlWithEmptyCollationElement =
				"<ldml><!--Comment--><identity><version number=\"\" /><generation date=\"0001-01-01T00:00:00\" /><language type=\"qaa\" /></identity><dates /><collations><collation></collation></collations><special xmlns:palaso=\"urn://palaso.org/ldmlExtensions/v1\" /><special></special></ldml>";
			string ldmlwithNoCollationElement =
				"<ldml><!--Comment--><identity><version number=\"\" /><generation date=\"0001-01-01T00:00:00\" /><language type=\"qaa\" /></identity><dates /><collations/><special xmlns:palaso=\"urn://palaso.org/ldmlExtensions/v1\" /><special></special></ldml>";

			string pathToLdmlWithEmptyCollationElement = Path.GetTempFileName();
			try
			{
				File.WriteAllText(pathToLdmlWithEmptyCollationElement, ldmlWithEmptyCollationElement);
				string pathToLdmlWithNoCollationElement = Path.GetTempFileName();
				try
				{
					File.WriteAllText(pathToLdmlWithNoCollationElement, ldmlwithNoCollationElement);


					var adaptor = new LdmlAdaptorV0();
					var wsFromEmptyCollationElement = new WritingSystemDefinitionV0();
					adaptor.Read(pathToLdmlWithEmptyCollationElement, wsFromEmptyCollationElement);
					var wsFromNoCollationElement = new WritingSystemDefinitionV0();
					adaptor.Read(pathToLdmlWithNoCollationElement, wsFromNoCollationElement);

					Assert.AreEqual(wsFromNoCollationElement.SortUsing, wsFromEmptyCollationElement.SortUsing);
				}
				finally
				{
					File.Delete(pathToLdmlWithNoCollationElement);
				}
			}
			finally
			{
				File.Delete(pathToLdmlWithEmptyCollationElement);
			}
		}
Ejemplo n.º 7
0
        private void WriteCollationsElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "collations";

            writer.WriteStartElement("collations");
            if (needToCopy)
            {
                if (reader.IsEmptyElement)
                {
                    reader.Skip();
                    needToCopy = false;
                }
                else
                {
                    reader.ReadStartElement("collations");
                    if (FindElement(reader, "alias"))
                    {
                        reader.Skip();
                    }
                    CopyUntilElement(writer, reader, "collation");
                }
            }
            WriteCollationElement(writer, reader, ws);
            if (needToCopy)
            {
                CopyToEndElement(writer, reader);
            }
            writer.WriteEndElement();
        }
Ejemplo n.º 8
0
 private void ReadLdml(XmlReader reader, WritingSystemDefinitionV0 ws)
 {
     Debug.Assert(reader != null);
     Debug.Assert(ws != null);
     if (reader.MoveToContent() != XmlNodeType.Element || reader.Name != "ldml")
     {
         throw new ApplicationException("Unable to load writing system definition: Missing <ldml> tag.");
     }
     reader.Read();
     if (FindElement(reader, "identity"))
     {
         ReadIdentityElement(reader, ws);
     }
     if (FindElement(reader, "layout"))
     {
         ReadLayoutElement(reader, ws);
     }
     if (FindElement(reader, "collations"))
     {
         ReadCollationsElement(reader, ws);
     }
     while (FindElement(reader, "special"))
     {
         ReadTopLevelSpecialElement(reader, ws);
     }
     ws.StoreID = "";
 }
Ejemplo n.º 9
0
        private void ReadCollationRulesForOtherLanguage(string collationXml, WritingSystemDefinitionV0 ws)
        {
            XmlReaderSettings readerSettings = new XmlReaderSettings();

            readerSettings.CloseInput       = true;
            readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
            using (XmlReader collationReader = XmlReader.Create(new StringReader(collationXml), readerSettings))
            {
                bool foundValue = false;
                if (FindElement(collationReader, "base"))
                {
                    if (!collationReader.IsEmptyElement && collationReader.ReadToDescendant("alias"))
                    {
                        string sortRules = collationReader.GetAttribute("source");
                        if (sortRules != null)
                        {
                            ws.SortRules = sortRules;
                            foundValue   = true;
                        }
                    }
                }
                if (!foundValue)
                {
                    // missing base alias element, fall back to ICU rules
                    ws.SortUsing = WritingSystemDefinitionV0.SortRulesType.CustomICU;
                    ReadCollationRulesForCustomICU(collationXml, ws);
                }
            }
        }
        public override void Migrate(string sourceFilePath, string destinationFilePath)
        {
            string sourceFileName = Path.GetFileName(sourceFilePath);

            var writingSystemDefinitionV0 = new WritingSystemDefinitionV0();

            new LdmlAdaptorV0().Read(sourceFilePath, writingSystemDefinitionV0);

            var rfcHelper = new IetfLanguageTagCleaner(
                writingSystemDefinitionV0.ISO639,
                writingSystemDefinitionV0.Script,
                writingSystemDefinitionV0.Region,
                writingSystemDefinitionV0.Variant,
                "");

            rfcHelper.Clean();

            var writingSystemDefinitionV1 = new WritingSystemDefinitionV1
            {
                DefaultFontName    = writingSystemDefinitionV0.DefaultFontName,
                Abbreviation       = writingSystemDefinitionV0.Abbreviation,
                DefaultFontSize    = writingSystemDefinitionV0.DefaultFontSize,
                IsUnicodeEncoded   = !writingSystemDefinitionV0.IsLegacyEncoded,
                Keyboard           = writingSystemDefinitionV0.Keyboard,
                LanguageName       = writingSystemDefinitionV0.LanguageName,
                RightToLeftScript  = writingSystemDefinitionV0.RightToLeftScript,
                SortRules          = writingSystemDefinitionV0.SortRules,
                SortUsing          = (WritingSystemDefinitionV1.SortRulesType)writingSystemDefinitionV0.SortUsing,
                SpellCheckingId    = writingSystemDefinitionV0.SpellCheckingId,
                VersionDescription = writingSystemDefinitionV0.VersionDescription,
                DateModified       = DateTime.Now
            };

            writingSystemDefinitionV1.SetAllComponents(
                rfcHelper.Language,
                rfcHelper.Script,
                rfcHelper.Region,
                ConcatenateVariantAndPrivateUse(rfcHelper.Variant, rfcHelper.PrivateUse)
                );
            _writingSystemsV1[sourceFileName] = writingSystemDefinitionV1;
            //_migratedWs.VerboseDescription //not written out by LdmlAdaptorV1 - flex?
            //_migratedWs.NativeName //not written out by LdmlAdaptorV1 - flex?);

            // Record the details for use in PostMigrate where we change the file name to match the rfc tag where we can.
            var migrationInfo = new LdmlMigrationInfo(sourceFileName)
            {
                LanguageTagBeforeMigration = writingSystemDefinitionV0.Rfc5646,
                LanguageTagAfterMigration  = writingSystemDefinitionV1.Bcp47Tag
            };

            _migrationInfo.Add(migrationInfo);
        }
Ejemplo n.º 11
0
        private void ReadCollationRulesForCustomSimple(string collationXml, WritingSystemDefinitionV0 ws)
        {
            string rules;

            if (LdmlCollationParserV0.TryGetSimpleRulesFromCollationNode(collationXml, out rules))
            {
                ws.SortRules = rules;
                return;
            }
            // fall back to ICU rules if Simple rules don't work
            ws.SortUsing = WritingSystemDefinitionV0.SortRulesType.CustomICU;
            ReadCollationRulesForCustomICU(collationXml, ws);
        }
		public override void Migrate(string sourceFilePath, string destinationFilePath)
		{
			string sourceFileName = Path.GetFileName(sourceFilePath);

			var writingSystemDefinitionV0 = new WritingSystemDefinitionV0();
			new LdmlAdaptorV0().Read(sourceFilePath, writingSystemDefinitionV0);

			var rfcHelper = new IetfLanguageTagCleaner(
				writingSystemDefinitionV0.ISO639,
				writingSystemDefinitionV0.Script,
				writingSystemDefinitionV0.Region,
				writingSystemDefinitionV0.Variant,
				"");

			rfcHelper.Clean();

			var writingSystemDefinitionV1 = new WritingSystemDefinitionV1
				{
					DefaultFontName = writingSystemDefinitionV0.DefaultFontName,
					Abbreviation = writingSystemDefinitionV0.Abbreviation,
					DefaultFontSize = writingSystemDefinitionV0.DefaultFontSize,
					IsUnicodeEncoded = !writingSystemDefinitionV0.IsLegacyEncoded,
					Keyboard = writingSystemDefinitionV0.Keyboard,
					LanguageName = writingSystemDefinitionV0.LanguageName,
					RightToLeftScript = writingSystemDefinitionV0.RightToLeftScript,
					SortRules = writingSystemDefinitionV0.SortRules,
					SortUsing = (WritingSystemDefinitionV1.SortRulesType) writingSystemDefinitionV0.SortUsing,
					SpellCheckingId = writingSystemDefinitionV0.SpellCheckingId,
					VersionDescription = writingSystemDefinitionV0.VersionDescription,
					DateModified = DateTime.Now
				};

			writingSystemDefinitionV1.SetAllComponents(
				rfcHelper.Language,
				rfcHelper.Script,
				rfcHelper.Region,
				ConcatenateVariantAndPrivateUse(rfcHelper.Variant, rfcHelper.PrivateUse)
			);
			_writingSystemsV1[sourceFileName] = writingSystemDefinitionV1;
			//_migratedWs.VerboseDescription //not written out by LdmlAdaptorV1 - flex?
			//_migratedWs.NativeName //not written out by LdmlAdaptorV1 - flex?);

			// Record the details for use in PostMigrate where we change the file name to match the rfc tag where we can.
			var migrationInfo = new LdmlMigrationInfo(sourceFileName)
				{
					LanguageTagBeforeMigration = writingSystemDefinitionV0.Rfc5646,
					LanguageTagAfterMigration = writingSystemDefinitionV1.Bcp47Tag
				};
			_migrationInfo.Add(migrationInfo);
		}
Ejemplo n.º 13
0
        public void FillWithDefaults(string rfc4646, WritingSystemDefinitionV0 ws)
        {
            string id = rfc4646.ToLower();

            switch (id)
            {
            case "en-latn":
                ws.ISO639       = "en";
                ws.LanguageName = "English";
                ws.Abbreviation = "eng";
                ws.Script       = "Latn";
                break;

            default:
                ws.Script = "Latn";
                break;
            }
        }
Ejemplo n.º 14
0
        private void WriteLayoutElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "identity";
            // if we're left-to-right, we don't need to write out default values
            bool needLayoutElement = ws.RightToLeftScript;

            if (needLayoutElement)
            {
                writer.WriteStartElement("layout");
                writer.WriteStartElement("orientation");
                // omit default value for "lines" attribute
                writer.WriteAttributeString("characters", "right-to-left");
                writer.WriteEndElement();
            }
            if (needToCopy)
            {
                if (reader.IsEmptyElement)
                {
                    reader.Skip();
                }
                else
                {
                    reader.Read();
                    // skip any existing orientation and alias element, and copy the rest
                    if (FindElement(reader, "orientation"))
                    {
                        reader.Skip();
                    }
                    if (reader.NodeType != XmlNodeType.EndElement && !needLayoutElement)
                    {
                        needLayoutElement = true;
                        writer.WriteStartElement("layout");
                    }
                    CopyToEndElement(writer, reader);
                }
            }
            if (needLayoutElement)
            {
                writer.WriteEndElement();
            }
        }
Ejemplo n.º 15
0
        private void ReadCollationElement(string collationXml, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(collationXml != null);
            Debug.Assert(ws != null);

            XmlReaderSettings readerSettings = new XmlReaderSettings();

            readerSettings.CloseInput       = true;
            readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
            using (XmlReader collationReader = XmlReader.Create(new StringReader(collationXml), readerSettings))
            {
                if (FindElement(collationReader, "special"))
                {
                    collationReader.Read();
                    string rulesTypeAsString = GetSpecialValue(collationReader, "palaso", "sortRulesType");
                    if (!String.IsNullOrEmpty(rulesTypeAsString))
                    {
                        ws.SortUsing = (WritingSystemDefinitionV0.SortRulesType)Enum.Parse(typeof(WritingSystemDefinitionV0.SortRulesType), rulesTypeAsString);
                    }
                }
            }
            switch (ws.SortUsing)
            {
            case WritingSystemDefinitionV0.SortRulesType.OtherLanguage:
                ReadCollationRulesForOtherLanguage(collationXml, ws);
                break;

            case WritingSystemDefinitionV0.SortRulesType.CustomSimple:
                ReadCollationRulesForCustomSimple(collationXml, ws);
                break;

            case WritingSystemDefinitionV0.SortRulesType.CustomICU:
                ReadCollationRulesForCustomICU(collationXml, ws);
                break;

            case WritingSystemDefinitionV0.SortRulesType.DefaultOrdering:
                break;

            default:
                string message = string.Format("Unhandled SortRulesType '{0}' while writing LDML definition file.", ws.SortUsing);
                throw new ApplicationException(message);
            }
        }
Ejemplo n.º 16
0
        public void Write(string filePath, WritingSystemDefinitionV0 ws, Stream oldFile)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (ws == null)
            {
                throw new ArgumentNullException("ws");
            }
            XmlReader reader = null;

            try
            {
                if (oldFile != null)
                {
                    var readerSettings = new XmlReaderSettings
                    {
                        NameTable        = _nameSpaceManager.NameTable,
                        ConformanceLevel = ConformanceLevel.Auto,
                        ValidationType   = ValidationType.None,
                        XmlResolver      = null,
                        DtdProcessing    = DtdProcessing.Parse,
                        IgnoreWhitespace = true
                    };
                    reader = XmlReader.Create(oldFile, readerSettings);
                }
                using (XmlWriter writer = XmlWriter.Create(filePath, CanonicalXmlSettings.CreateXmlWriterSettings()))
                {
                    writer.WriteStartDocument();
                    WriteLdml(writer, reader, ws);
                    writer.Close();
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Ejemplo n.º 17
0
        private void WriteIdentityElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "identity";

            writer.WriteStartElement("identity");
            writer.WriteStartElement("version");
            writer.WriteAttributeString("number", ws.VersionNumber);
            writer.WriteString(ws.VersionDescription);
            writer.WriteEndElement();
            WriteElementWithAttribute(writer, "generation", "date", String.Format("{0:s}", ws.DateModified));
            WriteElementWithAttribute(writer, "language", "type", ws.ISO639);
            if (!String.IsNullOrEmpty(ws.Script))
            {
                WriteElementWithAttribute(writer, "script", "type", ws.Script);
            }
            if (!String.IsNullOrEmpty(ws.Region))
            {
                WriteElementWithAttribute(writer, "territory", "type", ws.Region);
            }
            if (!String.IsNullOrEmpty(ws.Variant))
            {
                WriteElementWithAttribute(writer, "variant", "type", ws.Variant);
            }
            if (needToCopy)
            {
                if (reader.IsEmptyElement)
                {
                    reader.Skip();
                }
                else
                {
                    reader.Read();                     // move past "identity" element}

                    // <special> is the only node we possibly left out and need to copy
                    FindElement(reader, "special");
                    CopyToEndElement(writer, reader);
                }
            }
            writer.WriteEndElement();
        }
Ejemplo n.º 18
0
 protected virtual void WriteTopLevelSpecialElements(XmlWriter writer, WritingSystemDefinitionV0 ws)
 {
     WriteBeginSpecialElement(writer, "palaso");
     WriteSpecialValue(writer, "palaso", "abbreviation", ws.Abbreviation);
     WriteSpecialValue(writer, "palaso", "defaultFontFamily", ws.DefaultFontName);
     if (ws.DefaultFontSize != 0)
     {
         WriteSpecialValue(writer, "palaso", "defaultFontSize", ws.DefaultFontSize.ToString());
     }
     WriteSpecialValue(writer, "palaso", "defaultKeyboard", ws.Keyboard);
     if (ws.IsLegacyEncoded)
     {
         WriteSpecialValue(writer, "palaso", "isLegacyEncoded", ws.IsLegacyEncoded.ToString());
     }
     WriteSpecialValue(writer, "palaso", "languageName", ws.LanguageName);
     if (ws.SpellCheckingId != ws.ISO639)
     {
         WriteSpecialValue(writer, "palaso", "spellCheckingId", ws.SpellCheckingId);
     }
     writer.WriteEndElement();
 }
Ejemplo n.º 19
0
        private void ReadIdentityElement(XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "identity");
            using (XmlReader identityReader = reader.ReadSubtree())
            {
                identityReader.MoveToContent();
                identityReader.ReadStartElement("identity");
                if (FindElement(identityReader, "version"))
                {
                    ws.VersionNumber = identityReader.GetAttribute("number") ?? string.Empty;
                    if (!identityReader.IsEmptyElement)
                    {
                        ws.VersionDescription = identityReader.ReadString();
                        identityReader.ReadEndElement();
                    }
                }
                string   dateTime = GetSubNodeAttributeValue(identityReader, "generation", "date");
                DateTime modified = DateTime.UtcNow;
                if (!string.IsNullOrEmpty(dateTime.Trim()) && !DateTime.TryParse(dateTime, out modified))
                {
                    //CVS format:    "$Date: 2008/06/18 22:52:35 $"
                    modified = DateTime.ParseExact(dateTime, "'$Date: 'yyyy/MM/dd HH:mm:ss $", null,
                                                   DateTimeStyles.AssumeUniversal);
                }
                ws.DateModified = modified;
                ws.ISO639       = GetSubNodeAttributeValue(identityReader, "language", "type");
                ws.Script       = GetSubNodeAttributeValue(identityReader, "script", "type");
                ws.Region       = GetSubNodeAttributeValue(identityReader, "territory", "type");
                ws.Variant      = GetSubNodeAttributeValue(identityReader, "variant", "type");

                // move to end of identity node
                while (identityReader.Read())
                {
                }
            }
            if (!reader.IsEmptyElement)
            {
                reader.ReadEndElement();
            }
        }
Ejemplo n.º 20
0
		public void Read(string filePath, WritingSystemDefinitionV0 ws)
		{
			if (filePath == null)
			{
				throw new ArgumentNullException("filePath");
			}
			if (ws == null)
			{
				throw new ArgumentNullException("ws");
			}
			var settings = new XmlReaderSettings
			{
				NameTable = _nameSpaceManager.NameTable,
				ValidationType = ValidationType.None,
				XmlResolver = null,
				DtdProcessing = DtdProcessing.Parse
			};
			using (XmlReader reader = XmlReader.Create(filePath, settings))
			{
				ReadLdml(reader, ws);
			}
		}
Ejemplo n.º 21
0
		public void Read(XmlReader xmlReader, WritingSystemDefinitionV0 ws)
		{
			if (xmlReader == null)
			{
				throw new ArgumentNullException("xmlReader");
			}
			if (ws == null)
			{
				throw new ArgumentNullException("ws");
			}
			var settings = new XmlReaderSettings
			{
				NameTable = _nameSpaceManager.NameTable,
				ConformanceLevel = ConformanceLevel.Auto,
				ValidationType = ValidationType.None,
				XmlResolver = null,
				DtdProcessing = DtdProcessing.Parse
			};
			using (XmlReader reader = XmlReader.Create(xmlReader, settings))
			{
				ReadLdml(reader, ws);
			}
		}
Ejemplo n.º 22
0
        public void Read(string filePath, WritingSystemDefinitionV0 ws)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (ws == null)
            {
                throw new ArgumentNullException("ws");
            }
            var settings = new XmlReaderSettings
            {
                NameTable      = _nameSpaceManager.NameTable,
                ValidationType = ValidationType.None,
                XmlResolver    = null,
                DtdProcessing  = DtdProcessing.Parse
            };

            using (XmlReader reader = XmlReader.Create(filePath, settings))
            {
                ReadLdml(reader, ws);
            }
        }
Ejemplo n.º 23
0
        public void Write(XmlWriter xmlWriter, WritingSystemDefinitionV0 ws, XmlReader xmlReader)
        {
            if (xmlWriter == null)
            {
                throw new ArgumentNullException("xmlWriter");
            }
            if (ws == null)
            {
                throw new ArgumentNullException("ws");
            }
            XmlReader reader = null;

            try
            {
                if (xmlReader != null)
                {
                    var settings = new XmlReaderSettings
                    {
                        NameTable        = _nameSpaceManager.NameTable,
                        ConformanceLevel = ConformanceLevel.Auto,
                        ValidationType   = ValidationType.None,
                        XmlResolver      = null,
                        DtdProcessing    = DtdProcessing.Parse
                    };
                    reader = XmlReader.Create(xmlReader, settings);
                }
                WriteLdml(xmlWriter, reader, ws);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Ejemplo n.º 24
0
        public void Read(XmlReader xmlReader, WritingSystemDefinitionV0 ws)
        {
            if (xmlReader == null)
            {
                throw new ArgumentNullException("xmlReader");
            }
            if (ws == null)
            {
                throw new ArgumentNullException("ws");
            }
            var settings = new XmlReaderSettings
            {
                NameTable        = _nameSpaceManager.NameTable,
                ConformanceLevel = ConformanceLevel.Auto,
                ValidationType   = ValidationType.None,
                XmlResolver      = null,
                DtdProcessing    = DtdProcessing.Parse
            };

            using (XmlReader reader = XmlReader.Create(xmlReader, settings))
            {
                ReadLdml(reader, ws);
            }
        }
Ejemplo n.º 25
0
		private void ReadCollationRulesForCustomSimple(string collationXml, WritingSystemDefinitionV0 ws)
		{
			string rules;
			if (LdmlCollationParserV0.TryGetSimpleRulesFromCollationNode(collationXml, out rules))
			{
				ws.SortRules = rules;
				return;
			}
			// fall back to ICU rules if Simple rules don't work
			ws.SortUsing = WritingSystemDefinitionV0.SortRulesType.CustomICU;
			ReadCollationRulesForCustomICU(collationXml, ws);
		}
Ejemplo n.º 26
0
		private void WriteCollationRulesFromOtherLanguage(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.OtherLanguage);

			// Since the alias element gets all information from another source,
			// we should remove all other elements in this collation element.  We
			// leave "special" elements as they are custom data from some other app.
			writer.WriteStartElement("base");
			WriteElementWithAttribute(writer, "alias", "source", ws.SortRules);
			writer.WriteEndElement();
			if (reader != null)
			{
				// don't copy anything, but skip to the 1st special node
				FindElement(reader, "special");
			}
		}
		public void Migrate_OriginalFileContainsNoCollationInfo_StandardCollationInfoIsMigrated()
		{
			using (var environment = new TestEnvironment())
			{
				environment.WriteLdmlFile(
					"test.ldml",
					LdmlContentForTests.Version0WithCollationInfo(WritingSystemDefinitionV0.SortRulesType.DefaultOrdering));
				var wsV0 = new WritingSystemDefinitionV0();
				new LdmlAdaptorV0().Read(environment.FilePath("test.ldml"), wsV0);

				var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback);
				migrator.Migrate();

				var wsV3 = new WritingSystemDefinition();
				new LdmlDataMapper(null).Read(environment.MappedFilePath("test.ldml"), wsV3);
				var cdV3 = (IcuRulesCollationDefinition) wsV3.Collations.First();
				Assert.IsNullOrEmpty(wsV0.SortRules);
				Assert.IsNullOrEmpty(cdV3.CollationRules);
				Assert.That(cdV3.Type, Is.EqualTo("standard"));
			}
		}
		public void Migrate_OriginalFileContainsCustomIcuCollationInfo_CollationInfoIsMigrated()
		{
			using (var environment = new TestEnvironment())
			{
				environment.WriteLdmlFile(
					"test.ldml",
					LdmlContentForTests.Version0WithCollationInfo(WritingSystemDefinitionV0.SortRulesType.CustomICU));
				var wsV0 = new WritingSystemDefinitionV0();
				new LdmlAdaptorV0().Read(environment.FilePath("test.ldml"), wsV0);

				var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback);
				migrator.Migrate();

				var wsV3 = new WritingSystemDefinition();
				new LdmlDataMapper(null).Read(environment.MappedFilePath("test.ldml"), wsV3);
				var cdV3 = (IcuRulesCollationDefinition) wsV3.Collations.First();
				Assert.AreEqual(wsV0.SortRules, cdV3.CollationRules);
			}
		}
Ejemplo n.º 29
0
 private void WriteCollationRulesFromCustomIcu(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
 {
     Debug.Assert(writer != null);
     Debug.Assert(ws != null);
     Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.CustomICU);
     WriteCollationRulesFromIcuString(writer, reader, ws.SortRules);
 }
Ejemplo n.º 30
0
		private void WriteCollationRulesFromCustomIcu(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.CustomICU);
			WriteCollationRulesFromIcuString(writer, reader, ws.SortRules);
		}
Ejemplo n.º 31
0
		private void ReadCollationsElement(XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "collations");
			using (XmlReader collationsReader = reader.ReadSubtree())
			{
				collationsReader.MoveToContent();
				collationsReader.ReadStartElement("collations");
				bool found = false;
				while (FindElement(collationsReader, "collation"))
				{
					// having no type is the same as type=standard, and is the only one we're interested in
					string typeValue = collationsReader.GetAttribute("type");
					if (string.IsNullOrEmpty(typeValue) || typeValue == "standard")
					{
						found = true;
						break;
					}
					reader.Skip();
				}
				if (found)
				{
					reader.MoveToElement();
					string collationXml = reader.ReadInnerXml();
					ReadCollationElement(collationXml, ws);
				}
				while (collationsReader.Read()) { }
			}
			if (!reader.IsEmptyElement)
			{
				reader.ReadEndElement();
			}
		}
Ejemplo n.º 32
0
		private void ReadCollationRulesForOtherLanguage(string collationXml, WritingSystemDefinitionV0 ws)
		{
			XmlReaderSettings readerSettings = new XmlReaderSettings();
			readerSettings.CloseInput = true;
			readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
			using (XmlReader collationReader = XmlReader.Create(new StringReader(collationXml), readerSettings))
			{
				bool foundValue = false;
				if (FindElement(collationReader, "base"))
				{
					if (!collationReader.IsEmptyElement && collationReader.ReadToDescendant("alias"))
					{
						string sortRules = collationReader.GetAttribute("source");
						if (sortRules != null)
						{
							ws.SortRules = sortRules;
							foundValue = true;
						}
					}
				}
				if (!foundValue)
				{
					// missing base alias element, fall back to ICU rules
					ws.SortUsing = WritingSystemDefinitionV0.SortRulesType.CustomICU;
					ReadCollationRulesForCustomICU(collationXml, ws);
				}
			}
		}
Ejemplo n.º 33
0
		private void WriteIdentityElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "identity";

			writer.WriteStartElement("identity");
			writer.WriteStartElement("version");
			writer.WriteAttributeString("number", ws.VersionNumber);
			writer.WriteString(ws.VersionDescription);
			writer.WriteEndElement();
			WriteElementWithAttribute(writer, "generation", "date", String.Format("{0:s}", ws.DateModified));
			WriteElementWithAttribute(writer, "language", "type", ws.ISO639);
			if (!String.IsNullOrEmpty(ws.Script))
			{
				WriteElementWithAttribute(writer, "script", "type", ws.Script);
			}
			if (!String.IsNullOrEmpty(ws.Region))
			{
				WriteElementWithAttribute(writer, "territory", "type", ws.Region);
			}
			if (!String.IsNullOrEmpty(ws.Variant))
			{
				WriteElementWithAttribute(writer, "variant", "type", ws.Variant);
			}
			if (needToCopy)
			{
				if (reader.IsEmptyElement)
				{
					reader.Skip();
				}
				else
				{
					reader.Read(); // move past "identity" element}

					// <special> is the only node we possibly left out and need to copy
					FindElement(reader, "special");
					CopyToEndElement(writer, reader);
				}
			}
			writer.WriteEndElement();
		}
Ejemplo n.º 34
0
		private void ReadIdentityElement(XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "identity");
			using (XmlReader identityReader = reader.ReadSubtree())
			{
				identityReader.MoveToContent();
				identityReader.ReadStartElement("identity");
				if (FindElement(identityReader, "version"))
				{
					ws.VersionNumber = identityReader.GetAttribute("number") ?? string.Empty;
					if (!identityReader.IsEmptyElement)
					{
						ws.VersionDescription = identityReader.ReadString();
						identityReader.ReadEndElement();
					}
				}
				string dateTime = GetSubNodeAttributeValue(identityReader, "generation", "date");
				DateTime modified = DateTime.UtcNow;
				if (!string.IsNullOrEmpty(dateTime.Trim()) && !DateTime.TryParse(dateTime, out modified))
				{
					//CVS format:    "$Date: 2008/06/18 22:52:35 $"
					modified = DateTime.ParseExact(dateTime, "'$Date: 'yyyy/MM/dd HH:mm:ss $", null,
												   DateTimeStyles.AssumeUniversal);
				}
				ws.DateModified = modified;
				ws.ISO639 = GetSubNodeAttributeValue(identityReader, "language", "type");
				ws.Script = GetSubNodeAttributeValue(identityReader, "script", "type");
				ws.Region = GetSubNodeAttributeValue(identityReader, "territory", "type");
				ws.Variant = GetSubNodeAttributeValue(identityReader, "variant", "type");

				// move to end of identity node
				while (identityReader.Read()) { }
			}
			if (!reader.IsEmptyElement)
			{
				reader.ReadEndElement();
			}
		}
Ejemplo n.º 35
0
		private void WriteLayoutElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "identity";
			// if we're left-to-right, we don't need to write out default values
			bool needLayoutElement = ws.RightToLeftScript;

			if (needLayoutElement)
			{
				writer.WriteStartElement("layout");
				writer.WriteStartElement("orientation");
				// omit default value for "lines" attribute
				writer.WriteAttributeString("characters", "right-to-left");
				writer.WriteEndElement();
			}
			if (needToCopy)
			{
				if (reader.IsEmptyElement)
				{
					reader.Skip();
				}
				else
				{
					reader.Read();
					// skip any existing orientation and alias element, and copy the rest
					if (FindElement(reader, "orientation"))
					{
						reader.Skip();
					}
					if (reader.NodeType != XmlNodeType.EndElement && !needLayoutElement)
					{
						needLayoutElement = true;
						writer.WriteStartElement("layout");
					}
					CopyToEndElement(writer, reader);
				}
			}
			if (needLayoutElement)
			{
				writer.WriteEndElement();
			}
		}
Ejemplo n.º 36
0
		protected virtual void WriteTopLevelSpecialElements(XmlWriter writer, WritingSystemDefinitionV0 ws)
		{
			WriteBeginSpecialElement(writer, "palaso");
			WriteSpecialValue(writer, "palaso", "abbreviation", ws.Abbreviation);
			WriteSpecialValue(writer, "palaso", "defaultFontFamily", ws.DefaultFontName);
			if (ws.DefaultFontSize != 0)
			{
				WriteSpecialValue(writer, "palaso", "defaultFontSize", ws.DefaultFontSize.ToString());
			}
			WriteSpecialValue(writer, "palaso", "defaultKeyboard", ws.Keyboard);
			if (ws.IsLegacyEncoded)
			{
				WriteSpecialValue(writer, "palaso", "isLegacyEncoded", ws.IsLegacyEncoded.ToString());
			}
			WriteSpecialValue(writer, "palaso", "languageName", ws.LanguageName);
			if (ws.SpellCheckingId != ws.ISO639)
			{
				WriteSpecialValue(writer, "palaso", "spellCheckingId", ws.SpellCheckingId);
			}
			writer.WriteEndElement();
		}
Ejemplo n.º 37
0
		private void WriteCollationsElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "collations";

			writer.WriteStartElement("collations");
			if (needToCopy)
			{
				if (reader.IsEmptyElement)
				{
					reader.Skip();
					needToCopy = false;
				}
				else
				{
					reader.ReadStartElement("collations");
					if (FindElement(reader, "alias"))
					{
						reader.Skip();
					}
					CopyUntilElement(writer, reader, "collation");
				}
			}
			WriteCollationElement(writer, reader, ws);
			if (needToCopy)
			{
				CopyToEndElement(writer, reader);
			}
			writer.WriteEndElement();
		}
Ejemplo n.º 38
0
		protected virtual void ReadTopLevelSpecialElement(XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			if (reader.GetAttribute("xmlns:palaso") != null)
			{
				reader.ReadStartElement("special");
				ws.Abbreviation = GetSpecialValue(reader, "palaso", "abbreviation");
				ws.DefaultFontName = GetSpecialValue(reader, "palaso", "defaultFontFamily");
				float fontSize;
				if (float.TryParse(GetSpecialValue(reader, "palaso", "defaultFontSize"), out fontSize))
				{
					ws.DefaultFontSize = fontSize;
				}
				ws.Keyboard = GetSpecialValue(reader, "palaso", "defaultKeyboard");
				string isLegacyEncoded = GetSpecialValue(reader, "palaso", "isLegacyEncoded");
				if (!String.IsNullOrEmpty(isLegacyEncoded))
				{
					ws.IsLegacyEncoded = Convert.ToBoolean(isLegacyEncoded);
				}
				ws.LanguageName = GetSpecialValue(reader, "palaso", "languageName");
				ws.SpellCheckingId = GetSpecialValue(reader, "palaso", "spellCheckingId");

				while (reader.NodeType != XmlNodeType.EndElement)
				{
					reader.Read();
				}
				reader.ReadEndElement();
			}
			else
			{
				reader.Skip();
			}
		}
Ejemplo n.º 39
0
		private void ReadCollationRulesForCustomICU(string collationXml, WritingSystemDefinitionV0 ws)
		{
			ws.SortRules = LdmlCollationParserV0.GetIcuRulesFromCollationNode(collationXml);
		}
Ejemplo n.º 40
0
		public void FillWithDefaults(string rfc4646, WritingSystemDefinitionV0 ws)
		{
			string id = rfc4646.ToLower();
			switch (id)
			{
				case "en-latn":
					ws.ISO639 = "en";
					ws.LanguageName = "English";
					ws.Abbreviation = "eng";
					ws.Script = "Latn";
					break;
				 default:
					ws.Script = "Latn";
					break;
			}
		}
Ejemplo n.º 41
0
		private void ReadCollationElement(string collationXml, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(collationXml != null);
			Debug.Assert(ws != null);

			XmlReaderSettings readerSettings = new XmlReaderSettings();
			readerSettings.CloseInput = true;
			readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
			using (XmlReader collationReader = XmlReader.Create(new StringReader(collationXml), readerSettings))
			{
				if (FindElement(collationReader, "special"))
				{
					collationReader.Read();
					string rulesTypeAsString = GetSpecialValue(collationReader, "palaso", "sortRulesType");
					if (!String.IsNullOrEmpty(rulesTypeAsString))
					{
						ws.SortUsing = (WritingSystemDefinitionV0.SortRulesType)Enum.Parse(typeof(WritingSystemDefinitionV0.SortRulesType), rulesTypeAsString);
					}
				}
			}
			switch (ws.SortUsing)
			{
				case WritingSystemDefinitionV0.SortRulesType.OtherLanguage:
					ReadCollationRulesForOtherLanguage(collationXml, ws);
					break;
				case WritingSystemDefinitionV0.SortRulesType.CustomSimple:
					ReadCollationRulesForCustomSimple(collationXml, ws);
					break;
				case WritingSystemDefinitionV0.SortRulesType.CustomICU:
					ReadCollationRulesForCustomICU(collationXml, ws);
					break;
				case WritingSystemDefinitionV0.SortRulesType.DefaultOrdering:
					break;
				default:
					string message = string.Format("Unhandled SortRulesType '{0}' while writing LDML definition file.", ws.SortUsing);
					throw new ApplicationException(message);
			}
		}
Ejemplo n.º 42
0
		private void ReadLayoutElement(XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			// The orientation node has two attributes, "lines" and "characters" which define direction of writing.
			// The valid values are: "top-to-bottom", "bottom-to-top", "left-to-right", and "right-to-left"
			// Currently we only handle horizontal character orders with top-to-bottom line order, so
			// any value other than characters right-to-left, we treat as our default left-to-right order.
			// This probably works for many scripts such as various East Asian scripts which traditionally
			// are top-to-bottom characters and right-to-left lines, but can also be written with
			// left-to-right characters and top-to-bottom lines.
			Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == "layout");
			using (XmlReader layoutReader = reader.ReadSubtree())
			{
				layoutReader.MoveToContent();
				layoutReader.ReadStartElement("layout");
				ws.RightToLeftScript = GetSubNodeAttributeValue(layoutReader, "orientation", "characters") ==
									   "right-to-left";
				while (layoutReader.Read()) { }
			}
			if (!reader.IsEmptyElement)
			{
				reader.ReadEndElement();
			}
		}
Ejemplo n.º 43
0
        private void WriteCollationRulesFromOtherLanguage(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.OtherLanguage);

            // Since the alias element gets all information from another source,
            // we should remove all other elements in this collation element.  We
            // leave "special" elements as they are custom data from some other app.
            writer.WriteStartElement("base");
            WriteElementWithAttribute(writer, "alias", "source", ws.SortRules);
            writer.WriteEndElement();
            if (reader != null)
            {
                // don't copy anything, but skip to the 1st special node
                FindElement(reader, "special");
            }
        }
Ejemplo n.º 44
0
		private void WriteCollationRulesFromCustomSimple(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.CustomSimple);

			var parser = new SimpleRulesParser();
			string message;
			// avoid throwing exception, just don't save invalid data
			if (!parser.ValidateSimpleRules(ws.SortRules ?? string.Empty, out message))
			{
				return;
			}
			string icu = parser.ConvertToIcuRules(ws.SortRules ?? string.Empty);
			WriteCollationRulesFromIcuString(writer, reader, icu);
		}
Ejemplo n.º 45
0
        private void WriteCollationElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "collation";

            if (needToCopy)
            {
                string collationType = reader.GetAttribute("type");
                needToCopy = String.IsNullOrEmpty(collationType) || collationType == "standard";
            }
            if (needToCopy && reader.IsEmptyElement)
            {
                reader.Skip();
                needToCopy = false;
            }

            if (ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.DefaultOrdering && !needToCopy)
            {
                return;
            }

            if (needToCopy && reader.IsEmptyElement)
            {
                reader.Skip();
                needToCopy = false;
            }
            if (!needToCopy)
            {
                // set to null if we don't need to copy to make it easier to tell in the methods we call
                reader = null;
            }
            else
            {
                reader.ReadStartElement("collation");
                while (reader.NodeType == XmlNodeType.Attribute)
                {
                    reader.Read();
                }
            }

            if (ws.SortUsing != WritingSystemDefinitionV0.SortRulesType.DefaultOrdering)
            {
                writer.WriteStartElement("collation");
                switch (ws.SortUsing)
                {
                case WritingSystemDefinitionV0.SortRulesType.OtherLanguage:
                    WriteCollationRulesFromOtherLanguage(writer, reader, ws);
                    break;

                case WritingSystemDefinitionV0.SortRulesType.CustomSimple:
                    WriteCollationRulesFromCustomSimple(writer, reader, ws);
                    break;

                case WritingSystemDefinitionV0.SortRulesType.CustomICU:
                    WriteCollationRulesFromCustomIcu(writer, reader, ws);
                    break;

                default:
                    string message = string.Format("Unhandled SortRulesType '{0}' while writing LDML definition file.", ws.SortUsing);
                    throw new ApplicationException(message);
                }
                WriteBeginSpecialElement(writer, "palaso");
                WriteSpecialValue(writer, "palaso", "sortRulesType", ws.SortUsing.ToString());
                writer.WriteEndElement();
                if (needToCopy)
                {
                    if (FindElement(reader, "special"))
                    {
                        CopyOtherSpecialElements(writer, reader);
                    }
                    CopyToEndElement(writer, reader);
                }
                writer.WriteEndElement();
            }
            else if (needToCopy)
            {
                bool startElementWritten = false;
                if (FindElement(reader, "special"))
                {
                    // write out any other special elements
                    while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement &&
                           (reader.NodeType != XmlNodeType.Element || reader.Name == "special"))
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            bool knownNS = IsKnownSpecialElement(reader);
                            reader.MoveToElement();
                            if (knownNS)
                            {
                                reader.Skip();
                                continue;
                            }
                        }
                        if (!startElementWritten)
                        {
                            writer.WriteStartElement("collation");
                            startElementWritten = true;
                        }
                        writer.WriteNode(reader, false);
                    }
                }

                if (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
                {
                    // copy any other elements
                    if (!startElementWritten)
                    {
                        writer.WriteStartElement("collation");
                        startElementWritten = true;
                    }
                    CopyToEndElement(writer, reader);
                }
                if (startElementWritten)
                {
                    writer.WriteEndElement();
                }
            }
        }
Ejemplo n.º 46
0
		private void WriteLdml(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			writer.WriteStartElement("ldml");
			if (reader != null)
			{
				reader.MoveToContent();
				reader.ReadStartElement("ldml");
				CopyUntilElement(writer, reader, "identity");
			}
			WriteIdentityElement(writer, reader, ws);
			if (reader != null)
			{
				CopyUntilElement(writer, reader, "layout");
			}
			WriteLayoutElement(writer, reader, ws);
			if (reader != null)
			{
				CopyUntilElement(writer, reader, "collations");
			}
			WriteCollationsElement(writer, reader, ws);
			if (reader != null)
			{
				CopyUntilElement(writer, reader, "special");
			}
			WriteTopLevelSpecialElements(writer, ws);
			if (reader != null)
			{
				CopyOtherSpecialElements(writer, reader);
				CopyToEndElement(writer, reader);
			}
			writer.WriteEndElement();
		}
Ejemplo n.º 47
0
        private void WriteCollationRulesFromCustomSimple(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
        {
            Debug.Assert(writer != null);
            Debug.Assert(ws != null);
            Debug.Assert(ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.CustomSimple);

            var    parser = new SimpleRulesParser();
            string message;

            // avoid throwing exception, just don't save invalid data
            if (!parser.ValidateSimpleRules(ws.SortRules ?? string.Empty, out message))
            {
                return;
            }
            string icu = parser.ConvertToIcuRules(ws.SortRules ?? string.Empty);

            WriteCollationRulesFromIcuString(writer, reader, icu);
        }
Ejemplo n.º 48
0
 private void ReadCollationRulesForCustomICU(string collationXml, WritingSystemDefinitionV0 ws)
 {
     ws.SortRules = LdmlCollationParserV0.GetIcuRulesFromCollationNode(collationXml);
 }
		public void Migrate_OriginalFileContainsSystemCollationInfo_CollationInfoIsMigrated()
		{
			using (var environment = new TestEnvironment())
			{
				environment.WriteLdmlFile(
					"test.ldml",
					LdmlContentForTests.Version0WithSystemCollationInfo());
				var wsV0 = new WritingSystemDefinitionV0();
				new LdmlAdaptorV0().Read(environment.FilePath("test.ldml"), wsV0);

				var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback);
				migrator.Migrate();
				var repo = new TestLdmlInFolderWritingSystemRepository(environment.LdmlPath);
				migrator.ResetRemovedProperties(repo);

				WritingSystemDefinition ws = repo.Get("de");
				var scd = new SystemCollationDefinition {LanguageTag = "de"};
				Assert.That(ws.DefaultCollation.ValueEquals(scd), Is.True);
			}
		}
Ejemplo n.º 50
0
		public void Write(XmlWriter xmlWriter, WritingSystemDefinitionV0 ws, XmlReader xmlReader)
		{
			if (xmlWriter == null)
			{
				throw new ArgumentNullException("xmlWriter");
			}
			if (ws == null)
			{
				throw new ArgumentNullException("ws");
			}
			XmlReader reader = null;
			try
			{
				if (xmlReader != null)
				{
					var settings = new XmlReaderSettings
					{
						NameTable = _nameSpaceManager.NameTable,
						ConformanceLevel = ConformanceLevel.Auto,
						ValidationType = ValidationType.None,
						XmlResolver = null,
						DtdProcessing = DtdProcessing.Parse
					};
					reader = XmlReader.Create(xmlReader, settings);
				}
				WriteLdml(xmlWriter, reader, ws);
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
			}
		}
		public void Migrate_DateModified_IsLaterThanBeforeMigration()
		{
			using (var environment = new TestEnvironment())
			{
				environment.WriteLdmlFile(
					"test.ldml",
					LdmlContentForTests.Version0WithAllSortsOfDatathatdoesNotNeedSpecialAttention("", "", "", ""));

				var wsV0 = new WritingSystemDefinitionV0();
				new LdmlAdaptorV0().Read(environment.FilePath("test.ldml"), wsV0);
				DateTime dateBeforeMigration = wsV0.DateModified;

				var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback);
				migrator.Migrate();

				var wsV3 = new WritingSystemDefinition();
				new LdmlDataMapper(new TestWritingSystemFactory()).Read(environment.MappedFilePath("test.ldml"), wsV3);
				DateTime dateAfterMigration = wsV3.DateModified;
				Assert.IsTrue(dateAfterMigration > dateBeforeMigration);
			}
		}
Ejemplo n.º 52
0
		public void Write(string filePath, WritingSystemDefinitionV0 ws, Stream oldFile)
		{
			if (filePath == null)
			{
				throw new ArgumentNullException("filePath");
			}
			if (ws == null)
			{
				throw new ArgumentNullException("ws");
			}
			XmlReader reader = null;
			try
			{
				if (oldFile != null)
				{
					var readerSettings = new XmlReaderSettings
					{
						NameTable = _nameSpaceManager.NameTable,
						ConformanceLevel = ConformanceLevel.Auto,
						ValidationType = ValidationType.None,
						XmlResolver = null,
						DtdProcessing = DtdProcessing.Parse,
						IgnoreWhitespace = true
					};
					reader = XmlReader.Create(oldFile, readerSettings);
				}
				using (XmlWriter writer = XmlWriter.Create(filePath, CanonicalXmlSettings.CreateXmlWriterSettings()))
				{
					writer.WriteStartDocument();
					WriteLdml(writer, reader, ws);
					writer.Close();
				}
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
			}
		}
Ejemplo n.º 53
0
		private static void WriteVersion0Ldml(string language, string script, string territory, string variant, TempFile file)
		{
			//using a writing system V0 here because the real writing system can't cope with the way
			//flex encodes private-use language and shouldn't. But using a writing system + ldml adaptor
			//is the quickest way to generate ldml so I'm using it here.
			var ws = new WritingSystemDefinitionV0 {ISO639 = language, Script = script, Region = territory, Variant = variant};
			new LdmlAdaptorV0().Write(file.Path, ws, null);
		}
Ejemplo n.º 54
0
		private void WriteCollationElement(XmlWriter writer, XmlReader reader, WritingSystemDefinitionV0 ws)
		{
			Debug.Assert(writer != null);
			Debug.Assert(ws != null);
			bool needToCopy = reader != null && reader.NodeType == XmlNodeType.Element && reader.Name == "collation";
			if (needToCopy)
			{
				string collationType = reader.GetAttribute("type");
				needToCopy = String.IsNullOrEmpty(collationType) || collationType == "standard";
			}
			if (needToCopy && reader.IsEmptyElement)
			{
				reader.Skip();
				needToCopy = false;
			}

			if (ws.SortUsing == WritingSystemDefinitionV0.SortRulesType.DefaultOrdering && !needToCopy)
				return;

			if (needToCopy && reader.IsEmptyElement)
			{
				reader.Skip();
				needToCopy = false;
			}
			if (!needToCopy)
			{
				// set to null if we don't need to copy to make it easier to tell in the methods we call
				reader = null;
			}
			else
			{
				reader.ReadStartElement("collation");
				while (reader.NodeType == XmlNodeType.Attribute)
				{
					reader.Read();
				}
			}

			if (ws.SortUsing != WritingSystemDefinitionV0.SortRulesType.DefaultOrdering)
			{
				writer.WriteStartElement("collation");
				switch (ws.SortUsing)
				{
					case WritingSystemDefinitionV0.SortRulesType.OtherLanguage:
						WriteCollationRulesFromOtherLanguage(writer, reader, ws);
						break;
					case WritingSystemDefinitionV0.SortRulesType.CustomSimple:
						WriteCollationRulesFromCustomSimple(writer, reader, ws);
						break;
					case WritingSystemDefinitionV0.SortRulesType.CustomICU:
						WriteCollationRulesFromCustomIcu(writer, reader, ws);
						break;
					default:
						string message = string.Format("Unhandled SortRulesType '{0}' while writing LDML definition file.", ws.SortUsing);
						throw new ApplicationException(message);
				}
				WriteBeginSpecialElement(writer, "palaso");
				WriteSpecialValue(writer, "palaso", "sortRulesType", ws.SortUsing.ToString());
				writer.WriteEndElement();
				if (needToCopy)
				{
					if (FindElement(reader, "special"))
					{
						CopyOtherSpecialElements(writer, reader);
					}
					CopyToEndElement(writer, reader);
				}
				writer.WriteEndElement();
			}
			else if (needToCopy)
			{
				bool startElementWritten = false;
				if (FindElement(reader, "special"))
				{
					// write out any other special elements
					while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement
						&& (reader.NodeType != XmlNodeType.Element || reader.Name == "special"))
					{
						if (reader.NodeType == XmlNodeType.Element)
						{
							bool knownNS = IsKnownSpecialElement(reader);
							reader.MoveToElement();
							if (knownNS)
							{
								reader.Skip();
								continue;
							}
						}
						if (!startElementWritten)
						{
							writer.WriteStartElement("collation");
							startElementWritten = true;
						}
						writer.WriteNode(reader, false);
					}
				}

				if (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
				{
					// copy any other elements
					if (!startElementWritten)
					{
						writer.WriteStartElement("collation");
						startElementWritten = true;
					}
					CopyToEndElement(writer, reader);
				}
				if (startElementWritten)
					writer.WriteEndElement();
			}
		}