Example usage for javax.xml.datatype Duration getDays

List of usage examples for javax.xml.datatype Duration getDays

Introduction

In this page you can find the example usage for javax.xml.datatype Duration getDays.

Prototype

public int getDays() 

Source Link

Document

Obtains the value of the DAYS field as an integer value, or 0 if not present.

Usage

From source file:Main.java

/**
 * Create a new Duration object without any fractional second component.
 * @param duration The duration object to strip
 * @return The input Duration with the fractional second part stripped off.
 *//*from  w  w  w  .  j  ava2 s  .  c o  m*/
private static Duration stripFractionalSeconds(Duration duration) {
    int year = duration.getYears();
    int month = duration.getMonths();
    int day = duration.getDays();
    int hour = duration.getHours();
    int minute = duration.getMinutes();
    int second = duration.getSeconds();

    boolean positive = duration.getSign() >= 0;

    return FACTORY.newDuration(positive, year, month, day, hour, minute, second);
}

From source file:Main.java

/**
 * Special compare method that gets around the problem in java 1.5,
 * where years and months are converted to days after arithmetic.
 * @param d1 First duration//from w w w. ja va 2s.com
 * @param d2 Second duration
 * @return -1 if d1 < d2, 0 if d1 == d2 and +1 if d1 > d2
 */
public static int compare(Duration d1, Duration d2) {
    if (d1 == null && d2 == null)
        return 0;

    if (d1 == null)
        return -1;

    if (d2 == null)
        return 1;

    boolean b1 = d1.getSign() >= 0;
    boolean b2 = d2.getSign() >= 0;

    if (!b1 && b2)
        return -1;

    if (b1 && !b2)
        return 1;

    // Now normalise in case we are running with java 1.5 runtime
    javax.xml.datatype.Duration n1 = normaliseDays(d1);
    javax.xml.datatype.Duration n2 = normaliseDays(d2);

    if (n1.getDays() < n2.getDays())
        return -1;
    if (n1.getDays() > n2.getDays())
        return 1;

    if (n1.getHours() < n2.getHours())
        return -1;
    if (n1.getHours() > n2.getHours())
        return 1;

    if (n1.getMinutes() < n2.getMinutes())
        return -1;
    if (n1.getMinutes() > n2.getMinutes())
        return 1;

    BigDecimal s1 = (BigDecimal) n1.getField(DatatypeConstants.SECONDS);
    BigDecimal s2 = (BigDecimal) n2.getField(DatatypeConstants.SECONDS);

    return s1.compareTo(s2);
}

From source file:Main.java

/**
 * Add two positive Duration objects.//w  w w  .j a  v a  2  s.  c o  m
 * @param d1 The first Duration.
 * @param d2 The second Duration.
 * @return The sum of the two durations.
 */
private static Duration addPositiveDurations(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.add(s2);

    Duration strip1 = stripFractionalSeconds(d1);
    Duration strip2 = stripFractionalSeconds(d2);

    Duration stripResult = strip1.add(strip2);

    if (extraSeconds.compareTo(BigDecimal.ONE) >= 0) {
        stripResult = stripResult.add(DURATION_1_SECOND);
        extraSeconds = extraSeconds.subtract(BigDecimal.ONE);
    }

    BigDecimal properSeconds = BigDecimal.valueOf(stripResult.getSeconds()).add(extraSeconds);

    return FACTORY.newDuration(true, BigInteger.valueOf(stripResult.getYears()),
            BigInteger.valueOf(stripResult.getMonths()), BigInteger.valueOf(stripResult.getDays()),
            BigInteger.valueOf(stripResult.getHours()), BigInteger.valueOf(stripResult.getMinutes()),
            properSeconds);
}

From source file:Main.java

/**
 * Subtract one positive Duration from another, larger positive Duration.
 * @param d1 The larger positive duration
 * @param d2 The smaller positive duration
 * @return The difference//from   w  ww  . ja  v a 2 s  .c  o  m
 */
