Search in sources :

Example 1 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ORCID-Source by ORCID.

the class ValidateV2_1IdentifiersTest method marshall.

/** Marshals to a JAXBSource backed by SAX
     * 
     * @param type
     * @param obj
     * @return
     */
private Source marshall(Class<?> type, Object obj) {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        JAXBSource source = new JAXBSource(context, obj);
        return source;
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBSource(javax.xml.bind.util.JAXBSource)

Example 2 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ORCID-Source by ORCID.

the class ValidateV2RC4Identifiers method marshall.

/** Marshals to a JAXBSource backed by SAX
     * 
     * @param type
     * @param obj
     * @return
     */
private Source marshall(Class<?> type, Object obj) {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        JAXBSource source = new JAXBSource(context, obj);
        return source;
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBSource(javax.xml.bind.util.JAXBSource)

Example 3 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ORCID-Source by ORCID.

the class ValidateV2IdentifiersTest method marshall.

/** Marshals to a JAXBSource backed by SAX
     * 
     * @param type
     * @param obj
     * @return
     */
private Source marshall(Class<?> type, Object obj) {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        JAXBSource source = new JAXBSource(context, obj);
        return source;
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBSource(javax.xml.bind.util.JAXBSource)

Example 4 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ORCID-Source by ORCID.

the class ValidateV2RC1SamplesTest method validateGroupIdValue.

@Test
public void validateGroupIdValue() throws SAXException, IOException, JAXBException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/group-id-2.0_rc1/group-id-2.0_rc1.xsd"));
    Validator validator = schema.newValidator();
    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char[] upperAlphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray();
    char[] numbers = "0123456789".toCharArray();
    char[] validCharacters = "^._~:/?#[]@!$&'()*+,;=-".toCharArray();
    //All valid characters
    char[] allValid = ArrayUtils.addAll(alphabet, upperAlphabet);
    allValid = ArrayUtils.addAll(allValid, numbers);
    allValid = ArrayUtils.addAll(allValid, validCharacters);
    String invalidCharactersString = "{}\"<>\\";
    char[] invalidCharacters = invalidCharactersString.toCharArray();
    //All valid and non valid characters
    char[] allWithInvalids = ArrayUtils.addAll(allValid, invalidCharacters);
    GroupIdRecord g1 = new GroupIdRecord();
    g1.setDescription("description");
    g1.setType("newsletter");
    System.out.println("Validating group_id agains a list of 3000 valid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allValid);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
        } catch (Exception e) {
            fail("fail validating: " + randomValue + " on iteration " + i);
        }
    }
    System.out.println("Validating group_id agains a list of 3000 invalid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allWithInvalids);
        boolean regenerateString = true;
        do {
            for (int j = 0; j < randomValue.length(); j++) {
                if (invalidCharactersString.contains(String.valueOf(randomValue.charAt(j)))) {
                    regenerateString = false;
                    break;
                }
            }
            if (regenerateString) {
                randomValue += RandomStringUtils.random(3, invalidCharacters);
                regenerateString = false;
            }
        } while (regenerateString);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
            fail(randomValue + " should not be vaild according to the XSD on iteration " + i);
        } catch (Exception e) {
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) GroupIdRecord(org.orcid.jaxb.model.groupid_rc1.GroupIdRecord) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) Validator(javax.xml.validation.Validator) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) JAXBSource(javax.xml.bind.util.JAXBSource) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Example 5 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ORCID-Source by ORCID.

the class ValidateV2RC3Identifiers method marshall.

/** Marshals to a JAXBSource backed by SAX
     * 
     * @param type
     * @param obj
     * @return
     */
private Source marshall(Class<?> type, Object obj) {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        JAXBSource source = new JAXBSource(context, obj);
        return source;
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBSource(javax.xml.bind.util.JAXBSource)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)8 JAXBSource (javax.xml.bind.util.JAXBSource)8 JAXBException (javax.xml.bind.JAXBException)6 Test (org.junit.Test)3 IOException (java.io.IOException)2 JAXBElement (javax.xml.bind.JAXBElement)2 QName (javax.xml.namespace.QName)2 Source (javax.xml.transform.Source)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Schema (javax.xml.validation.Schema)2 Validator (javax.xml.validation.Validator)2 MotherElement (org.codice.ddf.parser.xml.domain.MotherElement)2 SAXException (org.xml.sax.SAXException)2 WebApplicationException (javax.ws.rs.WebApplicationException)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 GroupIdRecord (org.orcid.jaxb.model.groupid_rc1.GroupIdRecord)1 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)1