示例#1
0
 public static Customer[] getAllCustomers() {
   Mapping mapping = new Mapping();
   try {
     mapping.loadMapping(
         new InputSource(
             Thread.currentThread()
                 .getContextClassLoader()
                 .getResource("mappings.xml")
                 .getFile()));
     Unmarshaller unmarshaller = new Unmarshaller(mapping);
     unmarshaller.setIgnoreExtraElements(true);
     CustomersContainer customersContainer =
         (CustomersContainer)
             unmarshaller.unmarshal(
                 new InputSource(
                     Thread.currentThread()
                         .getContextClassLoader()
                         .getResource("Customers.xml")
                         .getFile()));
     Customer[] customers = new Customer[customersContainer.getCustomers().size()];
     for (int i = 0; i < customersContainer.getCustomers().size(); i++) {
       customers[i] = customersContainer.getCustomers().get(i);
     }
     return customers;
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  public synchronized Object fromXML(java.io.Reader input) throws XMLUtilityException {
    Object beanObject;

    org.exolab.castor.xml.Unmarshaller unmarshaller = null;
    try {
      log.debug("Creating unmarshaller");
      unmarshaller = new org.exolab.castor.xml.Unmarshaller(this.getMapping());
    } catch (MappingException e) {
      log.error("XML mapping file is invalid: ", e);
      throw new XMLUtilityException("XML mapping file invalid: ", e);
    } catch (Exception e) {
      log.error("General Exception caught trying to create unmarshaller: ", e);
      throw new XMLUtilityException("General Exception caught trying to create unmarshaller: ", e);
    }

    try {
      log.debug("About to unmarshal from input ");
      beanObject = unmarshaller.unmarshal(input);
    } catch (MarshalException e) {
      log.error("Error marshalling input: ", e);
      throw new XMLUtilityException("Error unmarshalling xml input: " + e.getMessage(), e);
    } catch (ValidationException e) {
      log.error("Error in xml validation when unmarshalling xml input: ", e);
      throw new XMLUtilityException("Error in xml validation when unmarshalling xml input: ", e);
    }
    return beanObject;
  }
  public <T> T sendRequest(Class<T> cls, Object request) throws WasabiNetClientException {
    StringWriter sw = new StringWriter();
    String convertida = null;
    String received = null;
    try {
      marshaller.setWriter(sw);
      marshaller.marshal(request);

      convertida = new String(sw.getBuffer().toString().getBytes("UTF8"), "UTF8");
      System.out.println(convertida);

      received = getData(serviceUrl, convertida);

      System.out.println("---" + received + "---");

      unmarshaller.setClass(cls);

      return (T) unmarshaller.unmarshal(new StringReader(received));

    } catch (IOException e) {
      throw new WasabiNetClientException("Error de lectura/escritura:" + e.getMessage());
    } catch (MarshalException e) {
      e.printStackTrace();
      throw new WasabiNetClientException(
          "Error al armar la respuesta:" + e.getMessage(), convertida, received);
    } catch (ValidationException e) {
      throw new WasabiNetClientException(
          "Error en la validacion de la respuesta:" + e.getMessage(), convertida, received);
    } // catch WasabiNetClientException -> Esta directamente salta al metodo que ha llamado a
      // sendrequest
  }
示例#4
0
 @Test
 public void validateCastorObjectAgainstSchema() throws Exception {
   org.exolab.castor.xml.Unmarshaller unmarshaller = CastorUtils.getUnmarshaller(getSampleClass());
   unmarshaller.setValidation(true);
   @SuppressWarnings("unchecked")
   T obj = (T) unmarshaller.unmarshal(new InputSource(getSampleXmlInputStream()));
   assertNotNull(obj);
 }
示例#5
0
 /** Loads the test spec (castor classes) from an xml file. */
 public PubSubScenario(Logger logger, File xmlFile, boolean validateXml)
     throws FileNotFoundException, IOException, MarshalException, ValidationException {
   this.logger = logger;
   if (xmlFile.exists() && !xmlFile.isDirectory() && xmlFile.canRead()) {
     Unmarshaller unm = new Unmarshaller(PubSubInfrastructureSpec.class);
     unm.setValidation(validateXml);
     spec = (PubSubInfrastructureSpec) unm.unmarshal(new FileReader(xmlFile));
   } else {
     throw new FileNotFoundException(xmlFile.getAbsolutePath());
   }
 }
示例#6
0
 public static Object read(InputStream is, XMLContext context) {
   try {
     Unmarshaller unmarshaller =
         context.createUnmarshaller(); // FIXME initialization is not thread safe
     unmarshaller.setWhitespacePreserve(true);
     Object object = unmarshaller.unmarshal(new InputSource(is));
     return object;
   } catch (MarshalException e) {
     throw new JRRuntimeException(e);
   } catch (ValidationException e) {
     throw new JRRuntimeException(e);
   }
 }
  @Test
  public void testCodingSchemeRevisions() throws Exception {

    URI sourceURI = new File("src/test/resources/csRevision/Automobiles2010_Test_CS.xml").toURI();

    org.exolab.castor.xml.Unmarshaller um =
        new org.exolab.castor.xml.Unmarshaller(SystemRelease.class);
    SystemRelease systemRelease =
        (SystemRelease)
            um.unmarshal(
                new InputStreamReader(sourceURI.toURL().openConnection().getInputStream()));

    service.loadSystemRelease(systemRelease, true);
  }
  @Test
  public void testVSPropertyRevisions() throws Exception {

    URI sourceURI = new File("src/test/resources/vsplRevision/vsplProperty_test.xml").toURI();

    org.exolab.castor.xml.Unmarshaller um =
        new org.exolab.castor.xml.Unmarshaller(SystemRelease.class);
    SystemRelease systemRelease =
        (SystemRelease)
            um.unmarshal(
                new InputStreamReader(sourceURI.toURL().openConnection().getInputStream()));

    service.loadSystemRelease(systemRelease, null);
  }
 /** @param reader */
 public static org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue unmarshal(
     java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue)
       Unmarshaller.unmarshal(
           org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue.class, reader);
 } // -- org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue unmarshal(java.io.Reader)
 /**
  * Method unmarshalXSDataCollectionPlan.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled org.edna.mxv1.xsdata.XSDataCollectionPlan
  */
 public static org.edna.mxv1.xsdata.XSDataCollectionPlan unmarshalXSDataCollectionPlan(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (org.edna.mxv1.xsdata.XSDataCollectionPlan)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           org.edna.mxv1.xsdata.XSDataCollectionPlan.class, reader);
 }
 /** @param reader */
 public static com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg unmarshal(
     java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg)
       Unmarshaller.unmarshal(
           com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg.class, reader);
 } // -- com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg
