Example usage for javax.xml.datatype XMLGregorianCalendar setMillisecond

List of usage examples for javax.xml.datatype XMLGregorianCalendar setMillisecond

Introduction

In this page you can find the example usage for javax.xml.datatype XMLGregorianCalendar setMillisecond.

Prototype

public abstract void setMillisecond(int millisecond);

Source Link

Document

Set milliseconds.

Usage

From source file:DatatypeAPIUsage.java

public static void main(String[] args) {
    try {//from   w  ww . j  a v  a  2s.c  o  m
        DatatypeFactory df = DatatypeFactory.newInstance();
        // my work number in milliseconds:
        Duration myPhone = df.newDuration(9054133519l);
        Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
        int compareVal = myPhone.compare(myLife);
        switch (compareVal) {
        case DatatypeConstants.LESSER:
            System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
            break;
        case DatatypeConstants.EQUAL:
            System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
            break;
        case DatatypeConstants.GREATER:
            System.out.println("There are more milliseconds in my phone number than my lifespan.");
            break;
        case DatatypeConstants.INDETERMINATE:
            System.out.println("The comparison could not be carried out.");
        }

        // create a yearMonthDuration
        Duration ymDuration = df.newDurationYearMonth("P12Y10M");
        System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());

        // create a dayTimeDuration (really this time)
        Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
        System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());

        // try to fool the factory!
        try {
            ymDuration = df.newDurationYearMonth("P12Y10M1D");
        } catch (IllegalArgumentException e) {
            System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
        }

        XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
        xgc.setYear(1975);
        xgc.setMonth(DatatypeConstants.AUGUST);
        xgc.setDay(11);
        xgc.setHour(6);
        xgc.setMinute(44);
        xgc.setSecond(0);
        xgc.setMillisecond(0);
        xgc.setTimezone(5);
        xgc.add(myPhone);
        System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);

        // adding a duration to XMLGregorianCalendar
        xgc.add(myLife);
        System.out.println("Adding the duration myLife to the above calendar:" + xgc);

        // create a new XMLGregorianCalendar using the string format of xgc.
        XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());

        // should be equal-if not what happened!!
        if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
            System.out.println("oooops!");
        } else {
            System.out.println("Very good: " + xgc + " is equal to " + xgcCopy);
        }
    } catch (DatatypeConfigurationException dce) {
        System.err.println("error: Datatype error occurred - " + dce.getMessage());
        dce.printStackTrace(System.err);
    }
}

From source file:Main.java

public static XMLGregorianCalendar getXMLDate(final Date dateAndTime) {
    GregorianCalendar c = new GregorianCalendar();
    c.setTimeZone(UTC);// w w w  .ja v a  2  s .c  om
    c.setTime(dateAndTime);

    try {
        XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        ret.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return ret;
    } catch (DatatypeConfigurationException e) {
        return null;
    }
}

From source file:Main.java

public static XMLGregorianCalendar getXMLDate(final Calendar calendar) {
    GregorianCalendar c;//w ww. j  a v  a2s  . c  o m
    if (calendar instanceof GregorianCalendar) {
        c = (GregorianCalendar) calendar;
    } else {
        c = new GregorianCalendar();
        c.setTimeZone(UTC);
        c.setTime(calendar.getTime());
    }

    try {
        XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        ret.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return ret;
    } catch (DatatypeConfigurationException e) {
        return null;
    }
}

From source file:net.servicefixture.converter.XMLGregorianCalendarConverter.java

private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) {
    XMLGregorianCalendar xmlCal;
    try {//from   w ww.  j a v a  2  s. c om
        xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException("Failed to create XMLGregorianCalendar", e);
    }

    xmlCal.setYear(calendar.get(Calendar.YEAR));
    xmlCal.setMonth(calendar.get(Calendar.MONTH) + 1);
    xmlCal.setDay(calendar.get(Calendar.DAY_OF_MONTH));
    xmlCal.setHour(calendar.get(Calendar.HOUR));
    xmlCal.setMinute(calendar.get(Calendar.MINUTE));
    xmlCal.setSecond(calendar.get(Calendar.SECOND));
    xmlCal.setMillisecond(calendar.get(Calendar.MILLISECOND));
    return xmlCal;
}