private static Duration subtractSmallerPositiveDurationFromLargerPositiveDuration(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.subtract(s2);

    Duration strip1 = stripFractionalSeconds(d1);
    Duration strip2 = stripFractionalSeconds(d2);

    Duration stripResult = strip1.subtract(strip2);

    if (extraSeconds.compareTo(BigDecimal.ZERO) < 0) {
        stripResult = stripResult.subtract(DURATION_1_SECOND);
        extraSeconds = extraSeconds.add(BigDecimal.ONE);
    }

    BigDecimal properSeconds = BigDecimal.valueOf(stripResult.getSeconds()).add(extraSeconds);

    return FACTORY.newDuration(true, BigInteger.valueOf(stripResult.getYears()),
            BigInteger.valueOf(stripResult.getMonths()), BigInteger.valueOf(stripResult.getDays()),
            BigInteger.valueOf(stripResult.getHours()), BigInteger.valueOf(stripResult.getMinutes()),
            properSeconds);
}

From source file:com.xqdev.jam.MLJAM.java

private static void sendXQueryResponse(HttpServletResponse res, Object o) throws IOException {
    // Make sure to leave the status code alone.  It defaults to 200, but sometimes
    // callers of this method will have set it to a custom code.
    res.setContentType("x-marklogic/xquery; charset=UTF-8");
    //res.setContentType("text/plain");
    Writer writer = res.getWriter(); // care to handle errors later?

    if (o == null) {
        writer.write("()");
    }//ww  w . j a v a 2s  . c o  m

    else if (o instanceof byte[]) {
        writer.write("binary {'");
        writer.write(hexEncode((byte[]) o));
        writer.write("'}");
    }

    else if (o instanceof Object[]) {
        Object[] arr = (Object[]) o;
        writer.write("(");
        for (int i = 0; i < arr.length; i++) {
            sendXQueryResponse(res, arr[i]);
            if (i + 1 < arr.length)
                writer.write(", ");
        }
        writer.write(")");
    }

    else if (o instanceof String) {
        writer.write("'");
        writer.write(escapeSingleQuotes(o.toString()));
        writer.write("'");
    } else if (o instanceof Integer) {
        writer.write("xs:int(");
        writer.write(o.toString());
        writer.write(")");
    } else if (o instanceof Long) {
        writer.write("xs:integer(");
        writer.write(o.toString());
        writer.write(")");
    } else if (o instanceof Float) {
        Float flt = (Float) o;
        writer.write("xs:float(");
        if (flt.equals(Float.POSITIVE_INFINITY)) {
            writer.write("'INF'");
        } else if (flt.equals(Float.NEGATIVE_INFINITY)) {
            writer.write("'-INF'");
        } else if (flt.equals(Float.NaN)) {
            writer.write("fn:number(())"); // poor man's way to write NaN
        } else {
            writer.write(o.toString());
        }
        writer.write(")");
    } else if (o instanceof Double) {
        Double dbl = (Double) o;
        writer.write("xs:double(");
        if (dbl.equals(Double.POSITIVE_INFINITY)) {
            writer.write("'INF'");
        } else if (dbl.equals(Double.NEGATIVE_INFINITY)) {
            writer.write("'-INF'");
        } else if (dbl.equals(Double.NaN)) {
            writer.write("fn:number(())"); // poor man's way to write NaN
        } else {
            writer.write(o.toString());
        }
        writer.write(")");
    } else if (o instanceof Boolean) {
        writer.write("xs:boolean('");
        writer.write(o.toString());
        writer.write("')");
    } else if (o instanceof BigDecimal) {
        writer.write("xs:decimal(");
        writer.write(o.toString());
        writer.write(")");
    } else if (o instanceof Date) {
        // We want something like: 2006-04-30T01:28:30.499-07:00
        // We format to get:       2006-04-30T01:28:30.499-0700
        // Then we add in the colon
        writer.write("xs:dateTime('");
        String d = dateFormat.format((Date) o);
        writer.write(d.substring(0, d.length() - 2));
        writer.write(":");
        writer.write(d.substring(d.length() - 2));
        writer.write("')");
    } else if (o instanceof XMLGregorianCalendar) {
        XMLGregorianCalendar greg = (XMLGregorianCalendar) o;
        QName type = greg.getXMLSchemaType();
        if (type.equals(DatatypeConstants.DATETIME)) {
            writer.write("xs:dateTime('");
        } else if (type.equals(DatatypeConstants.DATE)) {
            writer.write("xs:date('");
        } else if (type.equals(DatatypeConstants.TIME)) {
            writer.write("xs:time('");
        } else if (type.equals(DatatypeConstants.GYEARMONTH)) {
            writer.write("xs:gYearMonth('");
        } else if (type.equals(DatatypeConstants.GMONTHDAY)) {
            writer.write("xs:gMonthDay('");
        } else if (type.equals(DatatypeConstants.GYEAR)) {
            writer.write("xs:gYear('");
        } else if (type.equals(DatatypeConstants.GMONTH)) {
            writer.write("xs:gMonth('");
        } else if (type.equals(DatatypeConstants.GDAY)) {
            writer.write("xs:gDay('");
        }
        writer.write(greg.toXMLFormat());
        writer.write("')");
    } else if (o instanceof Duration) {
        Duration dur = (Duration) o;
        /*
        // The following fails on Xerces
        QName type = dur.getXMLSchemaType();
        if (type.equals(DatatypeConstants.DURATION)) {
          writer.write("xs:duration('");
        }
        else if (type.equals(DatatypeConstants.DURATION_DAYTIME)) {
          writer.write("xdt:dayTimeDuration('");
        }
        else if (type.equals(DatatypeConstants.DURATION_YEARMONTH)) {
          writer.write("xdt:yearMonthDuration('");
        }
        */
        // If no years or months, must be DURATION_DAYTIME
        if (dur.getYears() == 0 && dur.getMonths() == 0) {
            writer.write("xdt:dayTimeDuration('");
        }
        // If has years or months but nothing else, must be DURATION_YEARMONTH
        else if (dur.getDays() == 0 && dur.getHours() == 0 && dur.getMinutes() == 0 && dur.getSeconds() == 0) {
            writer.write("xdt:yearMonthDuration('");
        } else {
            writer.write("xs:duration('");
        }
        writer.write(dur.toString());
        writer.write("')");
    }

    else if (o instanceof org.jdom.Element) {
        org.jdom.Element elt = (org.jdom.Element) o;
        writer.write("xdmp:unquote('");
        // Because "&lt;" in XQuery is the same as "<" I need to double escape any ampersands
        writer.write(new org.jdom.output.XMLOutputter().outputString(elt).replaceAll("&", "&amp;")
                .replaceAll("'", "''"));
        writer.write("')/*"); // make sure to return the root elt
    } else if (o instanceof org.jdom.Document) {
        org.jdom.Document doc = (org.jdom.Document) o;
        writer.write("xdmp:unquote('");
        writer.write(new org.jdom.output.XMLOutputter().outputString(doc).replaceAll("&", "&amp;")
                .replaceAll("'", "''"));
        writer.write("')");
    } else if (o instanceof org.jdom.Text) {
        org.jdom.Text text = (org.jdom.Text) o;
        writer.write("text {'");
        writer.write(escapeSingleQuotes(text.getText()));
        writer.write("'}");
    } else if (o instanceof org.jdom.Attribute) {
        // <fake xmlns:pref="http://uri.com" pref:attrname="attrvalue"/>/@*:attrname
        // <fake xmlns="http://uri.com" attrname="attrvalue"/>/@*:attrname
        org.jdom.Attribute attr = (org.jdom.Attribute) o;
        writer.write("<fake xmlns");
        if ("".equals(attr.getNamespacePrefix())) {
            writer.write("=\"");
        } else {
            writer.write(":" + attr.getNamespacePrefix() + "=\"");
        }
        writer.write(attr.getNamespaceURI());
        writer.write("\" ");
        writer.write(attr.getQualifiedName());
        writer.write("=\"");
        writer.write(escapeSingleQuotes(attr.getValue()));
        writer.write("\"/>/@*:");
        writer.write(attr.getName());
    } else if (o instanceof org.jdom.Comment) {
        org.jdom.Comment com = (org.jdom.Comment) o;
        writer.write("comment {'");
        writer.write(escapeSingleQuotes(com.getText()));
        writer.write("'}");
    } else if (o instanceof org.jdom.ProcessingInstruction) {
        org.jdom.ProcessingInstruction pi = (org.jdom.ProcessingInstruction) o;
        writer.write("processing-instruction ");
        writer.write(pi.getTarget());
        writer.write(" {'");
        writer.write(escapeSingleQuotes(pi.getData()));
        writer.write("'}");
    }

    else if (o instanceof QName) {
        QName q = (QName) o;
        writer.write("fn:expanded-QName('");
        writer.write(escapeSingleQuotes(q.getNamespaceURI()));
        writer.write("','");
        writer.write(q.getLocalPart());
        writer.write("')");
    }

    else {
        writer.write("error('XQuery tried to retrieve unsupported type: " + o.getClass().getName() + "')");
    }

    writer.flush();
}