示例#12
0
 /**
  * Method unmarshalEnquiryRq.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled bf.com.misys.eqf.types.header.EnquiryRq
  */
 public static bf.com.misys.eqf.types.header.EnquiryRq unmarshalEnquiryRq(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (bf.com.misys.eqf.types.header.EnquiryRq)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           bf.com.misys.eqf.types.header.EnquiryRq.class, reader);
 }
 /**
  * Method unmarshalUpdateEsfStatusRqHeader.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled bf.com.misys.eqf.types.header.UpdateEsfStatusRqHeader
  */
 public static bf.com.misys.eqf.types.header.UpdateEsfStatusRqHeader
     unmarshalUpdateEsfStatusRqHeader(final java.io.Reader reader)
         throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (bf.com.misys.eqf.types.header.UpdateEsfStatusRqHeader)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           bf.com.misys.eqf.types.header.UpdateEsfStatusRqHeader.class, reader);
 }
示例#14
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    CResultadoOperacion resultado = null;
    super.doPost(request);

    try {

      Entidad entidad =
          (Entidad)
              Unmarshaller.unmarshal(
                  Entidad.class, new StringReader(request.getParameter(EnviarSeguro.mensajeXML)));
      Marshaller.marshal(entidad, new OutputStreamWriter(System.out));
      resultado = COperacionesAdministrador.ejecutarDeleteEntidad(entidad);
    } catch (Exception e) {
      java.io.StringWriter sw = new java.io.StringWriter();
      java.io.PrintWriter pw = new java.io.PrintWriter(sw);
      e.printStackTrace(pw);
      logger.error("Excepción al eliminar la ENTIDAD: " + sw.toString());
      resultado =
          new CResultadoOperacion(false, "Excepción al eliminar una entidad:" + e.toString());
    }
    Writer writer = response.getWriter();
    writer.write(resultado.buildResponse());
    writer.flush();
    writer.close();
  }