From source file:be.fedict.eid.applet.service.signer.facets.XAdESSignatureFacet.java

public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
        List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    LOG.debug("preSign");

    // QualifyingProperties
    QualifyingPropertiesType qualifyingProperties = this.xadesObjectFactory.createQualifyingPropertiesType();
    qualifyingProperties.setTarget("#" + signatureId);

    // SignedProperties
    SignedPropertiesType signedProperties = this.xadesObjectFactory.createSignedPropertiesType();
    String signedPropertiesId;/*from   w  w w  .  j  av a  2  s . c om*/
    if (null != this.idSignedProperties) {
        signedPropertiesId = this.idSignedProperties;
    } else {
        signedPropertiesId = signatureId + "-xades";
    }
    signedProperties.setId(signedPropertiesId);
    qualifyingProperties.setSignedProperties(signedProperties);

    // SignedSignatureProperties
    SignedSignaturePropertiesType signedSignatureProperties = this.xadesObjectFactory
            .createSignedSignaturePropertiesType();
    signedProperties.setSignedSignatureProperties(signedSignatureProperties);

    // SigningTime
    GregorianCalendar signingTime = new GregorianCalendar(TimeZone.getTimeZone("Z"));
    Date currentClockValue = this.clock.getTime();
    signingTime.setTime(currentClockValue);
    XMLGregorianCalendar xmlGregorianCalendar = this.datatypeFactory.newXMLGregorianCalendar(signingTime);
    xmlGregorianCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    signedSignatureProperties.setSigningTime(xmlGregorianCalendar);

    // SigningCertificate
    if (null == signingCertificateChain || signingCertificateChain.isEmpty()) {
        throw new RuntimeException("no signing certificate chain available");
    }
    X509Certificate signingCertificate = signingCertificateChain.get(0);
    CertIDType signingCertificateId = getCertID(signingCertificate, this.xadesObjectFactory,
            this.xmldsigObjectFactory, this.digestAlgorithm, this.issuerNameNoReverseOrder);
    CertIDListType signingCertificates = this.xadesObjectFactory.createCertIDListType();
    signingCertificates.getCert().add(signingCertificateId);
    signedSignatureProperties.setSigningCertificate(signingCertificates);

    // ClaimedRole
    if (null != this.role && false == this.role.isEmpty()) {
        SignerRoleType signerRole = this.xadesObjectFactory.createSignerRoleType();
        signedSignatureProperties.setSignerRole(signerRole);
        ClaimedRolesListType claimedRolesList = this.xadesObjectFactory.createClaimedRolesListType();
        signerRole.setClaimedRoles(claimedRolesList);
        AnyType claimedRole = this.xadesObjectFactory.createAnyType();
        claimedRole.getContent().add(this.role);
        claimedRolesList.getClaimedRole().add(claimedRole);
    }

    // XAdES-EPES
    if (null != this.signaturePolicyService) {
        SignaturePolicyIdentifierType signaturePolicyIdentifier = this.xadesObjectFactory
                .createSignaturePolicyIdentifierType();
        signedSignatureProperties.setSignaturePolicyIdentifier(signaturePolicyIdentifier);

        SignaturePolicyIdType signaturePolicyId = this.xadesObjectFactory.createSignaturePolicyIdType();
        signaturePolicyIdentifier.setSignaturePolicyId(signaturePolicyId);

        ObjectIdentifierType objectIdentifier = this.xadesObjectFactory.createObjectIdentifierType();
        signaturePolicyId.setSigPolicyId(objectIdentifier);
        IdentifierType identifier = this.xadesObjectFactory.createIdentifierType();
        objectIdentifier.setIdentifier(identifier);
        identifier.setValue(this.signaturePolicyService.getSignaturePolicyIdentifier());
        objectIdentifier.setDescription(this.signaturePolicyService.getSignaturePolicyDescription());

        byte[] signaturePolicyDocumentData = this.signaturePolicyService.getSignaturePolicyDocument();
        DigestAlgAndValueType sigPolicyHash = getDigestAlgAndValue(signaturePolicyDocumentData,
                this.xadesObjectFactory, this.xmldsigObjectFactory, this.digestAlgorithm);
        signaturePolicyId.setSigPolicyHash(sigPolicyHash);

        String signaturePolicyDownloadUrl = this.signaturePolicyService.getSignaturePolicyDownloadUrl();
        if (null != signaturePolicyDownloadUrl) {
            SigPolicyQualifiersListType sigPolicyQualifiers = this.xadesObjectFactory
                    .createSigPolicyQualifiersListType();
            signaturePolicyId.setSigPolicyQualifiers(sigPolicyQualifiers);

            AnyType sigPolicyQualifier = this.xadesObjectFactory.createAnyType();
            sigPolicyQualifiers.getSigPolicyQualifier().add(sigPolicyQualifier);

            JAXBElement<String> spUriElement = this.xadesObjectFactory.createSPURI(signaturePolicyDownloadUrl);
            sigPolicyQualifier.getContent().add(spUriElement);
        }
    } else if (this.signaturePolicyImplied) {
        SignaturePolicyIdentifierType signaturePolicyIdentifier = this.xadesObjectFactory
                .createSignaturePolicyIdentifierType();
        signedSignatureProperties.setSignaturePolicyIdentifier(signaturePolicyIdentifier);

        signaturePolicyIdentifier.setSignaturePolicyImplied("");
    }

    // DataObjectFormat
    if (false == this.dataObjectFormatMimeTypes.isEmpty()) {
        SignedDataObjectPropertiesType signedDataObjectProperties = this.xadesObjectFactory
                .createSignedDataObjectPropertiesType();
        signedProperties.setSignedDataObjectProperties(signedDataObjectProperties);

        List<DataObjectFormatType> dataObjectFormats = signedDataObjectProperties.getDataObjectFormat();
        for (Map.Entry<String, String> dataObjectFormatMimeType : this.dataObjectFormatMimeTypes.entrySet()) {
            DataObjectFormatType dataObjectFormat = this.xadesObjectFactory.createDataObjectFormatType();
            dataObjectFormat.setObjectReference("#" + dataObjectFormatMimeType.getKey());
            dataObjectFormat.setMimeType(dataObjectFormatMimeType.getValue());
            dataObjectFormats.add(dataObjectFormat);
        }
    }

    // marshall XAdES QualifyingProperties
    Node qualifyingPropertiesNode = marshallQualifyingProperties(document, this.xadesObjectFactory,
            qualifyingProperties);

    // add XAdES ds:Object
    List<XMLStructure> xadesObjectContent = new LinkedList<XMLStructure>();
    xadesObjectContent.add(new DOMStructure(qualifyingPropertiesNode));
    XMLObject xadesObject = signatureFactory.newXMLObject(xadesObjectContent, null, null, null);
    objects.add(xadesObject);

    // add XAdES ds:Reference
    DigestMethod digestMethod = signatureFactory.newDigestMethod(digestAlgorithm.getXmlAlgoId(), null);
    List<Transform> transforms = new LinkedList<Transform>();
    Transform exclusiveTransform = signatureFactory.newTransform(CanonicalizationMethod.INCLUSIVE,
            (TransformParameterSpec) null);
    transforms.add(exclusiveTransform);
    Reference reference = signatureFactory.newReference("#" + signedPropertiesId, digestMethod, transforms,
            XADES_TYPE, null);
    references.add(reference);
}