From source file:ca.phon.session.io.xml.v12.XMLSessionReader_v12.java

private Participant copyParticipant(SessionFactory factory, ParticipantType pt, LocalDate sessionDate) {
    final Participant retVal = factory.createParticipant();

    retVal.setId(pt.getId());/*from w ww.  j  ava  2 s .  c  o m*/
    retVal.setName(pt.getName());

    final XMLGregorianCalendar bday = pt.getBirthday();
    if (bday != null) {
        final LocalDate bdt = LocalDate.of(bday.getYear(), bday.getMonth(), bday.getDay());
        retVal.setBirthDate(bdt);

        // calculate age up to the session date
        final Period period = Period.between(bdt, sessionDate);
        retVal.setAgeTo(period);
    }

    final Duration ageDuration = pt.getAge();
    if (ageDuration != null) {
        // convert to period
        final Period age = Period.of(ageDuration.getYears(), ageDuration.getMonths(), ageDuration.getDays());
        retVal.setAge(age);
    }

    retVal.setEducation(pt.getEducation());
    retVal.setGroup(pt.getGroup());

    String langs = "";
    for (String lang : pt.getLanguage())
        langs += (langs.length() > 0 ? ", " : "") + lang;
    retVal.setLanguage(langs);

    if (pt.getSex() == SexType.MALE)
        retVal.setSex(Sex.MALE);
    else if (pt.getSex() == SexType.FEMALE)
        retVal.setSex(Sex.FEMALE);
    else
        retVal.setSex(Sex.UNSPECIFIED);

    ParticipantRole prole = ParticipantRole.fromString(pt.getRole());
    if (prole == null)
        prole = ParticipantRole.TARGET_CHILD;
    retVal.setRole(prole);

    retVal.setSES(pt.getSES());

    return retVal;
}

