コード例 #1
0
ファイル: ZmanimFormatter.cs プロジェクト: shaylami/Zmanim
        ///<summary>
        ///  A method that returns an XML formatted <c>String</c> representing
        ///  the serialized <c>Object</c>. The format used is:
        ///
        ///  <code>
        ///    &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;net.sourceforge.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;25.4&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
        ///    &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
        ///    &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
        ///    ...
        ///    &lt;/AstronomicalTimes&gt;
        ///  </code>
        ///
        ///  Note that the output uses the <a href = "http://www.w3.org/TR/xmlschema11-2/#Date">xsd:Date</a>
        ///  format for times such as sunrise, and <a href = "http://www.w3.org/TR/xmlschema11-2/#duration">xsd:duration</a>
        ///  format for times that are a duration such as the length of a
        ///  <see cref = "AstronomicalCalendar.GetTemporalHour()">temporal hour</see>.
        ///  The output of this method is returned by the <see cref = "AstronomicalCalendar.ToString" /> }.
        ///</summary>
        ///<returns> The XML formatted <c>String</c>. The format will be:
        ///
        ///  <code>
        ///    &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;net.sourceforge.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;25.4&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
        ///    &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
        ///    &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
        ///    ...
        ///    &lt;/AstronomicalTimes&gt;
        ///  </code>
        ///</returns>
        public static string ToXml(AstronomicalCalendar ac)
        {
            var    formatter = new ZmanimFormatter(XSD_DURATION_FORMAT, "yyyy-MM-dd'T'HH:mm:ss");
            string df        = "yyyy-MM-dd";
            var    output    = new StringBuilder("<");

            if (ac.GetType().Name.EndsWith("AstronomicalCalendar"))
            {
                output.Append("AstronomicalTimes");
            }
            else if (ac.GetType().Name.EndsWith("ZmanimCalendar"))
            {
                output.Append("Zmanim");
            }
            output.Append(" date=\"" + ac.DateWithLocation.Date.ToString(df) + "\"");
            output.Append(" type=\"" + ac.GetType().Name + "\"");
            output.Append(" algorithm=\"" + ac.AstronomicalCalculator.CalculatorName + "\"");
            output.Append(" location=\"" + ac.DateWithLocation.Location.LocationName + "\"");
            output.Append(" latitude=\"" + ac.DateWithLocation.Location.Latitude + "\"");
            output.Append(" longitude=\"" + ac.DateWithLocation.Location.Longitude + "\"");
            output.Append(" elevation=\"" + ac.DateWithLocation.Location.Elevation + "\"");
            output.Append(" timeZoneName=\"" + ac.DateWithLocation.Location.TimeZone.GetDisplayName() + "\"");
            output.Append(" timeZoneID=\"" + ac.DateWithLocation.Location.TimeZone.GetId() + "\"");
            output.Append(" timeZoneOffset=\"" +
                          (ac.DateWithLocation.Location.TimeZone.GetOffset(ac.DateWithLocation.Date.ToFileTime()) /
                           ((double)HOUR_MILLIS)) + "\"");

            output.Append(">\n");

            MethodInfo[]   theMethods   = ac.GetType().GetMethods();
            string         tagName      = "";
            object         @value       = null;
            IList <Zman>   dateList     = new List <Zman>();
            IList <Zman>   durationList = new List <Zman>();
            IList <string> otherList    = new List <string>();

            for (int i = 0; i < theMethods.Length; i++)
            {
                if (IncludeMethod(theMethods[i]))
                {
                    tagName = theMethods[i].Name.Substring(3);
                    //String returnType = theMethods[i].getReturnType().getName();
                    try
                    {
                        @value = theMethods[i].Invoke(ac, null);
                        if (@value == null) //FIXME: use reflection to determine what the return type is, not the value
                        {
                            otherList.Add("<" + tagName + ">N/A</" + tagName + ">");
                        }
                        else if (@value is DateTime)
                        {
                            dateList.Add(new Zman((DateTime)@value, tagName));
                        } // shaah zmanis
                        else if (@value is long?)
                        {
                            durationList.Add(new Zman((int)((long?)@value), tagName));
                        } // will probably never enter this block, but is
                        else
                        {
                            // present to be future proof
                            otherList.Add("<" + tagName + ">" + @value + "</" + tagName + ">");
                        }
                    }
                    catch (Exception e)
                    {
                        output.Append(e.StackTrace);
                        throw;
                    }
                }
            }

            foreach (Zman zman in dateList.OrderBy(x => x.ZmanTime))
            {
                output.Append("\t<" + zman.ZmanLabel);
                output.Append(">");
                output.Append(formatter.FormatDate(zman.ZmanTime, ac.DateWithLocation)
                              + "</" + zman.ZmanLabel + ">\n");
            }

            foreach (Zman zman in durationList.OrderBy(x => x.Duration))
            {
                output.Append("\t<" + zman.ZmanLabel);
                output.Append(">");
                output.Append(formatter.Format((int)zman.Duration) + "</"
                              + zman.ZmanLabel + ">\n");
            }

            foreach (string t in otherList)
            {
                // will probably never enter this block
                output.Append("\t" + t + "\n");
            }

            if (ac.GetType().Name.EndsWith("AstronomicalCalendar"))
            {
                output.Append("</AstronomicalTimes>");
            }
            else if (ac.GetType().Name.EndsWith("ZmanimCalendar"))
            {
                output.Append("</Zmanim>");
            }

            return(output.ToString());
        }