示例#15
0
 /**
  * Method unmarshal.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled org.opennms.netmgt.config.reporting.opennms.OpennmsReports
  */
 public static org.opennms.netmgt.config.reporting.opennms.OpennmsReports unmarshal(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (org.opennms.netmgt.config.reporting.opennms.OpennmsReports)
       Unmarshaller.unmarshal(
           org.opennms.netmgt.config.reporting.opennms.OpennmsReports.class, reader);
 }
示例#16
0
 /**
  * Method unmarshal.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled cto.framework.service.plugin.schema.ReturnType
  */
 public static cto.framework.service.plugin.schema.ReturnType unmarshal(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (cto.framework.service.plugin.schema.ReturnType)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           cto.framework.service.plugin.schema.ReturnType.class, reader);
 }
示例#17
0
 /**
  * Method unmarshal.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled cto.framework.web.action.plugin.schema.Validator
  */
 public static cto.framework.web.action.plugin.schema.Validator unmarshal(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (cto.framework.web.action.plugin.schema.Validator)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           cto.framework.web.action.plugin.schema.Validator.class, reader);
 }
示例#18
0
 /**
  * Method unmarshalExtensionDataRs.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled bf.com.misys.eqf.types.header.ExtensionDataRs
  */
 public static bf.com.misys.eqf.types.header.ExtensionDataRs unmarshalExtensionDataRs(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (bf.com.misys.eqf.types.header.ExtensionDataRs)
       org.exolab.castor.xml.Unmarshaller.unmarshal(
           bf.com.misys.eqf.types.header.ExtensionDataRs.class, reader);
 }
 /**
  * Method unmarshal.
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled es.pode.parseadorXML.lomes.castor.ComplexTypeClassification
  */
 public static es.pode.parseadorXML.castor.ComplexTypeClassification unmarshal(
     final java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (es.pode.parseadorXML.castor.ComplexTypeClassification)
       Unmarshaller.unmarshal(
           es.pode.parseadorXML.castor.GroupClassificationClassification.class, reader);
 }
 /** @param reader */
 public static com.cisco.eManager.common.process.GetProcessStatusForResp unmarshal(
     java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (com.cisco.eManager.common.process.GetProcessStatusForResp)
       Unmarshaller.unmarshal(
           com.cisco.eManager.common.process.GetProcessStatusForResp.class, reader);
 } // -- com.cisco.eManager.common.process.GetProcessStatusForResp unmarshal(java.io.Reader)
示例#21
0
  public List<EventReply> receive() throws IOException {
    byte[] buf = new byte[0xff];
    DatagramPacket packet = new DatagramPacket(buf, 0, 0xff);
    m_dgSocket.receive(packet);
    byte[] data = packet.getData();
    //		System.out.println("receive data is : "+new String(data,"utf-8"));
    StringReader reader = new StringReader(new String(data, 0, packet.getLength(), "utf-8"));
    EventReceipt receipt = null;
    try {

      receipt = (EventReceipt) Unmarshaller.unmarshal(EventReceipt.class, reader);
    } catch (MarshalException e) {
      e.printStackTrace();
    } catch (ValidationException e) {
      e.printStackTrace();
    }

    InetAddress remoteAddr = packet.getAddress();
    int port = packet.getPort();
    List<EventReply> replys = new ArrayList<EventReply>();
    if (receipt != null) {
      String[] uuids = receipt.getUuid();
      for (String uuid : uuids) {
        EventReply reply = new EventReply(remoteAddr, port, uuid);
        replys.add(reply);
      }
    }
    return replys;
  }
