Example #1
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);
        }
Example #2
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 #3
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 #4
0
        private void _save(FileStream fs)
        {
            if (this._useBOM && Enum.IsDefined(typeof(SpecialCodePage), this._codePage))
            {
                byte[] bom = TextFile.SpecialCodePageInfo[(SpecialCodePage)this._codePage].Bom;
                if ((bom != null) && (bom.Length > 0))
                {
                    fs.Write(bom, 0, bom.Length);
                }
            }
            if (this._lines.Count > 0)
            {
                Encoding encoding = Encoding.GetEncoding(this._codePage);

                IO.TextLine lastLine = this._lines[this._lines.Count - 1];
                byte[]      newLine  = null;
                switch (this._lineEnding)
                {
                case IO.LineEndings.Windows:
                    newLine = new byte[] { (byte)'\r', (byte)'\n' };
                    break;

                case IO.LineEndings.Linux:
                    newLine = new byte[] { (byte)'\n' };
                    break;

                case IO.LineEndings.Mac:
                    newLine = new byte[] { (byte)'\r' };
                    break;
                }
                foreach (IO.TextLine line in this._lines)
                {
                    if (line.Value.Length > 0)
                    {
                        byte[] buf = encoding.GetBytes(line.Value);
                        fs.Write(buf, 0, buf.Length);
                    }
                    if (line != lastLine)
                    {
                        fs.Write(newLine, 0, newLine.Length);
                    }
                }
            }
            fs.Flush();
        }
Example #5
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 #6
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 #7
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 #8
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 #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
 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;
 }