Beispiel #1
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));
            }
        }
Beispiel #2
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;
 }
Beispiel #3
0
 internal static DataLine CreateNew(PO.Entry entry, DataLine.Kinds kind, int? index, bool isContinuation, IO.TextLine addRelativeTo, bool addRelativeToBefore, string text)
 {
     if (entry == null)
         throw new ArgumentNullException("entry");
     if (addRelativeTo == null)
         throw new ArgumentNullException("addRelativeTo");
     int insertAt = entry.File.TextFile.Lines.IndexOf(addRelativeTo);
     if (insertAt < 0)
         throw new ArgumentOutOfRangeException("addRelativeTo");
     if (!addRelativeToBefore)
         insertAt++;
     IO.TextLine textLine = new IO.TextLine(BuildTextFileData(kind, index, isContinuation, text));
     entry.File.TextFile.Lines.Insert(insertAt, textLine);
     DataLine result;
     if (index.HasValue)
         result = new IndexedDataLine(entry.File, textLine, isContinuation, kind, text, index.Value);
     else
         result = new DataLine(entry.File, textLine, isContinuation, kind, text);
     entry.LineAdded(result);
     return result;
 }
Beispiel #4
0
 private static string BuildTextFileData(DataLine.Kinds kind, int? index, bool isContinuation, string text)
 {
     StringBuilder res = new StringBuilder();
     if (!isContinuation)
     {
         switch (kind)
         {
             case Kinds.Context:
                 res.Append("msgctxt ");
                 break;
             case Kinds.ID:
                 res.Append("msgid ");
                 break;
             case Kinds.IDPlural:
                 res.Append("msgid_plural ");
                 break;
             case Kinds.Translated:
                 res.Append("msgstr ");
                 break;
             case Kinds.TranslatedIndexed:
                 res.Append("msgstr[").Append(index.Value).Append("] ");
                 break;
             default:
                 throw new NotImplementedException();
         }
     }
     res.Append('"');
     if (!string.IsNullOrEmpty(text))
         res.Append(text);
     res.Append('"');
     return res.ToString();
 }
Beispiel #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;
 }