/// <summary>Fixes the GPS Timestamp in EXIF.</summary>
        /// <param name="exifSchema">the EXIF schema node</param>
        /// <exception cref="iText.Kernel.XMP.XMPException">Thrown if the date conversion fails.
        ///     </exception>
        private static void FixGPSTimeStamp(XMPNode exifSchema)
        {
            // Note: if dates are not found the convert-methods throws an exceptions,
            //       and this methods returns.
            XMPNode gpsDateTime = XMPNodeUtils.FindChildNode(exifSchema, "exif:GPSTimeStamp", false);

            if (gpsDateTime == null)
            {
                return;
            }

            try {
                XMPDateTime binGpsStamp = XMPUtils.ConvertToDate(gpsDateTime.GetValue());
                if (binGpsStamp.GetYear() != 0 || binGpsStamp.GetMonth() != 0 || binGpsStamp.GetDay() != 0)
                {
                    return;
                }

                XMPNode otherDate = XMPNodeUtils.FindChildNode(exifSchema, "exif:DateTimeOriginal", false);
                otherDate = otherDate ?? XMPNodeUtils.FindChildNode(exifSchema, "exif:DateTimeDigitized", false);

                XMPDateTime binOtherDate = XMPUtils.ConvertToDate(otherDate.GetValue());
                XMPCalendar cal          = binGpsStamp.GetCalendar();
                DateTime    dt           = new DateTime(binOtherDate.GetYear(), binOtherDate.GetMonth(), binOtherDate.GetDay(),
                                                        cal.GetDateTime().Hour, cal.GetDateTime().Minute, cal.GetDateTime().Second, cal.GetDateTime().Millisecond);
                cal.SetDateTime(dt);
                binGpsStamp = new XMPDateTimeImpl(cal);
                gpsDateTime.SetValue(XMPUtils.ConvertFromDate(binGpsStamp));
            }
            catch (XMPException) {
            }
        }
        /// <summary>
        /// Sets the local time zone without touching any other Any existing time zone value is replaced,
        /// the other date/time fields are not adjusted in any way.
        /// </summary>
        /// <param name="dateTime">the <code>XMPDateTime</code> variable containing the value to be modified.
        ///     </param>
        /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
        public static XMPDateTime SetLocalTimeZone(XMPDateTime dateTime)
        {
            XMPCalendar cal = dateTime.GetCalendar();

            cal.SetTimeZone(TimeZone.CurrentTimeZone);
            return(new XMPDateTimeImpl(cal));
        }
Beispiel #3
0
        /// <summary>
        /// Fixes the GPS Timestamp in EXIF. </summary>
        /// <param name="exifSchema"> the EXIF schema node </param>
        /// <exception cref="XmpException"> Thrown if the date conversion fails. </exception>
        private static void FixGpsTimeStamp(XmpNode exifSchema)
        {
            // Note: if dates are not found the convert-methods throws an exceptions,
            //       and this methods returns.
            XmpNode gpsDateTime = XmpNodeUtils.FindChildNode(exifSchema, "exif:GPSTimeStamp", false);

            if (gpsDateTime == null)
            {
                return;
            }

            try {
                XMPDateTime binGpsStamp = XMPUtils.ConvertToDate(gpsDateTime.Value);
                if (binGpsStamp.Year != 0 || binGpsStamp.Month != 0 || binGpsStamp.Day != 0)
                {
                    return;
                }

                XmpNode otherDate = XmpNodeUtils.FindChildNode(exifSchema, "exif:DateTimeOriginal", false);
                otherDate = otherDate ?? XmpNodeUtils.FindChildNode(exifSchema, "exif:DateTimeDigitized", false);

                XMPDateTime binOtherDate = XMPUtils.ConvertToDate(otherDate.Value);
                XmpCalendar cal          = binGpsStamp.Calendar;
                DateTime    dt           = new DateTime(binOtherDate.Year, binOtherDate.Month, binOtherDate.Day, cal.DateTime.Hour,
                                                        cal.DateTime.Minute, cal.DateTime.Second, cal.DateTime.Millisecond);
                cal.DateTime      = dt;
                binGpsStamp       = new XmpDateTimeImpl(cal);
                gpsDateTime.Value = XMPUtils.ConvertFromDate(binGpsStamp);
            }
            catch (XmpException) {
            }
        }
        /// <summary>Make sure a time is UTC.</summary>
        /// <remarks>
        /// Make sure a time is UTC. If the time zone is not UTC, the time is
        /// adjusted and the time zone set to be UTC.
        /// </remarks>
        /// <param name="dateTime">
        /// the <code>XMPDateTime</code> variable containing the time to
        /// be modified.
        /// </param>
        /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
        public static XMPDateTime ConvertToUTCTime(XMPDateTime dateTime)
        {
            long        timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
            XMPCalendar cal          = new XMPCalendar();

            cal.SetTimeInMillis(timeInMillis);
            return(new XMPDateTimeImpl(cal));
        }
 /// <summary>Make sure a time is UTC.</summary>
 /// <remarks>
 /// Make sure a time is UTC. If the time zone is not UTC, the time is
 /// adjusted and the time zone set to be UTC.
 /// </remarks>
 /// <param name="dateTime">
 /// the <code>XMPDateTime</code> variable containing the time to
 /// be modified.
 /// </param>
 /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
 public static XMPDateTime ConvertToUTCTime(XMPDateTime dateTime)
 {
     long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
     Sharpen.GregorianCalendar cal = new Sharpen.GregorianCalendar(Utc);
     cal.SetGregorianChange(Sharpen.Extensions.CreateDate(long.MinValue));
     cal.SetTimeInMillis(timeInMillis);
     return new XMPDateTimeImpl(cal);
 }
 /// <summary>Make sure a time is local.</summary>
 /// <remarks>
 /// Make sure a time is local. If the time zone is not the local zone, the time is adjusted and
 /// the time zone set to be local.
 /// </remarks>
 /// <param name="dateTime">the <code>XMPDateTime</code> variable containing the time to be modified.</param>
 /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
 public static XMPDateTime ConvertToLocalTime(XMPDateTime dateTime)
 {
     long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
     // has automatically local timezone
     Sharpen.GregorianCalendar cal = new Sharpen.GregorianCalendar();
     cal.SetTimeInMillis(timeInMillis);
     return new XMPDateTimeImpl(cal);
 }