コード例 #2
0
ファイル: ZmanimFormatter.cs プロジェクト: ayahc1093/Zmanim
        ///<summary>
        ///  A method that returns an XML formatted <c>String</c> representing
        ///  the serialized <c>Object</c>. The format used is:
        ///	
        ///  <code>
        ///    &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;net.sourceforge.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;25.4&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
        ///    &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
        ///    &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
        ///    ...
        ///    &lt;/AstronomicalTimes&gt;
        ///  </code>
        ///	
        ///  Note that the output uses the <a href = "http://www.w3.org/TR/xmlschema11-2/#Date">xsd:Date</a>
        ///  format for times such as sunrise, and <a href = "http://www.w3.org/TR/xmlschema11-2/#duration">xsd:duration</a>
        ///  format for times that are a duration such as the length of a
        ///  <see cref = "AstronomicalCalendar.GetTemporalHour()">temporal hour</see>.
        ///  The output of this method is returned by the <see cref = "AstronomicalCalendar.ToString" /> }.
        ///</summary>
        ///<returns> The XML formatted <c>String</c>. The format will be:
        ///	
        ///  <code>
        ///    &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;net.sourceforge.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;25.4&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
        ///    &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
        ///    &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
        ///    ...
        ///    &lt;/AstronomicalTimes&gt;
        ///  </code>
        ///</returns>
        public static string ToXml(AstronomicalCalendar ac)
        {
            var formatter = new ZmanimFormatter(XSD_DURATION_FORMAT, "yyyy-MM-dd'T'HH:mm:ss");
            string df = "yyyy-MM-dd";
            var output = new StringBuilder("<");

            if (ac.GetType().Name.EndsWith("AstronomicalCalendar"))
            {
                output.Append("AstronomicalTimes");
            }
            else if (ac.GetType().Name.EndsWith("ZmanimCalendar"))
            {
                output.Append("Zmanim");
            }
            output.Append(" date=\"" + ac.DateWithLocation.Date.ToString(df) + "\"");
            output.Append(" type=\"" + ac.GetType().Name + "\"");
            output.Append(" algorithm=\"" + ac.AstronomicalCalculator.CalculatorName + "\"");
            output.Append(" location=\"" + ac.DateWithLocation.Location.LocationName + "\"");
            output.Append(" latitude=\"" + ac.DateWithLocation.Location.Latitude + "\"");
            output.Append(" longitude=\"" + ac.DateWithLocation.Location.Longitude + "\"");
            output.Append(" elevation=\"" + ac.DateWithLocation.Location.Elevation + "\"");
            output.Append(" timeZoneName=\"" + ac.DateWithLocation.Location.TimeZone.GetDisplayName() + "\"");
            output.Append(" timeZoneID=\"" + ac.DateWithLocation.Location.TimeZone.GetId() + "\"");
            output.Append(" timeZoneOffset=\"" +
                          (ac.DateWithLocation.Location.TimeZone.GetOffset(ac.DateWithLocation.Date.ToFileTime()) /
                           ((double)HOUR_MILLIS)) + "\"");

            output.Append(">\n");

            MethodInfo[] theMethods = ac.GetType().GetMethods();
            string tagName = "";
            object @value = null;
            IList<Zman> dateList = new List<Zman>();
            IList<Zman> durationList = new List<Zman>();
            IList<string> otherList = new List<string>();
            for (int i = 0; i < theMethods.Length; i++)
            {
                if (IncludeMethod(theMethods[i]))
                {
                    tagName = theMethods[i].Name.Substring(3);
                    //String returnType = theMethods[i].getReturnType().getName();
                    try
                    {
                        @value = theMethods[i].Invoke(ac, null);
                        if (@value == null) //FIXME: use reflection to determine what the return type is, not the value
                        {
                            otherList.Add("<" + tagName + ">N/A</" + tagName + ">");
                        }
                        else if (@value is DateTime)
                        {
                            dateList.Add(new Zman((DateTime)@value, tagName));
                        } // shaah zmanis
                        else if (@value is long?)
                        {
                            durationList.Add(new Zman((int)((long?)@value), tagName));
                        } // will probably never enter this block, but is
                        else
                        {
                            // present to be future proof
                            otherList.Add("<" + tagName + ">" + @value + "</" + tagName + ">");
                        }
                    }
                    catch (Exception e)
                    {
                        output.Append(e.StackTrace);
                        throw;
                    }
                }
            }

            foreach (Zman zman in dateList.OrderBy(x => x.ZmanTime))
            {
                output.Append("\t<" + zman.ZmanLabel);
                output.Append(">");
                output.Append(formatter.FormatDate(zman.ZmanTime, ac.DateWithLocation)
                              + "</" + zman.ZmanLabel + ">\n");
            }

            foreach (Zman zman in durationList.OrderBy(x => x.Duration))
            {
                output.Append("\t<" + zman.ZmanLabel);
                output.Append(">");
                output.Append(formatter.Format((int)zman.Duration) + "</"
                              + zman.ZmanLabel + ">\n");
            }

            foreach (string t in otherList)
            {
                // will probably never enter this block
                output.Append("\t" + t + "\n");
            }

            if (ac.GetType().Name.EndsWith("AstronomicalCalendar"))
            {
                output.Append("</AstronomicalTimes>");
            }
            else if (ac.GetType().Name.EndsWith("ZmanimCalendar"))
            {
                output.Append("</Zmanim>");
            }

            return output.ToString();
        }