Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.labs64.utils.swid.support;
 
 
 
Support class provides convenient methods for working with JAXB.
 
 public final class JAXBUtils {
 
     public static <T> T readObject(final String resourcefinal Class<T> expectedType) {
         return readObjectFromInputStream(JAXBUtils.class.getClassLoader().getResourceAsStream(resource), expectedType);
     }
 
     public static <T> T readObjectFromString(final String contentfinal Class<T> expectedType) {
         return readObjectFromInputStream(new ByteArrayInputStream(content.getBytes()), expectedType);
     }
 
     public static <T> T readObjectFromInputStream(final InputStream inputStreamfinal Class<T> expectedType) {
         try {
             JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
             JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
             return element.getValue();
         } catch (final JAXBException e) {
             throw new SwidException("Cannot process resource."e);
         }
     }

    
Write XML entity to the given destination.

Parameters:
entity XML entity
destination destination to write to. Supported destinations: java.io.OutputStream, java.io.File, java.io.Writer
<T> JAXB entity
Throws:
java.lang.IllegalArgumentException
com.labs64.utils.swid.exception.SwidException
 
     public static <T> void writeObject(final T entityfinal Object destination) {
         try {
             JAXBContext jaxbContext;
             if (entity instanceof JAXBElement) {
                 jaxbContext = JAXBContext.newInstance(((JAXBElemententity).getValue().getClass());
             } else {
                 jaxbContext = JAXBContext.newInstance(entity.getClass());
             }
 
             Marshaller marshaller = jaxbContext.createMarshaller();
             marshaller.setProperty(.true);
 
             if (destination instanceof java.io.OutputStream) {
                 marshaller.marshal(entity, (OutputStreamdestination);
             } else if (destination instanceof java.io.File) {
                 marshaller.marshal(entity, (java.io.Filedestination);
             } else if (destination instanceof java.io.Writer) {
                 marshaller.marshal(entity, (java.io.Writerdestination);
             } else {
                 throw new IllegalArgumentException("Unsupported destination.");
             }
         } catch (final JAXBException e) {
             throw new SwidException("Cannot write object."e);
         }
     }

    
Write XML entity to the string.

    public static <T> String writeObjectToString(final T entity) {
        ByteArrayOutputStream destination = new ByteArrayOutputStream();
        writeObject(entitydestination);
        return destination.toString();
    }
New to GrepCode? Check out our FAQ X