示例#22
0
 /**
  * Method unmarshal
  *
  * @param reader
  * @throws org.exolab.castor.xml.MarshalException if object is null or if any SAXException is
  *     thrown during marshaling
  * @throws org.exolab.castor.xml.ValidationException if this object is an invalid instance
  *     according to the schema
  * @return the unmarshaled org.exolab.castor.tests.framework.testDescriptor.ConfigurationType
  */
 public static org.exolab.castor.tests.framework.testDescriptor.ConfigurationType unmarshal(
     java.io.Reader reader)
     throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
   return (org.exolab.castor.tests.framework.testDescriptor.ConfigurationType)
       Unmarshaller.unmarshal(
           org.exolab.castor.tests.framework.testDescriptor.Unmarshal.class, reader);
 } // -- org.exolab.castor.tests.framework.testDescriptor.ConfigurationType
示例#23
0
  public static Object read(Node node, Mapping mapping) {
    Object object = null;

    try {
      Unmarshaller unmarshaller = new Unmarshaller(mapping);
      unmarshaller.setWhitespacePreserve(true);
      object = unmarshaller.unmarshal(node);
    } catch (MappingException e) {
      throw new JRRuntimeException(e);
    } catch (MarshalException e) {
      throw new JRRuntimeException(e);
    } catch (ValidationException e) {
      throw new JRRuntimeException(e);
    }

    return object;
  }
示例#24
0
 /**
  * Creates castor object using mapping configuration and mock xml file
  *
  * @param path Path to mock xml file
  * @return Generated object from xml file
  */
 private static Object createCastorObject(String path) {
   Object resultObject = null;
   try {
     Mapping mapping = new Mapping();
     mapping.loadMapping(CASTOR_MAPPING_FILE_PATH);
     Unmarshaller unmarshaller = new Unmarshaller(mapping);
     resultObject = unmarshaller.unmarshal(new InputSource(new FileReader(path)));
   } catch (MarshalException exception) {
     LOGGER.error(exception);
   } catch (ValidationException exception) {
     LOGGER.error(exception);
   } catch (MappingException exception) {
     LOGGER.error(exception);
   } catch (IOException exception) {
     LOGGER.error(exception);
   }
   return resultObject;
 }
示例#25
0
  public static Object read(InputStream is, Mapping mapping) {
    Object object = null;

    try {
      Unmarshaller unmarshaller =
          new Unmarshaller(mapping); // FIXME initialization is not thread safe
      unmarshaller.setWhitespacePreserve(true);
      object = unmarshaller.unmarshal(new InputSource(is));
    } catch (MappingException e) {
      throw new JRRuntimeException(e);
    } catch (MarshalException e) {
      throw new JRRuntimeException(e);
    } catch (ValidationException e) {
      throw new JRRuntimeException(e);
    }

    return object;
  }
