Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    HumansList list = new HumansList();
    Person parent1 = new Person("parent1");
    list.addHuman(parent1);/*ww  w. j av a  2s . c  o m*/
    Person child11 = new Person("child11");
    list.addHuman(child11);
    Person child12 = new Person("child12");
    list.addHuman(child12);

    Person parent2 = new Person("parent2");
    list.addHuman(parent2);
    Person child21 = new Person("child21");
    list.addHuman(child21);
    Person child22 = new Person("child22");
    list.addHuman(child22);

    JAXBContext context = JAXBContext.newInstance(HumansList.class);
    Marshaller m = context.createMarshaller();
    StringWriter xml = new StringWriter();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

    m.marshal(list, xml);
    System.out.println(xml);
}

From source file:org.jasig.portlet.data.Importer.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//from w w  w.j  a v  a2s.  c o m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);
    Object service = context.getBean(serviceBeanName);

    JAXBContext jc = JAXBContext.newInstance(modelClass);

    File folder = new File(dir);
    File[] files = folder.listFiles(new ImportFileFilter());

    for (File f : files) {
        StreamSource xml = new StreamSource(f.getAbsoluteFile());
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass);
        Object object = je1.getValue();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass);
        method.invoke(service, object);
        transaction.commit();
    }
}

From source file:AuthenticationHeader.java

public static void main(String[] args) {

    try {/*from  w w  w  . ja  v  a2 s . c o  m*/
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        JAXBContext context = JAXBContext.newInstance(AuthenticationHeader.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(header, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MisspelledPersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    PersonName pn = new PersonName();
    pn.value = "foo";
    JAXBContext context = JAXBContext.newInstance(PersonName.class);
    context.createMarshaller().marshal(pn, System.out);
}

From source file:org.jasig.portlet.data.Exporter.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//from  w ww  . j  a va  2s. c  om
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);

    Object service = context.getBean(serviceBeanName);
    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(modelClass);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Method method = service.getClass().getMethod(serviceBeanMethodName);
    List<?> objects = (List<?>) method.invoke(service, null);

    for (Object o : objects) {
        session.lock(o, LockMode.NONE);
        JAXBElement je2 = new JAXBElement(new QName(modelClass.getSimpleName().toLowerCase()), modelClass, o);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:ListMObjects.java

public static void main(String[] args) {
    System.out.println("Executing List MObjects");
    try {//from   ww  w  . j  a va2  s .c  om
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsListMObjects request = new ParamsListMObjects();

        SuccessListMObjects result = port.listMObjects(request, header);
        JAXBContext context = JAXBContext.newInstance(SuccessListMObjects.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jasig.portlet.announcements.Exporter.java

public static void main(String[] args) throws Exception {

    String dir = args[0];/*  w  w  w. j a  v  a  2  s  .c om*/
    ApplicationContext context = PortletApplicationContextLocator
            .getApplicationContext(PortletApplicationContextLocator.DATABASE_CONTEXT_LOCATION);
    SessionFactory sessionFactory = context.getBean(SESSION_FACTORY_BEAN_NAME, SessionFactory.class);
    IAnnouncementService announcementService = context.getBean(ANNOUNCEMENT_SVC_BEAN_NAME,
            IAnnouncementService.class);

    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(Topic.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    List<Topic> topics = announcementService.getAllTopics();
    for (Topic topic : topics) {
        if (topic.getSubscriptionMethod() == 4) {
            continue;
        }

        session.lock(topic, LockMode.NONE);
        JAXBElement<Topic> je2 = new JAXBElement<Topic>(new QName("topic"), Topic.class, topic);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        System.out.println("Exporting Topic " + topic.getId() + " to file " + output);
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:DescribeMObject.java

public static void main(String[] args) {

    System.out.println("Executing Describe MObject");
    try {/*from ww  w  .j ava  2  s. c  o  m*/
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsDescribeMObject request = new ParamsDescribeMObject();
        request.setObjectName("ActivityRecord");

        SuccessDescribeMObject result = port.describeMObject(request, header);
        JAXBContext context = JAXBContext.newInstance(SuccessDescribeMObject.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:GetLead.java

public static void main(String[] args) {
    System.out.println("Executing Get Lead");
    try {//from  w w  w.j  ava  2s .c  o  m

        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();

        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsGetLead request = new ParamsGetLead();

        LeadKey key = new LeadKey();
        key.setKeyType(LeadKeyRef.EMAIL);
        key.setKeyValue("t@t.com");
        request.setLeadKey(key);

        SuccessGetLead result = port.getLead(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:GetImportToListStatus.java

public static void main(String[] args) {
    System.out.println("Executing Get Import To List Status");
    try {/*from   w  w w  . ja va 2s .  c o m*/
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsGetImportToListStatus request = new ParamsGetImportToListStatus();

        request.setProgramName("Trav-Demo-Program");
        request.setListName("Trav-Test-List");

        SuccessGetImportToListStatus result = port.getImportToListStatus(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetImportToListStatus.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}