示例#1
0
 public Source resolve(String href, String base) throws TransformerException {
   if (href == null || href.trim().length() == 0) {
     throw new TransformerException("href is null");
   }
   try {
     String aname = defaultXslStylesheetBase + href;
     if (aname.indexOf("..") != -1) {
       aname = new File(aname).getCanonicalPath().toString();
     }
     if (m_bundleContect.getBundle().getEntry(aname) == null) {
       aname = defaultXslStylesheetBase2 + href;
     }
     if (m_bundleContect.getBundle().getEntry(aname) == null) {
       aname = defaultXslStylesheetBase3 + href;
     }
     SAXSource ss =
         new SAXSource(new InputSource(m_bundleContect.getBundle().getEntry(aname).openStream()));
     XMLReader reader = m_saxParserFactory.newSAXParser().getXMLReader();
     ReaderConfig rc = ((WstxSAXParser) reader).getStaxConfig();
     rc.setXMLResolver(new XslURIResolver(m_bundleContect, m_saxParserFactory));
     ss.setXMLReader(reader);
     return ss;
   } catch (Exception e) {
     throw new RuntimeException("XslURIResolver.resolve", e);
   }
 }
示例#2
0
 public static NSData transform(Transformer transformer, NSData data) throws TransformerException {
   ByteArrayInputStream bis = new ByteArrayInputStream(data.bytes());
   SAXSource saxSource = new SAXSource();
   saxSource.setXMLReader(xmlReader);
   saxSource.setInputSource(new InputSource(bis));
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   Result r = new StreamResult(os);
   transformer.transform(saxSource, r);
   NSData result = new NSData(os.toByteArray());
   return result;
 }
示例#3
0
  /**
   * Creates a new {@link javax.xml.transform.Source} for the given content object.
   *
   * @param marshaller A marshaller instance that will be used to marshal <code>contentObject</code>
   *     into XML. This must be created from a JAXBContext that was used to build <code>
   *     contentObject</code> and must not be null.
   * @param contentObject An instance of a JAXB-generated class, which will be used as a {@link
   *     javax.xml.transform.Source} (by marshalling it into XML). It must not be null.
   * @throws JAXBException if an error is encountered while creating the JAXBSource or if either of
   *     the parameters are null.
   */
  public JAXBSource(Marshaller marshaller, Object contentObject) throws JAXBException {

    if (marshaller == null)
      throw new JAXBException(Messages.format(Messages.SOURCE_NULL_MARSHALLER));

    if (contentObject == null)
      throw new JAXBException(Messages.format(Messages.SOURCE_NULL_CONTENT));

    this.marshaller = marshaller;
    this.contentObject = contentObject;

    super.setXMLReader(pseudoParser);
    // pass a dummy InputSource. We don't care
    super.setInputSource(new InputSource());
  }
  public <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException {
    if (source instanceof SAXSource) {
      try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xmlReader = sp.getXMLReader();
        xmlReader.setFeature("http://xml.org/sax/features/validation", false);
        xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        ((SAXSource) source).setXMLReader(xmlReader);
        return delegate.unmarshal(source, declaredType);
      } catch (SAXException e) {
        throw new JAXBException(e);
      } catch (ParserConfigurationException e) {
        throw new JAXBException(e);
      }
    }

    throw new UnsupportedOperationException(errorMessage("Source, Class<T>"));
  }
  @Override
  protected Object unmarshalFromInputStream(
      Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt)
      throws JAXBException {
    try {

      Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
      if (t == null && supportJaxbOnly) {
        return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
      }

      if (unmarshaller.getClass().getName().contains("eclipse")) {
        // eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
        // so we need to bounce through a DOM
        Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
        DOMResult dom = new DOMResult();
        t.newTransformer().transform(reader, dom);
        return unmarshaller.unmarshal(dom.getNode());
      }
      XMLFilter filter = null;
      try {
        filter = factory.newXMLFilter(t);
      } catch (TransformerConfigurationException ex) {
        TemplatesImpl ti = (TemplatesImpl) t;
        filter = factory.newXMLFilter(ti.getTemplates());
        trySettingProperties(filter, ti);
      }
      XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
      filter.setParent(reader);
      SAXSource source = new SAXSource();
      source.setXMLReader(filter);
      if (systemId != null) {
        source.setSystemId(systemId);
      }
      return unmarshaller.unmarshal(source);
    } catch (TransformerException ex) {
      LOG.warning("Transformation exception : " + ex.getMessage());
      throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
  }