From source file:org.miloss.fgsms.common.Utility.java

/**
 * Converts a duration to 1yr 1mo 1d 1hr 1min 1s string format
 *
 * @param d//from w ww. ja va2s .com
 * @return
 */
public static String durationToString(Duration d) {
    if (d == null) {
        return "";
    }
    String s = "";
    if (d.getYears() > 0) {
        s += d.getYears() + "yr ";
    }
    if (d.getMonths() > 0) {
        s += d.getMonths() + "mo ";
    }
    if (d.getDays() > 0) {
        s += d.getDays() + "d ";
    }
    if (d.getHours() > 0) {
        s += d.getHours() + "hr ";
    }
    if (d.getMinutes() > 0) {
        s += d.getMinutes() + "min ";
    }
    if (d.getSeconds() > 0) {
        s += d.getSeconds() + "s ";
    }
    return s.trim();
}

From source file:org.miloss.fgsms.common.Utility.java

/**
 * returns just the largest unit of measure for durations up to years
 *
 * @param d//  ww  w  .  j a  v  a2  s. c o m
 * @return
 */
public static String durationLargestUnitToString(Duration d) {
    if (d == null) {
        return "";
    }
    if (d.getYears() > 0) {
        return d.getYears() + "yr";
    }
    if (d.getMonths() > 0) {
        return d.getMonths() + "mo";
    }
    if (d.getDays() > 0) {
        return d.getDays() + "d";
    }
    if (d.getHours() > 0) {
        return d.getHours() + "hr";
    }
    if (d.getMinutes() > 0) {
        return d.getMinutes() + "min";
    }
    if (d.getSeconds() > 0) {
        return d.getSeconds() + "s";
    }
    return "";
}

From source file:org.miloss.fgsms.presentation.Helper.java

/**
     *  returns xyr xmo xd xhr xmin xs representation of a duration
     * //from  w w w.jav a  2  s.c o m
     */
    public static String DurationToString(Duration d) {
        if (d == null) {
            return "";
        }
        String s = "";
        if (d.getYears() > 0) {
            s += d.getYears() + "yr ";
        }
        if (d.getMonths() > 0) {
            s += d.getMonths() + "mo ";
        }
        if (d.getDays() > 0) {
            s += d.getDays() + "d ";
        }
        if (d.getHours() > 0) {
            s += d.getHours() + "hr ";
        }
        if (d.getMinutes() > 0) {
            s += d.getMinutes() + "min ";
        }
        if (d.getSeconds() > 0) {
            s += d.getSeconds() + "s ";
        }
        return s;
    }