From source file:com.evolveum.midpoint.certification.impl.AccCertUpdateHelper.java

protected AccessCertificationStageType createStage(AccessCertificationCampaignType campaign,
        int requestedStageNumber) {
    AccessCertificationStageType stage = new AccessCertificationStageType(prismContext);
    stage.setNumber(requestedStageNumber);
    stage.setStart(XmlTypeConverter.createXMLGregorianCalendar(new Date()));

    AccessCertificationStageDefinitionType stageDef = CertCampaignTypeUtil.findStageDefinition(campaign,
            stage.getNumber());/*w ww .j  a  v a  2s .co  m*/
    XMLGregorianCalendar end = (XMLGregorianCalendar) stage.getStart().clone();
    if (stageDef.getDays() != null) {
        end.add(XmlTypeConverter.createDuration(true, 0, 0, stageDef.getDays(), 0, 0, 0));
    }
    end.setHour(23);
    end.setMinute(59);
    end.setSecond(59);
    end.setMillisecond(999);
    stage.setEnd(end);

    stage.setName(stageDef.getName());
    stage.setDescription(stageDef.getDescription());

    return stage;
}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileBES.java

protected QualifyingPropertiesType createXAdESQualifyingProperties(SignatureParameters params,
        String signedInfoId, List<Reference> documentReferences, Document document) {

    // QualifyingProperties
    QualifyingPropertiesType qualifyingProperties = xades13ObjectFactory.createQualifyingPropertiesType();

    SignedPropertiesType signedProperties = xades13ObjectFactory.createSignedPropertiesType();
    qualifyingProperties.setSignedProperties(signedProperties);

    signedProperties.setId(signedInfoId);

    SignedSignaturePropertiesType signedSignatureProperties = xades13ObjectFactory
            .createSignedSignaturePropertiesType();
    signedProperties.setSignedSignatureProperties(signedSignatureProperties);

    // SigningTime
    GregorianCalendar signingTime = new GregorianCalendar(TimeZone.getTimeZone("Z"));
    signingTime.setTime(params.getSigningDate());

    XMLGregorianCalendar xmlGregorianCalendar = getDataFactory().newXMLGregorianCalendar(signingTime);
    xmlGregorianCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    signedSignatureProperties.setSigningTime(xmlGregorianCalendar);

    X509Certificate signingCertificate = params.getSigningCertificate();
    CertIDType signingCertificateId = getCertID(signingCertificate);
    CertIDListType signingCertificates = xades13ObjectFactory.createCertIDListType();
    signingCertificates.getCert().add(signingCertificateId);
    signedSignatureProperties.setSigningCertificate(signingCertificates);

    // DataObjectProperties
    SignedDataObjectPropertiesType dataObjectProperties = new SignedDataObjectPropertiesType();
    Iterator<Reference> refIt = documentReferences.iterator();
    Iterator<Document> docIt = documentIterator(document);
    while (refIt.hasNext() && docIt.hasNext()) {
        Reference ref = refIt.next();
        Document doc = docIt.next();
        if (ref.getId() != null && doc.getMimeType() != null) {
            DataObjectFormatType dataFormat = new DataObjectFormatType();
            dataFormat.setObjectReference("#" + ref.getId());
            dataFormat.setMimeType(doc.getMimeType().getCode());
            dataObjectProperties.getDataObjectFormat().add(dataFormat);
        }//from ww w . j a  v  a 2s  . c o m
    }
    if (dataObjectProperties.getDataObjectFormat().size() > 0) {
        signedProperties.setSignedDataObjectProperties(dataObjectProperties);
    }

    // SignerRole
    if (params.getClaimedSignerRole() != null) {
        SignerRoleType signerRole = xades13ObjectFactory.createSignerRoleType();
        ClaimedRolesListType claimedRoles = xades13ObjectFactory.createClaimedRolesListType();

        /*
         * Add only one role
         */
        AnyType role = xades13ObjectFactory.createAnyType();
        role.getContent().add(params.getClaimedSignerRole());
        claimedRoles.getClaimedRole().add(role);

        signerRole.setClaimedRoles(claimedRoles);

        signedSignatureProperties.setSignerRole(signerRole);
    }

    return qualifyingProperties;
}