示例#26
0
  public void testGetUnmarshaller() throws Exception {
    String resource = getClass().getClassLoader().getResource("input.xml").toExternalForm();
    this.unmarshaller = (Unmarshaller) this.context.getBean("unmarshaller");
    assertNotNull(this.unmarshaller);

    Product product = (Product) unmarshaller.unmarshal(new InputSource(resource));
    assertNotNull(product);
    assertEquals(1, product.getId());
    assertEquals("blah", product.getName());
  }
  private static <T> T unmarshal(Class<?> elementClass, String body, Class<T> resultClass) {
    verifyStartsAsXML(body);
    body = removeBadTags(elementClass, body);

    String configFile = fromRedmineMap.get(elementClass);
    Unmarshaller unmarshaller = getUnmarshaller(configFile, resultClass);

    StringReader reader = null;
    try {
      reader = new StringReader(body);
      return resultClass.cast(unmarshaller.unmarshal(reader));
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }
示例#28
0
  /**
   * TDTEngine - constructor for a new Tag Data Translation engine
   *
   * @param confdir the string value of the path to a configuration directory consisting of two
   *     subdirectories, <code>schemes</code> and <code>auxiliary</code>.
   *     <p>When the class TDTEngine is constructed, the path to a local directory must be
   *     specified, by passing it as a single string parameter to the constructor method (without
   *     any trailing slash or file separator). e.g. <code>
   *     <pre>TDTEngine engine = new TDTEngine("/opt/TDT");</pre></code>
   *     <p>The specified directory must contain two subdirectories named auxiliary and schemes. The
   *     Tag Data Translation definition files for the various coding schemes should be located
   *     inside the subdirectory called <code>schemes</code>. Any auxiliary lookup files (such as
   *     <code>ManagerTranslation.xml</code>) should be located inside the subdirectory called
   *     <code>auxiliary</code>.
   *     <p>Files within the schemes directory ending in <code>.xml</code> are read in and
   *     unmarshalled using <a href = "http://www.castor.org">Castor</a>.
   */
  public TDTEngine(String confdir)
      throws FileNotFoundException, MarshalException, ValidationException, TDTException {
    xmldir = confdir;

    long t = System.currentTimeMillis();
    File[] dir =
        new java.io.File(confdir + File.separator + "schemes").listFiles(new XMLFilenameFilter());
    // java.util.Arrays.sort(dir); // Sort it
    if (dir == null) throw new TDTException("Cannot find schemes in " + confdir);
    Unmarshaller unmar = new Unmarshaller();
    for (File f : dir) {
      EpcTagDataTranslation tdt =
          (EpcTagDataTranslation) unmar.unmarshal(EpcTagDataTranslation.class, new FileReader(f));

      initFromTDT(tdt);
    }

    // need to populate the hashmap gs1cpi from the ManagerTranslation.xml table in auxiliary.
    // Unmarshaller unmar = new Unmarshaller();
    GEPC64Table cpilookup =
        (GEPC64Table)
            unmar.unmarshal(
                GEPC64Table.class,
                new FileReader(
                    confdir
                        + File.separator
                        + "auxiliary"
                        + File.separator
                        + "ManagerTranslation.xml"));
    for (Enumeration e = cpilookup.enumerateEntry(); e.hasMoreElements(); ) {
      Entry entry = (Entry) e.nextElement();
      String comp = entry.getCompanyPrefix();
      String indx = Integer.toString(entry.getIndex());
      gs1cpi.put(indx, comp);
      gs1cpi.put(comp, indx);
    }

    // System.out.println("Loaded schemas in " +
    //		   (System.currentTimeMillis() - t)
    //		   + " millisecs");

  }
  private static Unmarshaller getUnmarshaller(String configFile, Class<?> classToUse) {
    //		String configFile = configFilesMap.get(classToUse);
    InputSource inputSource =
        new InputSource(RedmineXMLParser.class.getResourceAsStream(configFile));
    ClassLoader cl = RedmineXMLParser.class.getClassLoader();
    // Note: Castor XML is packed in a separate OSGI bundle, so
    // must set the classloader so that Castor will see our classes
    Mapping mapping = new Mapping(cl);
    mapping.loadMapping(inputSource);

    Unmarshaller unmarshaller;
    try {
      unmarshaller = new Unmarshaller(mapping);
    } catch (MappingException e) {
      throw new RuntimeException(e);
    }
    unmarshaller.setClass(classToUse);
    unmarshaller.setWhitespacePreserve(true);
    return unmarshaller;
  }
示例#30
0
 public FlatFileRecord(File descriptor, char[] record)
     throws MarshalException, ValidationException, FileNotFoundException {
   FlatRecord fr =
       (FlatRecord) Unmarshaller.unmarshal(FlatRecord.class, new FileReader(descriptor));
   _fieldMap = new HashMap();
   FlatField[] fields = fr.getFlatField();
   for (int i = 0; i < fields.length; i++) {
     _fieldMap.put(fields[i].getName(), fields[i]);
   }
   _record = record;
 }