/// <summary>
        /// Initializes a new instance of the <see cref="ElementInfo"/> class.
        /// </summary>
        /// <param name="name">The Xml name of the element.</param>
        /// <param name="element">The element this class describes.</param>
        public ElementInfo(XmlNameInfo name, XliffElement element)
            : base(name)
        {
            ArgValidator.Create(element, "element").IsNotNull();

            this.Element = element;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SchemaEntityAttribute"/> class.
        /// </summary>
        /// <param name="prefix">The prefix of the Xml attribute.</param>
        /// <param name="ns">The namespace of the Xml attribute.</param>
        /// <param name="localName">The local name of the Xml attribute.</param>
        /// <param name="requirement">Indicates whether the attribute is required in the XLIFF document.</param>
        public SchemaEntityAttribute(string prefix, string ns, string localName, Requirement requirement)
        {
            ArgValidator.Create(localName, "localName").IsNotNullOrWhitespace();

            this.Name        = new XmlNameInfo(prefix, ns, localName);
            this.Requirement = requirement;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericExtension"/> class.
        /// </summary>
        /// <param name="name">The name of the extension.</param>
        public GenericExtension(string name)
        {
            ArgValidator.Create(name, "name").IsNotNullOrWhitespace();

            this.attributes = new Lazy <List <IExtensionAttribute> >();
            this.children   = new Lazy <List <ElementInfo> >();
            this.Name       = name;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SchemaChildAttribute"/> class.
        /// </summary>
        /// <param name="prefix">The prefix of the XLIFF fragment.</param>
        /// <param name="ns">The namespace of the XLIFF fragment.</param>
        /// <param name="localName">The local name of the XLIFF fragment.</param>
        /// <param name="type">The the type of the class associated with the XLIFF element.</param>
        public SchemaChildAttribute(string prefix, string ns, string localName, Type type)
        {
            // The value of localName may be null if the child is the inner text (ex. PlainText).
            ArgValidator.Create(localName, "name").IsNotWhitespaceOnly();
            ArgValidator.Create(type, "type").IsNotNull();

            this.ChildType = type;
            this.Name      = new XmlNameInfo(prefix, ns, localName);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ExtensionNameInfo"/> class.
        /// </summary>
        /// <param name="prefix">The Xml prefix of the member used when writing information to a file.</param>
        /// <param name="ns">The namespace of the member.</param>
        /// <param name="localName">The local name of the member.</param>
        public ExtensionNameInfo(string prefix, string ns, string localName)
        {
            ArgValidator.Create(ns, "ns").IsNotNullOrWhitespace();
            ArgValidator.Create(localName, "localName").IsNotNullOrWhitespace();

            this.LocalName = localName;
            this.Namespace = ns;
            this.Prefix    = prefix;
        }
Exemple #6
0
        /// <summary>
        /// Deserializes XLIFF content from a stream.
        /// </summary>
        /// <param name="stream">The stream to deserialize.</param>
        /// <returns>The <see cref="XliffDocument"/> that defines the root of the document.</returns>
        public XliffDocument Deserialize(Stream stream)
        {
            ArgValidator.Create(stream, "stream").IsNotNull();

            using (XmlReader reader = XmlReader.Create(stream))
            {
                return(this.Deserialize(reader));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InheritValueAttribute"/> class.
        /// </summary>
        /// <param name="inheritance">The type of inheritance.</param>
        /// <param name="ancestorType">The type of the ancestor to inherit from.</param>
        /// <param name="propertyName">The name of the property to get from the ancestor.</param>
        public InheritValueAttribute(Inheritance inheritance, Type ancestorType, string propertyName)
        {
            if (inheritance == Inheritance.AncestorType)
            {
                ArgValidator.Create(ancestorType, "ancestorType").IsNotNull();
            }

            this.AncestorPropertyName = propertyName;
            this.AncestorType         = ancestorType;
            this.Inheritance          = inheritance;
        }
        /// <summary>
        /// Converts a dictionary whose key and value are strings to a string where a comma delimits the key from the
        /// value, and a backslash delimits entries.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representation of the value that is used by XLIFF.</returns>
        public string Convert(object value)
        {
            IDictionary <string, string> styles;
            StringBuilder builder;

            ArgValidator.Create(value, "value").IsNotNull().IsOfType(typeof(IDictionary <string, string>));

            styles  = (IDictionary <string, string>)value;
            builder = new StringBuilder();

            foreach (KeyValuePair <string, string> pair in styles)
            {
                string entry;
                string entryKey;
                string entryValue;

                try
                {
                    XmlConvert.VerifyName(pair.Key);
                }
                catch (Exception e)
                {
                    string message;

                    message = string.Format(Properties.Resources.SubFormatStyleConverter_InvalidKey_Format, pair.Key);
                    throw new FormatException(message, e);
                }

                entryKey = pair.Key.Replace(
                    SubFormatStyleConverter.BackslashString,
                    SubFormatStyleConverter.EscapedBackslash)
                           .Replace(
                    SubFormatStyleConverter.CommaString,
                    SubFormatStyleConverter.EscapedComma);

                entryValue = pair.Value.Replace(
                    SubFormatStyleConverter.BackslashString,
                    SubFormatStyleConverter.EscapedBackslash)
                             .Replace(
                    SubFormatStyleConverter.CommaString,
                    SubFormatStyleConverter.EscapedComma);

                entry = string.Join(SubFormatStyleConverter.CommaString, entryKey, entryValue);
                builder.Append(entry + SubFormatStyleConverter.BackslashString);
            }

            // Remove the trailing backslash.
            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            return(builder.Length > 0 ? builder.ToString() : null);
        }
Exemple #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XliffReader"/> class.
        /// </summary>
        /// <param name="settings">Settings that determine how to read content.</param>
        public XliffReader(XliffReaderSettings settings)
        {
            GenericExtensionHandler defaultHandler;

            ArgValidator.Create(settings, "settings").IsNotNull();

            this.elementStack = new Stack <ElementState>();
            this.handlers     = new Lazy <Dictionary <string, IExtensionHandler> >();
            this.settings     = settings;

            defaultHandler = new GenericExtensionHandler();
            this.RegisterExtensionHandler(XliffReader.DefaultHandlerKey, defaultHandler);
        }
        /// <summary>
        /// Selects an item matching the selection query.
        /// </summary>
        /// <param name="path">The selection query.</param>
        /// <returns>The object that was selected from the query path, or null if no match was found.</returns>
        /// <example>The value of <paramref name="path"/> might look something like "#g=group1/f=file1/u=unit1/n=note1"
        /// which is a full path from the document root.</example>
        public ISelectable Select(string path)
        {
            string selectionPath;

            ArgValidator.Create(path, "path").IsNotNull().StartsWith(Utilities.Constants.SelectorPathIndictator);

            selectionPath = Utilities.RemoveSelectorIndicator(path);
            if ((selectionPath.Length > 0) && (selectionPath[0] == Utilities.Constants.SelectorPathSeparator))
            {
                // Strip the leading '/'.
                selectionPath = selectionPath.Substring(1);
            }

            return(this.SelectElement(selectionPath));
        }
Exemple #11
0
        /// <summary>
        /// Registers a handler to call when reading non-native XLIFF elements and attributes to store extension data.
        /// </summary>
        /// <param name="ns">The namespace of items handled by the handler.</param>
        /// <param name="handler">The handler to add.</param>
        public void RegisterExtensionHandler(string ns, IExtensionHandler handler)
        {
            ArgValidator.Create(ns, "ns").IsNotNullOrWhitespace();
            ArgValidator.Create(handler, "handler").IsNotNull();

            if (this.handlers.Value.ContainsKey(ns))
            {
                string message;

                message = string.Format(Properties.Resources.XliffReader_ExtensionAlreadyRegistered_Format, ns);
                throw new ExtensionAlreadyRegisteredException(message);
            }

            this.handlers.Value.Add(ns, handler);
        }
        /// <summary>
        /// Converts a value from a native type to a string.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representation of the value that is used by XLIFF.</returns>
        public string Convert(object value)
        {
            string result;

            ArgValidator.Create(value, "value").IsNotNull().IsOfType(typeof(TData));

            if (!this.Map.TryGetValue((TData)value, out result))
            {
                string message;

                message = string.Format(
                    Properties.Resources.ConverterBase_ValueNotImplemented_Format,
                    value,
                    this.GetType().Name);
                throw new NotImplementedException(message);
            }

            return(result);
        }
        /// <summary>
        /// Serializes an <see cref="XliffDocument"/> to a stream.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="document">The document to write.</param>
        public void Serialize(Stream stream, XliffDocument document)
        {
            XmlWriterSettings settings;

            ArgValidator.Create(stream, "stream").IsNotNull();
            ArgValidator.Create(document, "document").IsNotNull();

            settings        = new XmlWriterSettings();
            settings.Indent = this.settings.Indent;
            if (settings.Indent && (this.settings.IndentChars != null))
            {
                settings.IndentChars = this.settings.IndentChars;
            }

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                this.Serialize(writer, document);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ExplicitOutputDependencyAttribute"/> class.
        /// </summary>
        /// <param name="property">The name of the property the attributed property depends on.</param>
        public ExplicitOutputDependencyAttribute(string property)
        {
            ArgValidator.Create(property, "property").IsNotNull();

            this.Property = property;
        }
        /// <summary>
        /// Converts a hexidecimal string to an integer.
        /// </summary>
        /// <param name="value">The XLIFF string to convert.</param>
        /// <returns>The integer value of the string.</returns>
        object IValueConverter.ConvertBack(string value)
        {
            ArgValidator.Create(value, "value").IsNotNullOrWhitespace();

            return(int.Parse(value, NumberStyles.HexNumber));
        }
        /// <summary>
        /// Converts an integer to a hexidecimal string.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representation of the value that is used by XLIFF.</returns>
        string IValueConverter.Convert(object value)
        {
            ArgValidator.Create(value, "value").IsNotNull().IsOfType(typeof(int));

            return(string.Format("{0:X}", value).PadLeft(4, '0'));
        }
 /// <summary>
 /// Selects an item matching the selection query.
 /// </summary>
 /// <param name="path">The selection query.</param>
 /// <returns>The object that was selected from the query path, or null if no match was found.</returns>
 /// <example>The value of <paramref name="path"/> might look something like "#g=group1/f=file1/u=unit1/n=note1"
 /// which is a relative path from the current object, not a full path from the document root.</example>
 public ISelectable Select(string path)
 {
     ArgValidator.Create(path, "path").IsNotNull().StartsWith(Utilities.Constants.SelectorPathIndictator);
     return(this.SelectElement(Utilities.RemoveSelectorIndicator(path)));
 }
        /// <summary>
        /// Converts a string to a DateTime.
        /// </summary>
        /// <param name="value">The XLIFF string to convert.</param>
        /// <returns>The integer value of the string.</returns>
        object IValueConverter.ConvertBack(string value)
        {
            ArgValidator.Create(value, "value").IsNotNullOrWhitespace();

            return(DateTime.Parse(value, null, DateTimeStyles.RoundtripKind));
        }
        /// <summary>
        /// Converts a DateTime to a string.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representation of the value that is used by XLIFF.</returns>
        string IValueConverter.Convert(object value)
        {
            ArgValidator.Create(value, "value").IsNotNull().IsOfType(typeof(DateTime));

            return(((DateTime)value).ToString("O"));
        }
Exemple #20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConverterAttribute"/> class.
        /// </summary>
        /// <param name="type">The type of the converter class.</param>
        public ConverterAttribute(Type type)
        {
            ArgValidator.Create(type, "type").IsNotNull();

            this.TypeName = type.FullName;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HasValueIndicatorAttribute"/> class.
        /// </summary>
        /// <param name="type">The type of the indicator class.</param>
        public HasValueIndicatorAttribute(Type type)
        {
            ArgValidator.Create(type, "type").IsNotNull();

            this.TypeName = type.FullName;
        }
Exemple #22
0
        /// <summary>
        /// Adds an element or text member to the extension data.
        /// </summary>
        /// <param name="child">The child to add.</param>
        public void AddChild(ElementInfo child)
        {
            ArgValidator.Create(child, "child").IsNotNull();

            this.children.Value.Add(child);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XliffWriter"/> class.
        /// </summary>
        /// <param name="settings">The settings describing how to write content.</param>
        public XliffWriter(XliffWriterSettings settings)
        {
            ArgValidator.Create(settings, "settings").IsNotNull();

            this.settings = settings;
        }
        /// <summary>
        /// Converts an XLIFF string to a dictionary whose key and value are strings.
        /// </summary>
        /// <param name="value">The XLIFF string to convert.</param>
        /// <returns>The native type of the data.</returns>
        public object ConvertBack(string value)
        {
            IDictionary <string, string> result;
            int    start;
            int    length;
            string key;

            ArgValidator.Create(value, "value").IsNotWhitespaceOnly();

            result = new Dictionary <string, string>();

            start  = 0;
            length = 0;
            key    = null;
            for (int i = 0; i < value.Length; i++)
            {
                char c;

                c = value[i];
                if (c == SubFormatStyleConverter.CommaChar)
                {
                    // Delimiting the key from the value.
                    if (key == null)
                    {
                        try
                        {
                            key = value.Substring(start, length);
                            XmlConvert.VerifyName(key);
                        }
                        catch (Exception e)
                        {
                            string message;

                            message = string.Format(Properties.Resources.SubFormatStyleConverter_InvalidKey_Format, key);
                            throw new FormatException(message, e);
                        }

                        start  = i + 1;
                        length = 0;
                    }
                    else
                    {
                        string message;

                        message = string.Format(Properties.Resources.SubFormatStyleConverter_CommaInValue_Format, key);
                        throw new FormatException(message);
                    }
                }
                else if (c == SubFormatStyleConverter.BackslashChar)
                {
                    if ((i < (value.Length - 1)) &&
                        ((string.Format("{0}{1}", c, value[i + 1]) == SubFormatStyleConverter.EscapedBackslash) ||
                         (string.Format("{0}{1}", c, value[i + 1]) == SubFormatStyleConverter.EscapedComma)))
                    {
                        i++;
                        length += 2;
                    }
                    else if (key != null)
                    {
                        // Delimiting entries. Overwrite duplicates.
                        result[key] = value.Substring(start, length);
                        key         = null;
                        start       = i + 1;
                        length      = 0;
                    }
                    else
                    {
                        throw new FormatException(Properties.Resources.SubFormatStyleConverter_BackslashInKey);
                    }
                }
                else
                {
                    length++;
                }
            }

            if (length > 0)
            {
                if (key == null)
                {
                    string message;

                    message = string.Format(
                        Properties.Resources.SubFormatStyleConverter_KeyWithoutValue_Format,
                        value.Substring(start, length));
                    throw new FormatException(message);
                }
                else
                {
                    // Overwrite duplicates.
                    result[key] = value.Substring(start, length);
                }
            }

            return(result);
        }
Exemple #25
0
        /// <summary>
        /// Adds an attribute member to the extension data.
        /// </summary>
        /// <param name="attribute">The attribute to add.</param>
        public void AddAttribute(IExtensionAttribute attribute)
        {
            ArgValidator.Create(attribute, "attribute").IsNotNull();

            this.attributes.Value.Add(attribute);
        }
Exemple #26
0
        /// <summary>
        /// Converts a string to a float.
        /// </summary>
        /// <param name="value">The XLIFF string to convert.</param>
        /// <returns>The float value of the string.</returns>
        object IValueConverter.ConvertBack(string value)
        {
            ArgValidator.Create(value, "value").IsNotNullOrWhitespace();

            return(float.Parse(value));
        }
        /// <summary>
        /// Adds an element to this object as a child.
        /// </summary>
        /// <param name="element">The object to add.</param>
        public void AddChild(ElementInfo element)
        {
            ArgValidator.Create(element, "element").IsNotNull();

            this.StoreChild(element);
        }
Exemple #28
0
        /// <summary>
        /// Converts an integer to a string.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representation of the value that is used by XLIFF.</returns>
        string IValueConverter.Convert(object value)
        {
            ArgValidator.Create(value, "value").IsNotNull().IsOfType(typeof(int));

            return(value.ToString());
        }