public ResXFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("File not found or invalid filename specified");
            }
            FilePath = filePath;
            Entries  = new List <ResourceFileEntry>();

            try
            {
                XDocument xDoc      = XDocument.Load(filePath, LoadOptions.SetLineInfo);
                var       resxNodes = from x in xDoc.Descendants("data")
                                      where x.Attribute("mimetype") == null
                                      select new ResourceFileEntry(
                    (string)x.Attribute("name"),                                                                                                // ResourceId.
                    (string)x.Descendants("value").FirstOrDefault(),                                                                            // Value.
                    ((IXmlLineInfo)x.Attribute("name")).LineNumber,                                                                             // Start Line.
                    ((IXmlLineInfo)x.NextNode)?.LineNumber - 1 ?? ((IXmlLineInfo)x.LastNode).LineNumber + 1,                                    // End Line.
                    filePath,                                                                                                                   // FilePath.
                    x.Descendants("comment").FirstOrDefault()?.Value == null ? string.Empty : (string)x.Descendants("comment").FirstOrDefault() // Comments.
                    );

                Entries = resxNodes.ToList();

                // Need to identify duplicated ResourceId values and make them unique.
                ParserUtils.MakeResourceIdUnique(Entries, true);

                // Need to identify duplicated SourceString values and make them unique.
                ParserUtils.MakeSourceStringUnique(Entries, true);
            }
            catch (Exception ex)
            {
                throw new Exception("Error parsing .resx file: " + ex.Message);
            }
        }
Beispiel #2
0
        public AppleStringsFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("File not found or invalid filename specified");
            }
            FilePath = filePath;
            Entries  = new List <ResourceFileEntry>();
            var    parsedFile         = File.ReadAllLines(filePath);
            string currentComment     = string.Empty;
            int    startLine          = 0;
            int    endLine            = 0;
            int    currentLine        = 0;
            bool   cStyleCommentBlock = false;

            foreach (string line in parsedFile)
            {
                currentLine++;
                try
                {
                    if (line.StartsWith("#"))
                    {
                        // Ignore simple comment lines
                    }

                    // Comments
                    else if (line.StartsWith("/*"))
                    {
                        startLine       = currentLine;
                        currentComment += line.Substring(line.IndexOf("/*") + 2);
                        // Single-line comment
                        if (line.EndsWith("*/"))
                        {
                            currentComment = currentComment.TrimEnd(new char[] { '*', '/' });
                        }
                        // Multi-line comment
                        else
                        {
                            cStyleCommentBlock = true;
                        }
                    }
                    else if (cStyleCommentBlock)
                    {
                        currentComment += line;
                        if (line.EndsWith("*/"))
                        {
                            currentComment     = currentComment.TrimEnd(new char[] { '*', '/' });
                            cStyleCommentBlock = false;
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(line) && line.Contains('='))
                    {
                        int indexOfEquals = line.IndexOf('=');

                        endLine = currentLine;
                        if (currentComment.Length == 0)
                        {
                            startLine = currentLine;
                        }

                        // Remove any leading and trailing whitespace from comment
                        currentComment = currentComment.Trim();

                        // Parse resourceID
                        string resourceID = line.Substring(0, indexOfEquals).Trim();

                        // Parse string value
                        string value = line.Substring(line.IndexOf('=') + 1, line.Length - (line.IndexOf('=') + 1)).Trim();

                        string devComment = currentComment;

                        Entries.Add(new ResourceFileEntry(resourceID, value, startLine, endLine, filePath, devComment));

                        currentComment = string.Empty;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error parsing Apple .strings file", ex);
                }
            }

            // Need to identify duplicated ResourceId values and make them unique.
            ParserUtils.MakeResourceIdUnique(Entries, true);

            // Need to identify duplicated SourceString values and append a Tag and Index to the ResourceId.
            ParserUtils.MakeSourceStringUnique(Entries, true);
        }