Beispiel #7
0
        /// <summary>Make sure a time is UTC.</summary>
        /// <remarks>
        /// Make sure a time is UTC. If the time zone is not UTC, the time is
        /// adjusted and the time zone set to be UTC.
        /// </remarks>
        /// <param name="dateTime">
        /// the <code>XMPDateTime</code> variable containing the time to
        /// be modified.
        /// </param>
        /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
        public static XMPDateTime ConvertToUTCTime(XMPDateTime dateTime)
        {
            long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();

            Sharpen.GregorianCalendar cal = new Sharpen.GregorianCalendar(Utc);
            cal.SetGregorianChange(Sharpen.Extensions.CreateDate(long.MinValue));
            cal.SetTimeInMillis(timeInMillis);
            return(new XMPDateTimeImpl(cal));
        }
        /// <summary>Make sure a time is local.</summary>
        /// <remarks>
        /// Make sure a time is local. If the time zone is not the local zone, the time is adjusted and
        /// the time zone set to be local.
        /// </remarks>
        /// <param name="dateTime">the <code>XMPDateTime</code> variable containing the time to be modified.
        ///     </param>
        /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
        public static XMPDateTime ConvertToLocalTime(XMPDateTime dateTime)
        {
            long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
            // has automatically local timezone
            XMPCalendar cal = new XMPCalendar();

            cal.SetTimeInMillis(timeInMillis);
            return(new XMPDateTimeImpl(cal));
        }
