Example #1
0
        /// <summary>Parses an xliff element.</summary>
        /// <param name="xliff">An XML element that is supposed to be an xliff element.</param>
        /// <returns>List of assets.</returns>
        public IEnumerable <IAsset> Parse(XElement xliff)
        {
            // Check if it is an XLIFF document.
            var X = xliff.Name.Namespace;

            if (X != XliffAsset.XLIFF && X != XNamespace.None)
            {
                return(null);
            }
            if (xliff.Name.LocalName != "xliff")
            {
                return(null);
            }

            // OK.  It seems an XLIFF.  Try to detect a flavour if set to Auto.
            if (Flavour == XliffReader.Flavour.Auto)
            {
                Flavour = DetectFlavour(xliff);
            }

            return(xliff.Elements(X + "file").Select(CreateAsset).ToArray());
        }
Example #2
0
        public IAssetBundle Read(Stream stream)
        {
            XElement xliff;

            try
            {
                // I experienced that some import filter (used with some CAT software)
                // produces some illegal entities, e.g., "&#x1F;".
                // Although it is NOT a wellformed XML in theory, we need to take care of them.
                // Another issue is that some XML file includes DOCTYPE declaration
                // with a system identifier,
                // that XmlReader tries to access to to get a DTD,
                // so we need to instruct explicitly not to do so.
                var settings = new XmlReaderSettings()
                {
                    CheckCharacters  = false,
                    IgnoreWhitespace = false,
                    DtdProcessing    = DtdProcessing.Ignore,
                    XmlResolver      = null,
                    CloseInput       = true
                };
                using (var rd = XmlReader.Create(stream, settings))
                {
                    xliff = XElement.Load(rd);
                }
            }
            catch (XmlException)
            {
                // This is usually thrown when the given file was not an XML document,
                // or it was meant to be an XML but contained some error.
                // The first case is normal, since we try to parse everything as an XML.
                // The latter case needs diagnostic,
                // but I don't know how I can identify the cases.
                // We need some diagnostic logging feature.  FIXME.
                return(null);
            }
            catch (IOException)
            {
                // This must be an indication of an issue in the underlying file access.
                throw;
            }
            catch (Exception)
            {
                // If we get an exception of other type,
                // I believe it is an indication of some unexpected error.
                // However, the API document of XElement.Load(Strream) is too vague,
                // and I don't know what we should do.
                return(null);
            }

            // We are probably seeing a wellformed XML document.
            // Check if it is an XLIFF document.
            var X = xliff.Name.Namespace;

            if (X != XliffAsset.XLIFF && X != XNamespace.None)
            {
                return(null);
            }
            if (xliff.Name.LocalName != "xliff")
            {
                return(null);
            }

            // OK.  It seems an XLIFF.  Try to detect a flavour if set to Auto.
            if (Flavour == XliffReader.Flavour.Auto)
            {
                Flavour = DetectFlavour(xliff);
            }

            return(new SimpleAssetBundle(xliff.Elements(X + "file").Select(CreateAsset).ToArray(), ReaderManager.FriendlyFilename(Filename)));
        }