From source file:be.fedict.eid.tsl.TrustServiceList.java

public void addXadesBes(XMLSignatureFactory signatureFactory, Document document, String signatureId,
        X509Certificate signingCertificate, List<Reference> references, List<XMLObject> objects)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    LOG.debug("preSign");

    // QualifyingProperties
    QualifyingPropertiesType qualifyingProperties = this.xadesObjectFactory.createQualifyingPropertiesType();
    qualifyingProperties.setTarget("#" + signatureId);

    // SignedProperties
    SignedPropertiesType signedProperties = this.xadesObjectFactory.createSignedPropertiesType();
    String signedPropertiesId = signatureId + "-xades";
    signedProperties.setId(signedPropertiesId);
    qualifyingProperties.setSignedProperties(signedProperties);

    // SignedSignatureProperties
    SignedSignaturePropertiesType signedSignatureProperties = this.xadesObjectFactory
            .createSignedSignaturePropertiesType();
    signedProperties.setSignedSignatureProperties(signedSignatureProperties);

    // SigningTime
    GregorianCalendar signingTime = new GregorianCalendar();
    signingTime.setTimeZone(TimeZone.getTimeZone("Z"));
    XMLGregorianCalendar xmlSigningTime = this.datatypeFactory.newXMLGregorianCalendar(signingTime);
    xmlSigningTime.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    signedSignatureProperties.setSigningTime(xmlSigningTime);

    // SigningCertificate
    CertIDListType signingCertificates = this.xadesObjectFactory.createCertIDListType();
    CertIDType signingCertificateId = this.xadesObjectFactory.createCertIDType();

    X509IssuerSerialType issuerSerial = this.xmldsigObjectFactory.createX509IssuerSerialType();
    issuerSerial.setX509IssuerName(signingCertificate.getIssuerX500Principal().toString());
    issuerSerial.setX509SerialNumber(signingCertificate.getSerialNumber());
    signingCertificateId.setIssuerSerial(issuerSerial);

    DigestAlgAndValueType certDigest = this.xadesObjectFactory.createDigestAlgAndValueType();
    DigestMethodType jaxbDigestMethod = this.xmldsigObjectFactory.createDigestMethodType();
    jaxbDigestMethod.setAlgorithm(DigestMethod.SHA256);
    certDigest.setDigestMethod(jaxbDigestMethod);
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    byte[] digestValue;
    try {//from   ww w  .  j  a  v a2  s  .c o  m
        digestValue = messageDigest.digest(signingCertificate.getEncoded());
    } catch (CertificateEncodingException e) {
        throw new RuntimeException("certificate encoding error: " + e.getMessage(), e);
    }
    certDigest.setDigestValue(digestValue);
    signingCertificateId.setCertDigest(certDigest);

    signingCertificates.getCert().add(signingCertificateId);
    signedSignatureProperties.setSigningCertificate(signingCertificates);

    // marshall XAdES QualifyingProperties
    Node qualifyingPropertiesNode = marshallQualifyingProperties(document, qualifyingProperties);

    // add XAdES ds:Object
    List<XMLStructure> xadesObjectContent = new LinkedList<XMLStructure>();
    xadesObjectContent.add(new DOMStructure(qualifyingPropertiesNode));
    XMLObject xadesObject = signatureFactory.newXMLObject(xadesObjectContent, null, null, null);
    objects.add(xadesObject);

    // add XAdES ds:Reference
    DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA256, null);
    List<Transform> transforms = new LinkedList<Transform>();
    Transform exclusiveTransform = signatureFactory.newTransform(CanonicalizationMethod.EXCLUSIVE,
            (TransformParameterSpec) null);
    transforms.add(exclusiveTransform);
    Reference reference = signatureFactory.newReference("#" + signedPropertiesId, digestMethod, transforms,
            XADES_TYPE, null);
    references.add(reference);
}

From source file:org.openvpms.esci.adapter.map.invoice.UBLInvoice.java

/**
 * Returns the invoice issue date/time.//from w w w .  j a v  a2  s  .com
 *
 * @return the issue date/time
 * @throws ESCIAdapterException if the issue date isn't set
 */
public Date getIssueDatetime() {
    IssueDateType issueDate = getRequired(invoice.getIssueDate(), "IssueDate");
    XMLGregorianCalendar calendar = issueDate.getValue();
    checkRequired(calendar, "IssueDate");

    IssueTimeType issueTime = invoice.getIssueTime();
    if (issueTime != null) {
        XMLGregorianCalendar time = issueTime.getValue();
        if (time != null) {
            calendar.setHour(time.getHour());
            calendar.setMinute(time.getMinute());
            calendar.setSecond(time.getSecond());
            calendar.setMillisecond(time.getMillisecond());
        }
    }
    return calendar.toGregorianCalendar().getTime();
}

From source file:org.orcid.utils.DateUtils.java

public static XMLGregorianCalendar convertToXMLGregorianCalendarNoTimeZoneNoMillis(Date date) {
    XMLGregorianCalendar basicCalender = convertToXMLGregorianCalendar(date);
    basicCalender.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    basicCalender.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    return basicCalender;
}