Beispiel #9
0
        /// <summary>
        /// Converts the node value to String, apply special conversions for defined
        /// types in XMP.
        /// </summary>
        /// <param name="value">
        ///            the node value to set </param>
        /// <returns> Returns the String representation of the node value. </returns>
        internal static string SerializeNodeValue(object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = null;
            }
            else if (value is bool?)
            {
                strValue = XMPUtils.ConvertFromBoolean((bool)((bool?)value));
            }
            else if (value is int?)
            {
                strValue = XMPUtils.ConvertFromInteger((int)((int?)value));
            }
            else if (value is long?)
            {
                strValue = XMPUtils.ConvertFromLong((long)((long?)value));
            }
            else if (value is double?)
            {
                strValue = XMPUtils.ConvertFromDouble((double)((double?)value));
            }
            else if (value is XMPDateTime)
            {
                strValue = XMPUtils.ConvertFromDate((XMPDateTime)value);
            }
            else if (value is XmpCalendar)
            {
                XMPDateTime dt = XMPDateTimeFactory.CreateFromCalendar((XmpCalendar)value);
                strValue = XMPUtils.ConvertFromDate(dt);
            }
            else if (value is byte[])
            {
                strValue = XMPUtils.EncodeBase64((byte[])value);
            }
            else
            {
                strValue = value.ToString();
            }

            return(strValue != null?Utils.RemoveControlChars(strValue) : null);
        }
 /// <summary>Converts a <code>Calendar</code> into an ISO 8601 string.</summary>
 /// <remarks>
 /// Converts a <code>Calendar</code> into an ISO 8601 string.
 /// Format a date according to ISO 8601 and http://www.w3.org/TR/NOTE-datetime:
 /// <ul>
 /// <li>YYYY
 /// <li>YYYY-MM
 /// <li>YYYY-MM-DD
 /// <li>YYYY-MM-DDThh:mmTZD
 /// <li>YYYY-MM-DDThh:mm:ssTZD
 /// <li>YYYY-MM-DDThh:mm:ss.sTZD
 /// </ul>
 /// Data fields:
 /// <ul>
 /// <li>YYYY = four-digit year
 /// <li>MM	 = two-digit month (01=January, etc.)
 /// <li>DD	 = two-digit day of month (01 through 31)
 /// <li>hh	 = two digits of hour (00 through 23)
 /// <li>mm	 = two digits of minute (00 through 59)
 /// <li>ss	 = two digits of second (00 through 59)
 /// <li>s	 = one or more digits representing a decimal fraction of a second
 /// <li>TZD	 = time zone designator (Z or +hh:mm or -hh:mm)
 /// </ul>
 /// <p>
 /// <em>Note:</em> ISO 8601 does not seem to allow years less than 1000 or greater than 9999.
 /// We allow any year, even negative ones. The year is formatted as "%.4d".<p>
 /// <em>Note:</em> Fix for bug 1269463 (silently fix out of range values) included in parsing.
 /// The quasi-bogus "time only" values from Photoshop CS are not supported.
 /// </remarks>
 /// <param name="dateTime">an XMPDateTime-object.</param>
 /// <returns>Returns an ISO 8601 string.</returns>
 public static string Render(XMPDateTime dateTime)
 {
     StringBuilder buffer = new StringBuilder();
     if (dateTime.HasDate())
     {
         // year is rendered in any case, even 0000
         DecimalFormat df = new DecimalFormat("0000", new DecimalFormatSymbols(Sharpen.Extensions.GetEnglishCulture()));
         buffer.Append(df.Format(dateTime.GetYear()));
         if (dateTime.GetMonth() == 0)
         {
             return buffer.ToString();
         }
         // month
         df.ApplyPattern("'-'00");
         buffer.Append(df.Format(dateTime.GetMonth()));
         if (dateTime.GetDay() == 0)
         {
             return buffer.ToString();
         }
         // day
         buffer.Append(df.Format(dateTime.GetDay()));
         // time, rendered if any time field is not zero
         if (dateTime.HasTime())
         {
             // hours and minutes
             buffer.Append('T');
             df.ApplyPattern("00");
             buffer.Append(df.Format(dateTime.GetHour()));
             buffer.Append(':');
             buffer.Append(df.Format(dateTime.GetMinute()));
             // seconds and nanoseconds
             if (dateTime.GetSecond() != 0 || dateTime.GetNanoSecond() != 0)
             {
                 double seconds = dateTime.GetSecond() + dateTime.GetNanoSecond() / 1e9d;
                 df.ApplyPattern(":00.#########");
                 buffer.Append(df.Format(seconds));
             }
             // time zone
             if (dateTime.HasTimeZone())
             {
                 // used to calculate the time zone offset incl. Daylight Savings
                 long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
                 int offset = dateTime.GetTimeZone().GetOffset(timeInMillis);
                 if (offset == 0)
                 {
                     // UTC
                     buffer.Append('Z');
                 }
                 else
                 {
                     int thours = offset / 3600000;
                     int tminutes = Math.Abs(offset % 3600000 / 60000);
                     df.ApplyPattern("+00;-00");
                     buffer.Append(df.Format(thours));
                     df.ApplyPattern(":00");
                     buffer.Append(df.Format(tminutes));
                 }
             }
         }
     }
     return buffer.ToString();
 }
		public virtual void SetPropertyDate(String schemaNS, String propName, XMPDateTime
			 propValue)
		{
			SetProperty(schemaNS, propName, propValue, null);
		}
 /// <summary>
 /// Sets the local time zone without touching any other Any existing time zone value is replaced,
 /// the other date/time fields are not adjusted in any way.
 /// </summary>
 /// <param name="dateTime">the <code>XMPDateTime</code> variable containing the value to be modified.</param>
 /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
 public static XMPDateTime SetLocalTimeZone(XMPDateTime dateTime)
 {
     Sharpen.Calendar cal = dateTime.GetCalendar();
     cal.SetTimeZone(System.TimeZoneInfo.Local);
     return new XMPDateTimeImpl(cal);
 }
        /// <param name="iso8601String">a date string that is ISO 8601 conform.</param>
        /// <param name="binValue">an existing XMPDateTime to set with the parsed date</param>
        /// <returns>Returns an XMPDateTime-object containing the ISO8601-date.</returns>
        /// <exception cref="Com.Adobe.Xmp.XMPException">Is thrown when the string is non-conform.</exception>
        public static XMPDateTime Parse(string iso8601String, XMPDateTime binValue)
        {
            if (iso8601String == null)
            {
                throw new XMPException("Parameter must not be null", XMPErrorConstants.Badparam);
            }
            else
            {
                if (iso8601String.Length == 0)
                {
                    return(binValue);
                }
            }
            ParseState input = new ParseState(iso8601String);
            int        value;

            if (input.Ch(0) == '-')
            {
                input.Skip();
            }
            // Extract the year.
            value = input.GatherInt("Invalid year in date string", 9999);
            if (input.HasNext() && input.Ch() != '-')
            {
                throw new XMPException("Invalid date string, after year", XMPErrorConstants.Badvalue);
            }
            if (input.Ch(0) == '-')
            {
                value = -value;
            }
            binValue.SetYear(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();
            // Extract the month.
            value = input.GatherInt("Invalid month in date string", 12);
            if (input.HasNext() && input.Ch() != '-')
            {
                throw new XMPException("Invalid date string, after month", XMPErrorConstants.Badvalue);
            }
            binValue.SetMonth(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();
            // Extract the day.
            value = input.GatherInt("Invalid day in date string", 31);
            if (input.HasNext() && input.Ch() != 'T')
            {
                throw new XMPException("Invalid date string, after day", XMPErrorConstants.Badvalue);
            }
            binValue.SetDay(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();
            // Extract the hour.
            value = input.GatherInt("Invalid hour in date string", 23);
            binValue.SetHour(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            // Extract the minute.
            if (input.Ch() == ':')
            {
                input.Skip();
                value = input.GatherInt("Invalid minute in date string", 59);
                if (input.HasNext() && input.Ch() != ':' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
                {
                    throw new XMPException("Invalid date string, after minute", XMPErrorConstants.Badvalue);
                }
                binValue.SetMinute(value);
            }
            if (!input.HasNext())
            {
                return(binValue);
            }
            else
            {
                if (input.HasNext() && input.Ch() == ':')
                {
                    input.Skip();
                    value = input.GatherInt("Invalid whole seconds in date string", 59);
                    if (input.HasNext() && input.Ch() != '.' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
                    {
                        throw new XMPException("Invalid date string, after whole seconds", XMPErrorConstants.Badvalue);
                    }
                    binValue.SetSecond(value);
                    if (input.Ch() == '.')
                    {
                        input.Skip();
                        int digits = input.Pos();
                        value = input.GatherInt("Invalid fractional seconds in date string", 999999999);
                        if (input.HasNext() && (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-'))
                        {
                            throw new XMPException("Invalid date string, after fractional second", XMPErrorConstants.Badvalue);
                        }
                        digits = input.Pos() - digits;
                        for (; digits > 9; --digits)
                        {
                            value = value / 10;
                        }
                        for (; digits < 9; ++digits)
                        {
                            value = value * 10;
                        }
                        binValue.SetNanoSecond(value);
                    }
                }
                else
                {
                    if (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
                    {
                        throw new XMPException("Invalid date string, after time", XMPErrorConstants.Badvalue);
                    }
                }
            }
            int tzSign   = 0;
            int tzHour   = 0;
            int tzMinute = 0;

            if (!input.HasNext())
            {
                // no Timezone at all
                return(binValue);
            }
            else
            {
                if (input.Ch() == 'Z')
                {
                    input.Skip();
                }
                else
                {
                    if (input.HasNext())
                    {
                        if (input.Ch() == '+')
                        {
                            tzSign = 1;
                        }
                        else
                        {
                            if (input.Ch() == '-')
                            {
                                tzSign = -1;
                            }
                            else
                            {
                                throw new XMPException("Time zone must begin with 'Z', '+', or '-'", XMPErrorConstants.Badvalue);
                            }
                        }
                        input.Skip();
                        // Extract the time zone hour.
                        tzHour = input.GatherInt("Invalid time zone hour in date string", 23);
                        if (input.HasNext())
                        {
                            if (input.Ch() == ':')
                            {
                                input.Skip();
                                // Extract the time zone minute.
                                tzMinute = input.GatherInt("Invalid time zone minute in date string", 59);
                            }
                            else
                            {
                                throw new XMPException("Invalid date string, after time zone hour", XMPErrorConstants.Badvalue);
                            }
                        }
                    }
                }
            }
            // create a corresponding TZ and set it time zone
            int offset = (tzHour * 3600 * 1000 + tzMinute * 60 * 1000) * tzSign;

            binValue.SetTimeZone((TimeZoneInfo) new SimpleTimeZone(offset, string.Empty));
            if (input.HasNext())
            {
                throw new XMPException("Invalid date string, extra chars at end", XMPErrorConstants.Badvalue);
            }
            return(binValue);
        }
        /// <summary>Converts a <code>Calendar</code> into an ISO 8601 string.</summary>
        /// <remarks>
        /// Converts a <code>Calendar</code> into an ISO 8601 string.
        /// Format a date according to ISO 8601 and http://www.w3.org/TR/NOTE-datetime:
        /// <ul>
        /// <li>YYYY
        /// <li>YYYY-MM
        /// <li>YYYY-MM-DD
        /// <li>YYYY-MM-DDThh:mmTZD
        /// <li>YYYY-MM-DDThh:mm:ssTZD
        /// <li>YYYY-MM-DDThh:mm:ss.sTZD
        /// </ul>
        /// Data fields:
        /// <ul>
        /// <li>YYYY = four-digit year
        /// <li>MM	 = two-digit month (01=January, etc.)
        /// <li>DD	 = two-digit day of month (01 through 31)
        /// <li>hh	 = two digits of hour (00 through 23)
        /// <li>mm	 = two digits of minute (00 through 59)
        /// <li>ss	 = two digits of second (00 through 59)
        /// <li>s	 = one or more digits representing a decimal fraction of a second
        /// <li>TZD	 = time zone designator (Z or +hh:mm or -hh:mm)
        /// </ul>
        /// <p>
        /// <em>Note:</em> ISO 8601 does not seem to allow years less than 1000 or greater than 9999.
        /// We allow any year, even negative ones. The year is formatted as "%.4d".<p>
        /// <em>Note:</em> Fix for bug 1269463 (silently fix out of range values) included in parsing.
        /// The quasi-bogus "time only" values from Photoshop CS are not supported.
        /// </remarks>
        /// <param name="dateTime">an XMPDateTime-object.</param>
        /// <returns>Returns an ISO 8601 string.</returns>
        public static string Render(XMPDateTime dateTime)
        {
            StringBuilder buffer = new StringBuilder();

            if (dateTime.HasDate())
            {
                // year is rendered in any case, even 0000
                DecimalFormat df = new DecimalFormat("0000", new DecimalFormatSymbols(Sharpen.Extensions.GetEnglishCulture()));
                buffer.Append(df.Format(dateTime.GetYear()));
                if (dateTime.GetMonth() == 0)
                {
                    return(buffer.ToString());
                }
                // month
                df.ApplyPattern("'-'00");
                buffer.Append(df.Format(dateTime.GetMonth()));
                if (dateTime.GetDay() == 0)
                {
                    return(buffer.ToString());
                }
                // day
                buffer.Append(df.Format(dateTime.GetDay()));
                // time, rendered if any time field is not zero
                if (dateTime.HasTime())
                {
                    // hours and minutes
                    buffer.Append('T');
                    df.ApplyPattern("00");
                    buffer.Append(df.Format(dateTime.GetHour()));
                    buffer.Append(':');
                    buffer.Append(df.Format(dateTime.GetMinute()));
                    // seconds and nanoseconds
                    if (dateTime.GetSecond() != 0 || dateTime.GetNanoSecond() != 0)
                    {
                        double seconds = dateTime.GetSecond() + dateTime.GetNanoSecond() / 1e9d;
                        df.ApplyPattern(":00.#########");
                        buffer.Append(df.Format(seconds));
                    }
                    // time zone
                    if (dateTime.HasTimeZone())
                    {
                        // used to calculate the time zone offset incl. Daylight Savings
                        long timeInMillis = dateTime.GetCalendar().GetTimeInMillis();
                        int  offset       = dateTime.GetTimeZone().GetOffset(timeInMillis);
                        if (offset == 0)
                        {
                            // UTC
                            buffer.Append('Z');
                        }
                        else
                        {
                            int thours   = offset / 3600000;
                            int tminutes = Math.Abs(offset % 3600000 / 60000);
                            df.ApplyPattern("+00;-00");
                            buffer.Append(df.Format(thours));
                            df.ApplyPattern(":00");
                            buffer.Append(df.Format(tminutes));
                        }
                    }
                }
            }
            return(buffer.ToString());
        }
 /// <summary>Convert from <code>XMPDateTime</code> to string.</summary>
 /// <param name="value">an <code>XMPDateTime</code></param>
 /// <returns>The string representation of the long.</returns>
 public static string ConvertFromDate(XMPDateTime value)
 {
     return ISO8601Converter.Render(value);
 }
		public virtual void SetPropertyDate(String schemaNS, String propName, XMPDateTime
			 propValue, PropertyOptions options)
		{
			SetProperty(schemaNS, propName, propValue, options);
		}
		/// <seealso cref="Com.Adobe.Xmp.XMPMeta.SetPropertyDate(string, string, Com.Adobe.Xmp.XMPDateTime)"/>
		/// <exception cref="Com.Adobe.Xmp.XMPException"/>
		public virtual void SetPropertyDate(string schemaNS, string propName, XMPDateTime propValue)
		{
			SetProperty(schemaNS, propName, propValue, null);
		}
 /// <summary>Converts a <code>Calendar</code> into an ISO 8601 string.</summary>
 /// <remarks>
 /// Converts a <code>Calendar</code> into an ISO 8601 string.
 /// Format a date according to ISO 8601 and http://www.w3.org/TR/NOTE-datetime:
 /// <ul>
 /// <li>YYYY
 /// <li>YYYY-MM
 /// <li>YYYY-MM-DD
 /// <li>YYYY-MM-DDThh:mmTZD
 /// <li>YYYY-MM-DDThh:mm:ssTZD
 /// <li>YYYY-MM-DDThh:mm:ss.sTZD
 /// </ul>
 /// Data fields:
 /// <ul>
 /// <li>YYYY = four-digit year
 /// <li>MM	 = two-digit month (01=January, etc.)
 /// <li>DD	 = two-digit day of month (01 through 31)
 /// <li>hh	 = two digits of hour (00 through 23)
 /// <li>mm	 = two digits of minute (00 through 59)
 /// <li>ss	 = two digits of second (00 through 59)
 /// <li>s	 = one or more digits representing a decimal fraction of a second
 /// <li>TZD	 = time zone designator (Z or +hh:mm or -hh:mm)
 /// </ul>
 /// <p>
 /// <em>Note:</em> ISO 8601 does not seem to allow years less than 1000 or greater than 9999.
 /// We allow any year, even negative ones. The year is formatted as "%.4d".<p>
 /// <em>Note:</em> Fix for bug 1269463 (silently fix out of range values) included in parsing.
 /// The quasi-bogus "time only" values from Photoshop CS are not supported.
 /// </remarks>
 /// <param name="dateTime">an XMPDateTime-object.</param>
 /// <returns>Returns an ISO 8601 string.</returns>
 public static String Render(XMPDateTime dateTime)
 {
     return(dateTime.GetCalendar().GetDateTime().ToString("s"));
 }
        /// <param name="iso8601String">a date string that is ISO 8601 conform.</param>
        /// <param name="binValue">an existing XMPDateTime to set with the parsed date</param>
        /// <returns>Returns an XMPDateTime-object containing the ISO8601-date.</returns>
        /// <exception cref="iText.Kernel.XMP.XMPException">Is thrown when the string is non-conform.
        ///     </exception>
        public static XMPDateTime Parse(String iso8601String, XMPDateTime binValue)
        {
            if (iso8601String == null)
            {
                throw new XMPException("Parameter must not be null", XMPError.BADPARAM);
            }
            if (iso8601String.Length == 0)
            {
                return(binValue);
            }

            ParseState input = new ParseState(iso8601String);

            if (input.Ch(0) == '-')
            {
                input.Skip();
            }

            // Extract the year.
            int value = input.GatherInt("Invalid year in date string", 9999);

            if (input.HasNext() && input.Ch() != '-')
            {
                throw new XMPException("Invalid date string, after year", XMPError.BADVALUE);
            }

            if (input.Ch(0) == '-')
            {
                value = -value;
            }
            binValue.SetYear(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();


            // Extract the month.
            value = input.GatherInt("Invalid month in date string", 12);
            if (input.HasNext() && input.Ch() != '-')
            {
                throw new XMPException("Invalid date string, after month", XMPError.BADVALUE);
            }
            binValue.SetMonth(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();


            // Extract the day.
            value = input.GatherInt("Invalid day in date string", 31);
            if (input.HasNext() && input.Ch() != 'T')
            {
                throw new XMPException("Invalid date string, after day", XMPError.BADVALUE);
            }
            binValue.SetDay(value);
            if (!input.HasNext())
            {
                return(binValue);
            }
            input.Skip();

            // Extract the hour.
            value = input.GatherInt("Invalid hour in date string", 23);
            binValue.SetHour(value);
            if (!input.HasNext())
            {
                return(binValue);
            }

            // Extract the minute.
            if (input.Ch() == ':')
            {
                input.Skip();
                value = input.GatherInt("Invalid minute in date string", 59);
                if (input.HasNext() && input.Ch() != ':' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
                {
                    throw new XMPException("Invalid date string, after minute", XMPError.BADVALUE);
                }
                binValue.SetMinute(value);
            }

            if (!input.HasNext())
            {
                return(binValue);
            }
            if (input.HasNext() && input.Ch() == ':')
            {
                input.Skip();
                value = input.GatherInt("Invalid whole seconds in date string", 59);
                if (input.HasNext() && input.Ch() != '.' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
                {
                    throw new XMPException("Invalid date string, after whole seconds", XMPError.BADVALUE);
                }
                binValue.SetSecond(value);
                if (input.Ch() == '.')
                {
                    input.Skip();
                    int digits = input.Pos();
                    value = input.GatherInt("Invalid fractional seconds in date string", 999999999);
                    if (input.HasNext() && (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-'))
                    {
                        throw new XMPException("Invalid date string, after fractional second",
                                               XMPError.BADVALUE);
                    }
                    digits = input.Pos() - digits;
                    for (; digits > 9; --digits)
                    {
                        value = value / 10;
                    }
                    for (; digits < 9; ++digits)
                    {
                        value = value * 10;
                    }
                    binValue.SetNanoSecond(value);
                }
            }
            else if (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
            {
                throw new XMPException("Invalid date string, after time", XMPError.BADVALUE);
            }


            if (!input.HasNext())
            {
                // no Timezone at all
                return(binValue);
            }
            if (input.Ch() == 'Z')
            {
                input.Skip();
            }
            else if (input.HasNext())
            {
                if (input.Ch() == '+')
                {
                }
                else if (input.Ch() == '-')
                {
                }
                else
                {
                    throw new XMPException("Time zone must begin with 'Z', '+', or '-'", XMPError.BADVALUE);
                }

                input.Skip();
                // Extract the time zone hour.
                if (input.HasNext())
                {
                    if (input.Ch() == ':')
                    {
                        input.Skip();
                    }
                    else
                    {
                        throw new XMPException("Invalid date string, after time zone hour", XMPError.BADVALUE);
                    }
                }
            }

            // create a corresponding TZ and set it time zone
            binValue.SetTimeZone(TimeZone.CurrentTimeZone);

            if (input.HasNext())
            {
                throw new XMPException("Invalid date string, extra chars at end", XMPError.BADVALUE);
            }

            return(binValue);
        }
Beispiel #20
0
 /// <summary>Convert from <code>XMPDateTime</code> to string.</summary>
 /// <param name="value">an <code>XMPDateTime</code></param>
 /// <returns>The string representation of the long.</returns>
 public static String ConvertFromDate(XMPDateTime value)
 {
     return(ISO8601Converter.Render(value));
 }
 /// <param name="iso8601String">a date string that is ISO 8601 conform.</param>
 /// <param name="binValue">an existing XMPDateTime to set with the parsed date</param>
 /// <returns>Returns an XMPDateTime-object containing the ISO8601-date.</returns>
 /// <exception cref="Com.Adobe.Xmp.XMPException">Is thrown when the string is non-conform.</exception>
 public static XMPDateTime Parse(string iso8601String, XMPDateTime binValue)
 {
     if (iso8601String == null)
     {
         throw new XMPException("Parameter must not be null", XMPErrorConstants.Badparam);
     }
     else
     {
         if (iso8601String.Length == 0)
         {
             return binValue;
         }
     }
     ParseState input = new ParseState(iso8601String);
     int value;
     if (input.Ch(0) == '-')
     {
         input.Skip();
     }
     // Extract the year.
     value = input.GatherInt("Invalid year in date string", 9999);
     if (input.HasNext() && input.Ch() != '-')
     {
         throw new XMPException("Invalid date string, after year", XMPErrorConstants.Badvalue);
     }
     if (input.Ch(0) == '-')
     {
         value = -value;
     }
     binValue.SetYear(value);
     if (!input.HasNext())
     {
         return binValue;
     }
     input.Skip();
     // Extract the month.
     value = input.GatherInt("Invalid month in date string", 12);
     if (input.HasNext() && input.Ch() != '-')
     {
         throw new XMPException("Invalid date string, after month", XMPErrorConstants.Badvalue);
     }
     binValue.SetMonth(value);
     if (!input.HasNext())
     {
         return binValue;
     }
     input.Skip();
     // Extract the day.
     value = input.GatherInt("Invalid day in date string", 31);
     if (input.HasNext() && input.Ch() != 'T')
     {
         throw new XMPException("Invalid date string, after day", XMPErrorConstants.Badvalue);
     }
     binValue.SetDay(value);
     if (!input.HasNext())
     {
         return binValue;
     }
     input.Skip();
     // Extract the hour.
     value = input.GatherInt("Invalid hour in date string", 23);
     binValue.SetHour(value);
     if (!input.HasNext())
     {
         return binValue;
     }
     // Extract the minute.
     if (input.Ch() == ':')
     {
         input.Skip();
         value = input.GatherInt("Invalid minute in date string", 59);
         if (input.HasNext() && input.Ch() != ':' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
         {
             throw new XMPException("Invalid date string, after minute", XMPErrorConstants.Badvalue);
         }
         binValue.SetMinute(value);
     }
     if (!input.HasNext())
     {
         return binValue;
     }
     else
     {
         if (input.HasNext() && input.Ch() == ':')
         {
             input.Skip();
             value = input.GatherInt("Invalid whole seconds in date string", 59);
             if (input.HasNext() && input.Ch() != '.' && input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
             {
                 throw new XMPException("Invalid date string, after whole seconds", XMPErrorConstants.Badvalue);
             }
             binValue.SetSecond(value);
             if (input.Ch() == '.')
             {
                 input.Skip();
                 int digits = input.Pos();
                 value = input.GatherInt("Invalid fractional seconds in date string", 999999999);
                 if (input.HasNext() && (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-'))
                 {
                     throw new XMPException("Invalid date string, after fractional second", XMPErrorConstants.Badvalue);
                 }
                 digits = input.Pos() - digits;
                 for (; digits > 9; --digits)
                 {
                     value = value / 10;
                 }
                 for (; digits < 9; ++digits)
                 {
                     value = value * 10;
                 }
                 binValue.SetNanoSecond(value);
             }
         }
         else
         {
             if (input.Ch() != 'Z' && input.Ch() != '+' && input.Ch() != '-')
             {
                 throw new XMPException("Invalid date string, after time", XMPErrorConstants.Badvalue);
             }
         }
     }
     int tzSign = 0;
     int tzHour = 0;
     int tzMinute = 0;
     if (!input.HasNext())
     {
         // no Timezone at all
         return binValue;
     }
     else
     {
         if (input.Ch() == 'Z')
         {
             input.Skip();
         }
         else
         {
             if (input.HasNext())
             {
                 if (input.Ch() == '+')
                 {
                     tzSign = 1;
                 }
                 else
                 {
                     if (input.Ch() == '-')
                     {
                         tzSign = -1;
                     }
                     else
                     {
                         throw new XMPException("Time zone must begin with 'Z', '+', or '-'", XMPErrorConstants.Badvalue);
                     }
                 }
                 input.Skip();
                 // Extract the time zone hour.
                 tzHour = input.GatherInt("Invalid time zone hour in date string", 23);
                 if (input.HasNext())
                 {
                     if (input.Ch() == ':')
                     {
                         input.Skip();
                         // Extract the time zone minute.
                         tzMinute = input.GatherInt("Invalid time zone minute in date string", 59);
                     }
                     else
                     {
                         throw new XMPException("Invalid date string, after time zone hour", XMPErrorConstants.Badvalue);
                     }
                 }
             }
         }
     }
     // create a corresponding TZ and set it time zone
     int offset = (tzHour * 3600 * 1000 + tzMinute * 60 * 1000) * tzSign;
     binValue.SetTimeZone((TimeZoneInfo)new SimpleTimeZone(offset, string.Empty));
     if (input.HasNext())
     {
         throw new XMPException("Invalid date string, extra chars at end", XMPErrorConstants.Badvalue);
     }
     return binValue;
 }
		/// <seealso cref="Com.Adobe.Xmp.XMPMeta.SetPropertyDate(string, string, Com.Adobe.Xmp.XMPDateTime, Com.Adobe.Xmp.Options.PropertyOptions)"/>
		/// <exception cref="Com.Adobe.Xmp.XMPException"/>
		public virtual void SetPropertyDate(string schemaNS, string propName, XMPDateTime propValue, PropertyOptions options)
		{
			SetProperty(schemaNS, propName, propValue, options);
		}
Beispiel #23
0
 /// <summary>
 /// Sets the local time zone without touching any other Any existing time zone value is replaced,
 /// the other date/time fields are not adjusted in any way.
 /// </summary>
 /// <param name="dateTime">the <code>XMPDateTime</code> variable containing the value to be modified.</param>
 /// <returns>Returns an updated <code>XMPDateTime</code>-object.</returns>
 public static XMPDateTime SetLocalTimeZone(XMPDateTime dateTime)
 {
     Sharpen.Calendar cal = dateTime.GetCalendar();
     cal.SetTimeZone(System.TimeZoneInfo.Local);
     return(new XMPDateTimeImpl(cal));
 }
Beispiel #24
0
        /// <summary>
        /// Converts the node value to String, apply special conversions for defined
        /// types in XMP.
        /// </summary>
        /// <param name="value">the node value to set</param>
        /// <returns>Returns the String representation of the node value.</returns>
        internal static string SerializeNodeValue(object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = null;
            }
            else
            {
                if (value is bool)
                {
                    strValue = XMPUtils.ConvertFromBoolean(((bool)value));
                }
                else
                {
                    if (value is int)
                    {
                        strValue = XMPUtils.ConvertFromInteger(((int)value).IntValue());
                    }
                    else
                    {
                        if (value is long)
                        {
                            strValue = XMPUtils.ConvertFromLong(((long)value).LongValue());
                        }
                        else
                        {
                            if (value is double)
                            {
                                strValue = XMPUtils.ConvertFromDouble(((double)value).DoubleValue());
                            }
                            else
                            {
                                if (value is XMPDateTime)
                                {
                                    strValue = XMPUtils.ConvertFromDate((XMPDateTime)value);
                                }
                                else
                                {
                                    if (value is Sharpen.GregorianCalendar)
                                    {
                                        XMPDateTime dt = XMPDateTimeFactory.CreateFromCalendar((Sharpen.GregorianCalendar)value);
                                        strValue = XMPUtils.ConvertFromDate(dt);
                                    }
                                    else
                                    {
                                        if (value is sbyte[])
                                        {
                                            strValue = XMPUtils.EncodeBase64((sbyte[])value);
                                        }
                                        else
                                        {
                                            strValue = value.ToString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(strValue != null?Utils.RemoveControlChars(strValue) : null);
        }
		/// <summary>
		/// Evaluates a raw node value to the given value type, apply special
		/// conversions for defined types in XMP.
		/// </summary>
		/// <param name="valueType">an int indicating the value type</param>
		/// <param name="propNode">the node containing the value</param>
		/// <returns>Returns a literal value for the node.</returns>
		/// <exception cref="iText.Kernel.XMP.XMPException">
		/// if the value of <code>propNode</code> is <code>null</code> or empty or the conversion fails.
		/// </exception>
		private Object EvaluateNodeValue(int valueType, XMPNode propNode)
		{
			Object value;
			String rawValue = propNode.GetValue();
			switch (valueType)
			{
				case VALUE_BOOLEAN:
				{
					value = XMPUtils.ConvertToBoolean(rawValue);
					break;
				}

				case VALUE_INTEGER:
				{
					value = XMPUtils.ConvertToInteger(rawValue);
					break;
				}

				case VALUE_LONG:
				{
					value = XMPUtils.ConvertToLong(rawValue);
					break;
				}

				case VALUE_DOUBLE:
				{
					value = XMPUtils.ConvertToDouble(rawValue);
					break;
				}

				case VALUE_DATE:
				{
					value = XMPUtils.ConvertToDate(rawValue);
					break;
				}

				case VALUE_CALENDAR:
				{
					XMPDateTime dt = XMPUtils.ConvertToDate(rawValue);
					value = dt.GetCalendar();
					break;
				}

				case VALUE_BASE64:
				{
					value = XMPUtils.DecodeBase64(rawValue);
					break;
				}

				case VALUE_STRING:
				default:
				{
					// leaf values return empty string instead of null
					// for the other cases the converter methods provides a "null"
					// value.
					// a default value can only occur if this method is made public.
					value = rawValue != null || propNode.GetOptions().IsCompositeProperty() ? rawValue
						 : "";
					break;
				}
			}
			return value;
		}