Example #1
0
 protected Line(POFile file, IO.TextLine source, Line.Types type, bool isContinuation, string text)
 {
     this._file           = file;
     this._source         = source;
     this._type           = type;
     this._isContinuation = isContinuation;
     this._text           = text;
 }
Example #2
0
 public static Line ParseLine(POFile file, IO.TextLine source, Line previous)
 {
     if (source.Value[0] == '#')
     {
         return(CommentLine.Parse(file, source, previous));
     }
     else
     {
         return(DataLine.Parse(file, source, previous));
     }
 }
Example #3
0
 internal IndexedDataLine(POFile file, IO.TextLine source, bool isContinuation, DataLine.Kinds kind, string text, int index)
     : base(file, source, isContinuation, kind, text)
 {
     this._index = index;
 }
Example #4
0
        public static DataLine Parse(POFile file, IO.TextLine source, Line previous)
        {
            Match match;

            bool isContinuation;

            DataLine.Kinds?kind;
            string         text;
            int?           index;

            string sourceValue = source.Value;

            if ((match = DataLine.RxContinuation.Match(sourceValue)).Success)
            {
                if (previous == null)
                {
                    throw new Exception("Data continuation at block start.");
                }
                if (previous.Type != Line.Types.Data)
                {
                    throw new Exception("Data continuation after a non-data line.");
                }
                DataLine prevData = (DataLine)previous;
                isContinuation = true;
                kind           = prevData._kind;
                text           = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevData as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else
            {
                isContinuation = false;
                kind           = null;
                text           = null;
                index          = null;
                foreach (KeyValuePair <DataLine.Kinds, Regex> kv in DataLine.RxFindKind)
                {
                    match = kv.Value.Match(sourceValue);
                    if (!match.Success)
                    {
                        continue;
                    }
                    kind  = kv.Key;
                    text  = match.Groups["text"].Value;
                    index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null;
                    break;
                }
            }
            if (!kind.HasValue)
            {
                throw new Exception("Unknown line kind.");
            }
            if (index.HasValue)
            {
                return(new IndexedDataLine(file, source, isContinuation, kind.Value, text, index.Value));
            }
            else
            {
                return(new DataLine(file, source, isContinuation, kind.Value, text));
            }
        }
Example #5
0
 protected DataLine(POFile file, IO.TextLine source, bool isContinuation, DataLine.Kinds kind, string text)
     : base(file, source, Line.Types.Data, isContinuation, text)
 {
     this._kind = kind;
 }
Example #6
0
 public static Line ParseLine(POFile file, IO.TextLine source, Line previous)
 {
     if (source.Value[0] == '#')
         return CommentLine.Parse(file, source, previous);
     else
         return DataLine.Parse(file, source, previous);
 }
Example #7
0
        public static CommentLine Parse(POFile file, IO.TextLine source, Line previous)
        {
            Match match;

            bool isContinuation;

            CommentLine.Kinds?kind;
            string            text;
            int?index;

            string sourceValue = source.Value;

            if ((match = CommentLine.RxContinuation_PreviousUntraslated.Match(sourceValue)).Success)
            {
                if (previous == null)
                {
                    throw new Exception("Comment of kind 'previous-untranslated' continuation at block start.");
                }
                if (previous.Type != Line.Types.Comment)
                {
                    throw new Exception("Comment of kind 'previous-untranslated' continuation after a non-comment line.");
                }
                CommentLine prevComment = (CommentLine)previous;
                switch (prevComment._kind)
                {
                case CommentLine.Kinds.PreviousUntraslated_Context:
                case CommentLine.Kinds.PreviousUntraslated_ID:
                case CommentLine.Kinds.PreviousUntraslated_IDPlural:
                    break;

                default:
                    throw new Exception(string.Format("Comment of kind 'previous-untranslated' continuation after a comment of kind '{0}'.", prevComment._kind));
                }
                isContinuation = true;
                kind           = prevComment._kind;
                text           = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevComment as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else if ((match = CommentLine.RxContinuation_Removed.Match(sourceValue)).Success)
            {
                if (previous == null)
                {
                    throw new Exception("Comment of kind 'removed' continuation at block start.");
                }
                if (previous.Type != Line.Types.Comment)
                {
                    throw new Exception("Comment of kind 'removed' continuation after a non-comment line.");
                }
                CommentLine prevComment = (CommentLine)previous;
                switch (prevComment._kind)
                {
                case CommentLine.Kinds.Removed_Context:
                case CommentLine.Kinds.Removed_ID:
                case CommentLine.Kinds.Removed_IDPlural:
                case CommentLine.Kinds.Removed_Translated:
                case CommentLine.Kinds.Removed_TranslatedIndexed:
                    break;

                default:
                    throw new Exception(string.Format("Comment of kind 'removed' continuation after a comment of kind '{0}'.", prevComment._kind));
                }
                isContinuation = true;
                kind           = prevComment._kind;
                text           = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevComment as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else
            {
                isContinuation = false;
                kind           = null;
                text           = null;
                index          = null;
                foreach (KeyValuePair <CommentLine.Kinds, Regex> kv in CommentLine.RxFindKind)
                {
                    match = kv.Value.Match(sourceValue);
                    if (!match.Success)
                    {
                        continue;
                    }
                    kind  = kv.Key;
                    text  = match.Groups["text"].Value;
                    index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null;
                    break;
                }
            }
            if (index.HasValue)
            {
                return(new IndexedCommentLine(file, source, isContinuation, kind.Value, text, index.Value));
            }
            else
            {
                return(new CommentLine(file, source, isContinuation, kind.Value, text));
            }
        }
Example #8
0
 internal IndexedDataLine(POFile file, IO.TextLine source, bool isContinuation, DataLine.Kinds kind, string text, int index)
     : base(file, source, isContinuation, kind, text)
 {
     this._index = index;
 }
Example #9
0
 protected Line(POFile file, IO.TextLine source, Line.Types type, bool isContinuation, string text)
 {
     this._file = file;
     this._source = source;
     this._type = type;
     this._isContinuation = isContinuation;
     this._text = text;
 }
Example #10
0
        public static DataLine Parse(POFile file, IO.TextLine source, Line previous)
        {
            Match match;

            bool isContinuation;
            DataLine.Kinds? kind;
            string text;
            int? index;

            string sourceValue = source.Value;
            if ((match = DataLine.RxContinuation.Match(sourceValue)).Success)
            {
                if (previous == null)
                    throw new Exception("Data continuation at block start.");
                if (previous.Type != Line.Types.Data)
                    throw new Exception("Data continuation after a non-data line.");
                DataLine prevData = (DataLine)previous;
                isContinuation = true;
                kind = prevData._kind;
                text = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevData as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else
            {
                isContinuation = false;
                kind = null;
                text = null;
                index = null;
                foreach (KeyValuePair<DataLine.Kinds, Regex> kv in DataLine.RxFindKind)
                {
                    match = kv.Value.Match(sourceValue);
                    if (!match.Success)
                        continue;
                    kind = kv.Key;
                    text = match.Groups["text"].Value;
                    index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null;
                    break;
                }
            }
            if (!kind.HasValue)
                throw new Exception("Unknown line kind.");
            if (index.HasValue)
                return new IndexedDataLine(file, source, isContinuation, kind.Value, text, index.Value);
            else
                return new DataLine(file, source, isContinuation, kind.Value, text);
        }
Example #11
0
 protected DataLine(POFile file, IO.TextLine source, bool isContinuation, DataLine.Kinds kind, string text)
     : base(file, source, Line.Types.Data, isContinuation, text)
 {
     this._kind = kind;
 }
Example #12
0
        public static CommentLine Parse(POFile file, IO.TextLine source, Line previous)
        {
            Match match;

            bool isContinuation;
            CommentLine.Kinds? kind;
            string text;
            int? index;

            string sourceValue = source.Value;
            if ((match = CommentLine.RxContinuation_PreviousUntraslated.Match(sourceValue)).Success)
            {
                if (previous == null)
                    throw new Exception("Comment of kind 'previous-untranslated' continuation at block start.");
                if (previous.Type != Line.Types.Comment)
                    throw new Exception("Comment of kind 'previous-untranslated' continuation after a non-comment line.");
                CommentLine prevComment = (CommentLine)previous;
                switch (prevComment._kind)
                {
                    case CommentLine.Kinds.PreviousUntraslated_Context:
                    case CommentLine.Kinds.PreviousUntraslated_ID:
                    case CommentLine.Kinds.PreviousUntraslated_IDPlural:
                        break;
                    default:
                        throw new Exception(string.Format("Comment of kind 'previous-untranslated' continuation after a comment of kind '{0}'.", prevComment._kind));
                }
                isContinuation = true;
                kind = prevComment._kind;
                text = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevComment as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else if ((match = CommentLine.RxContinuation_Removed.Match(sourceValue)).Success)
            {
                if (previous == null)
                    throw new Exception("Comment of kind 'removed' continuation at block start.");
                if (previous.Type != Line.Types.Comment)
                    throw new Exception("Comment of kind 'removed' continuation after a non-comment line.");
                CommentLine prevComment = (CommentLine)previous;
                switch (prevComment._kind)
                {
                    case CommentLine.Kinds.Removed_Context:
                    case CommentLine.Kinds.Removed_ID:
                    case CommentLine.Kinds.Removed_IDPlural:
                    case CommentLine.Kinds.Removed_Translated:
                    case CommentLine.Kinds.Removed_TranslatedIndexed:
                        break;
                    default:
                        throw new Exception(string.Format("Comment of kind 'removed' continuation after a comment of kind '{0}'.", prevComment._kind));
                }
                isContinuation = true;
                kind = prevComment._kind;
                text = match.Groups["text"].Value;
                Line.IIndexedLine idx = prevComment as Line.IIndexedLine;
                index = (idx != null) ? idx.Index : (int?)null;
            }
            else
            {
                isContinuation = false;
                kind = null;
                text = null;
                index = null;
                foreach (KeyValuePair<CommentLine.Kinds, Regex> kv in CommentLine.RxFindKind)
                {
                    match = kv.Value.Match(sourceValue);
                    if (!match.Success)
                        continue;
                    kind = kv.Key;
                    text = match.Groups["text"].Value;
                    index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null;
                    break;
                }
            }
            if (index.HasValue)
                return new IndexedCommentLine(file, source, isContinuation, kind.Value, text, index.Value);
            else
                return new CommentLine(file, source, isContinuation, kind.Value, text);
        }
Example #13
0
        public static Entry Parse(bool isTheFirstOne, POFile file, List<IO.TextLine> textLines)
        {
            List<PO.Line> lines = new List<PO.Line>(textLines.Count);
            PO.Line prevLine = null;
            foreach (IO.TextLine textLine in textLines)
            {
                try
                {
                    lines.Add(prevLine = PO.Line.ParseLine(file, textLine, prevLine));
                }
                catch (Exception x)
                {
                    Exception inner = x;
                    while (inner.InnerException != null)
                        inner = inner.InnerException;
                    throw new Exception(string.Format("Error at line {1}{0}{2}{0}Error message: {3}", Environment.NewLine, file.TextFile.Lines.IndexOf(textLine) + 1, textLine.Value, inner.Message));
                }
            }
            try
            {
                if (isTheFirstOne)
                {
                    List<PO.Line> linesTester = new List<PO.Line>(lines.Count);
                    foreach (PO.Line l in lines)
                    {
                        bool skip = false;
                        if (l.Type == Line.Types.Comment)
                        {
                            switch (((PO.CommentLine)l).Kind)
                            {
                                case CommentLine.Kinds.Flags:
                                case CommentLine.Kinds.TranslatorComment:
                                    skip = true;
                                    break;
                            }
                        }
                        if (!skip)
                            linesTester.Add(l);
                    }
                    if (
                        (linesTester.Count >= 2)
                        &&
                        (!linesTester[0].IsContinuation)
                        &&
                        ((linesTester[0] as Line.IIndexedLine) == null)
                        &&
                        (linesTester[0] is DataLine)
                        &&
                        (((DataLine)linesTester[0]).Kind == DataLine.Kinds.ID)
                        &&
                        (!linesTester[1].IsContinuation)
                        &&
                        ((linesTester[1] as Line.IIndexedLine) == null)
                        &&
                        (linesTester[1] is DataLine)
                        &&
                        (((DataLine)linesTester[1]).Kind == DataLine.Kinds.Translated)
                        )
                    {
                        bool ok = true;
                        for (int i = 2; ok && (i < linesTester.Count); i++)
                            if (!linesTester[i].IsContinuation)
                                ok = false;
                        if (ok)
                            return new HeaderEntry(lines);
                    }
                }
                bool hasData = false, hasPluralData = false, hasRemoved = false, hasPluralRemoved = false;
                foreach (PO.Line line in lines)
                {
                    switch (line.Type)
                    {
                        case Line.Types.Comment:
                            {
                                CommentLine.Kinds kind = ((CommentLine)line).Kind;
                                switch (kind)
                                {
                                    case CommentLine.Kinds.Removed_Context:
                                    case CommentLine.Kinds.Removed_ID:
                                    case CommentLine.Kinds.Removed_IDPlural:
                                    case CommentLine.Kinds.Removed_Translated:
                                    case CommentLine.Kinds.Removed_TranslatedIndexed:
                                        hasRemoved = true;
                                        switch (kind)
                                        {
                                            case CommentLine.Kinds.Removed_IDPlural:
                                            case CommentLine.Kinds.Removed_TranslatedIndexed:
                                                hasPluralRemoved = true;
                                                break;

                                        }
                                        break;
                                }
                            }
                            break;
                        case Line.Types.Data:
                            {
                                hasData = true;
                                DataLine.Kinds kind = ((DataLine)line).Kind;
                                switch (kind)
                                {
                                    case DataLine.Kinds.IDPlural:
                                    case DataLine.Kinds.TranslatedIndexed:
                                        hasPluralData = true;
                                        break;
                                }
                            }
                            break;
                    }
                }
                if (hasData)
                {
                    if (hasRemoved)
                        throw new Exception("Found both removed and standard lines.");
                    if (hasPluralData)
                        return new PluralDataEntry(lines);
                    else
                        return new SingleDataEntry(lines);
                }
                else if (hasRemoved)
                {
                    if (hasPluralRemoved)
                        return new PluralRemovedEntry(lines);
                    else
                        return new SingleRemovedEntry(lines);
                }
                else
                    throw new Exception("Unknown entry kind.");
            }
            catch (Exception x)
            {
                Exception inner = x;
                while (inner.InnerException != null)
                    inner = inner.InnerException;
                throw new Exception(string.Format("Error at lines {1}~{2}:{0}{3}", Environment.NewLine, file.TextFile.Lines.IndexOf(textLines[0]) + 1, file.TextFile.Lines.IndexOf(textLines[textLines.Count - 1]) + 1, inner.Message));

            }
        }
Example #14
0
        internal Entry(List<PO.Line> lines)
        {
            StringBuilder sb;

            if ((lines == null) || (lines.Count == 0))
                throw new ArgumentNullException("lines");
            this._lines = lines;
            this._file = this._lines[0].File;
            this._translatorCommentLines = new List<PO.CommentLine>();
            this._extractedCommentLines = new List<PO.CommentLine>();
            this._referenceLines = new List<PO.CommentLine>();
            this._flagsLines = new List<PO.CommentLine>();
            this._previousUntranslatedContextLines = new List<PO.CommentLine>();
            this._previousUntranslatedStringLines = new List<PO.CommentLine>();
            this._previousUntranslatedStringPluralLines = new List<PO.CommentLine>();
            PO.Line prevLine = null;
            foreach (PO.Line line in this._lines)
            {
                switch (line.Type)
                {
                    case Line.Types.Comment:
                        if ((prevLine != null) && (prevLine.Type == Line.Types.Data))
                            throw new Exception("Comment line after data line.");
                        PO.CommentLine comment = (PO.CommentLine)line;
                        switch (comment.Kind)
                        {
                            case CommentLine.Kinds.TranslatorComment:
                                this._translatorCommentLines.Add(comment);
                                break;
                            case CommentLine.Kinds.ExtractedComment:
                                this._extractedCommentLines.Add(comment);
                                break;
                            case CommentLine.Kinds.Reference:
                                this._referenceLines.Add(comment);
                                break;
                            case CommentLine.Kinds.Flags:
                                this._flagsLines.Add(comment);
                                break;
                            case CommentLine.Kinds.PreviousUntraslated_Context:
                                if ((this._previousUntranslatedContextLines.Count > 0))
                                {
                                    if (!comment.IsContinuation)
                                        throw new Exception("Previous untranslated context started more than once.");
                                }
                                this._previousUntranslatedContextLines.Add(comment);
                                break;
                            case CommentLine.Kinds.PreviousUntraslated_ID:
                                if (this._previousUntranslatedStringLines.Count > 0)
                                {
                                    if (!comment.IsContinuation)
                                        throw new Exception("Previous untranslated string started more than once.");
                                }
                                this._previousUntranslatedStringLines.Add(comment);
                                break;
                            case CommentLine.Kinds.PreviousUntraslated_IDPlural:
                                if (this._previousUntranslatedStringPluralLines.Count > 0)
                                {
                                    if (!comment.IsContinuation)
                                        throw new Exception("Previous untranslated plural string started more than once.");
                                }
                                this._previousUntranslatedStringPluralLines.Add(comment);
                                break;
                        }
                        break;
                }
                prevLine = line;
            }

            sb = null;
            foreach (PO.CommentLine comment in this._translatorCommentLines)
            {
                if (sb == null)
                    sb = new StringBuilder();
                else
                    sb.AppendLine();
                sb.Append(comment.Text);
            }
            this._translatorComment = (sb == null) ? "" : sb.ToString();

            sb = null;
            foreach (PO.CommentLine comment in this._extractedCommentLines)
            {
                if (sb == null)
                    sb = new StringBuilder();
                else
                    sb.AppendLine();
                sb.Append(comment.Text);
            }
            this._extractedComment = (sb == null) ? "" : sb.ToString();

            sb = null;
            foreach (PO.CommentLine comment in this._referenceLines)
            {
                if (sb == null)
                    sb = new StringBuilder();
                else
                    sb.Append(' ');
                sb.Append(comment.Text);
            }
            if (sb == null)
            {
                this._reference = new string[] { };
            }
            else
            {
                string s = sb.ToString().Trim(' ', '\t', '\r', '\n');
                if (s.Length == 0)
                    this._reference = new string[] { };
                else if (s.IndexOf('"') < 0)
                    this._reference = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                else
                {
                    List<string> reference = new List<string>();
                    while (s.Length > 0)
                    {
                        int p = s.IndexOf('"');
                        if (p < 0)
                        {
                            reference.AddRange(s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                            s = "";
                        }
                        else
                        {
                            string s2;
                            int q = s.IndexOf('"', p + 1);
                            if (q < 0)
                                throw new Exception("Malformed reference.");
                            s2 = (p == 0) ? "" : s.Substring(0, p).TrimStart(' ', '\t', '\r', '\n');
                            if (s2.Length > 0)
                                reference.AddRange(s2.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                            s2 = (q == (p + 1)) ? "" : s.Substring(p + 1, q - p - 1).Trim(' ', '\t', '\r', '\n');
                            if (s2.Length > 0)
                                reference.AddRange(s2.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                            s = (q == (s.Length - 1)) ? "" : s.Substring(q + 1).TrimStart(' ', '\t', '\r', '\n');
                        }
                    }
                    this._reference = reference.ToArray();
                }
            }

            sb = null;
            foreach (PO.CommentLine comment in this._flagsLines)
            {
                if (sb == null)
                    sb = new StringBuilder();
                else
                    sb.Append(',');
                sb.Append(comment.Text.Trim(' ', '\t'));
            }
            this._flags = Flag.None;
            if (sb != null)
            {
                foreach (string flag in sb.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string flagTrimmed = flag.Trim();
                    Flag? found = null;
                    foreach (KeyValuePair<Flag, FlagInfoAttribute> kv in Entry.FlagInfo)
                    {
                        if (flagTrimmed.Equals(kv.Value.Text, StringComparison.Ordinal))
                        {
                            found = kv.Key;
                            break;
                        }
                    }
                    if (!found.HasValue)
                    {
                        throw new Exception(string.Format("Unknown flag: {0}", flagTrimmed));
                    }
                    this._flags |= found.Value;
                }
            }

            if (this._previousUntranslatedContextLines.Count > 0)
            {
                if (this._previousUntranslatedStringLines.Count == 0)
                    throw new Exception("Previous untranslated context without untranslated text.");
            }
            if (this._previousUntranslatedStringPluralLines.Count > 0)
            {
                if (this._previousUntranslatedStringLines.Count == 0)
                    throw new Exception("Plural untranslated text without singular untranslated text.");
            }

            sb = new StringBuilder();
            foreach (PO.CommentLine comment in this._previousUntranslatedContextLines)
            {
                sb.Append(Entry.UnescapeText(comment.Text));
            }
            this._previousUntranslatedContext = sb.ToString();

            sb = new StringBuilder();
            foreach (PO.CommentLine comment in this._previousUntranslatedStringLines)
            {
                sb.Append(Entry.UnescapeText(comment.Text));
            }
            this._previousUntranslatedString = sb.ToString();

            sb = new StringBuilder();
            foreach (PO.CommentLine comment in this._previousUntranslatedStringPluralLines)
            {
                sb.Append(Entry.UnescapeText(comment.Text));
            }
            this._previousUntranslatedStringPlural = sb.ToString();
        }