コード例 #1
1
ファイル: Marshaller.java プロジェクト: INightmare/jxmlizer
  /**
   * Serialize object to XML and output it to given OutputStream
   *
   * @param object objec to marshal
   * @param outputStream stream to put the resulting XML to
   */
  public void marshal(Object object, OutputStream outputStream) {
    try {
      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
      ByteArrayOutputStream tempOutput = null;
      XMLStreamWriter writer = null;

      if (autoIndent) {
        tempOutput = new ByteArrayOutputStream();
        writer = outputFactory.createXMLStreamWriter(tempOutput);
      } else {
        writer = outputFactory.createXMLStreamWriter(outputStream);
      }

      //            if (autoIndent) {
      //                writer = new IndentingXMLStreamWriterDecorator(writer);
      //            }

      writerContext.writer = writer;

      writer.writeStartDocument();

      marshalObject(writer, object);

      writer.writeEndDocument();
      writer.close();

      if (autoIndent) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty("doctype-public", "yes");
        transformer.transform(
            new StreamSource(new ByteArrayInputStream(tempOutput.toByteArray())),
            new StreamResult(outputStream));
      }

    } catch (TransformerException ex) {
      throw new XmlizerException("Error whule indenting XML", ex);
    } catch (XMLStreamException ex) {
      throw new XmlizerException("Error occured while writting", ex);
    } catch (IllegalAccessException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    } catch (IllegalArgumentException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    } catch (InvocationTargetException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    }
  }
コード例 #2
0
 protected byte[] object2WbXml(
     Object o,
     WbXmlDefinition def,
     WbXmlVersion version,
     WbXmlEncoder.StrtblType encoderType,
     boolean skipSpaces,
     boolean event)
     throws Exception {
   XMLStreamWriter xmlStreamWriter = null;
   XMLEventWriter xmlEventWriter = null;
   ByteArrayOutputStream out = null;
   try {
     out = new ByteArrayOutputStream();
     outFact.setProperty(WbXmlOutputFactory.ENCODING_TYPE_PROPERTY, encoderType);
     outFact.setProperty(WbXmlOutputFactory.SKIP_SPACES_PROPERTY, skipSpaces);
     outFact.setProperty(WbXmlOutputFactory.VERSION_PROPERTY, version);
     outFact.setProperty(WbXmlOutputFactory.DEFINITION_PROPERTY, def);
     if (event) {
       xmlEventWriter = outFact.createXMLEventWriter(out);
     } else {
       xmlStreamWriter = outFact.createXMLStreamWriter(out);
     }
     JAXBContext jc = JAXBContext.newInstance(o.getClass());
     Marshaller marshaller = jc.createMarshaller();
     if (event) {
       marshaller.marshal(o, xmlEventWriter);
     } else {
       marshaller.marshal(o, xmlStreamWriter);
     }
     return out.toByteArray();
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (Exception e) {
       }
     }
     if (xmlStreamWriter != null) {
       try {
         xmlStreamWriter.close();
       } catch (Exception e) {
       }
     }
     if (xmlEventWriter != null) {
       try {
         xmlEventWriter.close();
       } catch (Exception e) {
       }
     }
   }
 }
コード例 #3
0
 protected byte[] doc2WbXml(
     Document doc,
     WbXmlDefinition def,
     WbXmlVersion version,
     WbXmlEncoder.StrtblType encoderType,
     boolean skipSpaces,
     boolean event)
     throws Exception {
   XMLStreamWriter xmlStreamWriter = null;
   XMLEventWriter xmlEventWriter = null;
   ByteArrayOutputStream out = null;
   try {
     out = new ByteArrayOutputStream();
     outFact.setProperty(WbXmlOutputFactory.ENCODING_TYPE_PROPERTY, encoderType);
     outFact.setProperty(WbXmlOutputFactory.SKIP_SPACES_PROPERTY, skipSpaces);
     outFact.setProperty(WbXmlOutputFactory.VERSION_PROPERTY, version);
     outFact.setProperty(WbXmlOutputFactory.DEFINITION_PROPERTY, def);
     if (event) {
       xmlEventWriter = outFact.createXMLEventWriter(out);
     } else {
       xmlStreamWriter = outFact.createXMLStreamWriter(out);
     }
     Transformer xformer = TransformerFactory.newInstance().newTransformer();
     Source domSource = new DOMSource(doc);
     StAXResult staxResult =
         (event) ? new StAXResult(xmlEventWriter) : new StAXResult(xmlStreamWriter);
     xformer.transform(domSource, staxResult);
     return out.toByteArray();
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (Exception e) {
       }
     }
     if (xmlStreamWriter != null) {
       try {
         xmlStreamWriter.close();
       } catch (Exception e) {
       }
     }
     if (xmlEventWriter != null) {
       try {
         xmlEventWriter.close();
       } catch (Exception e) {
       }
     }
   }
 }
コード例 #4
0
  public static void doXmlOutput(boolean useRepairing) throws XMLStreamException {
    StringWriter buffer = new StringWriter();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    if (useRepairing) {
      outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
    }
    XMLStreamWriter out = outputFactory.createXMLStreamWriter(buffer);
    out.writeStartDocument();
    out.writeStartElement("env", "Envelope", SOAP12);
    out.writeNamespace("env", SOAP12);
    out.writeNamespace("test", "http://someTestUri");
    out.writeStartElement("env", "Body", SOAP12);

    out.writeStartElement("test");
    out.writeAttribute("foo", "bar");
    out.writeEndElement();

    out.writeStartElement("test");
    out.writeAttribute("foo", "bar");
    out.writeCharacters("");
    out.writeEndElement();

    out.writeStartElement("test");
    out.writeAttribute("foo", "bar");
    out.writeCharacters(" ");
    out.writeEndElement();

    out.writeEndElement();
    out.writeEndElement();

    out.writeEndDocument();
    out.close();
    System.out.println("Created " + (useRepairing ? "" : "not") + " using repairing :-");
    System.out.println(buffer);
  }
コード例 #5
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testPoint2()
      throws XMLStreamException, FactoryConfigurationError, IOException, TransformationException,
          UnknownCRSException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + POINT2_FILE));
    xmlReader.nextTag();
    Point point = new GML2GeometryReader().parsePoint(xmlReader, null);
    Assert.assertEquals(5.0, point.get0(), DELTA);
    Assert.assertEquals(30.0, point.get1(), DELTA);

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");

    exporter.export(point);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #6
0
 public GenericDirectoryTester(String directory, WbXmlDefinition def) {
   this.directory = directory;
   this.def = def;
   this.inFact = new WbXmlInputFactory();
   inFact.setProperty(WbXmlInputFactory.DEFINITION_PROPERTY, def);
   this.outFact = new WbXmlOutputFactory();
   outFact.setProperty(WbXmlOutputFactory.DEFINITION_PROPERTY, def);
 }
コード例 #7
0
ファイル: DirectSkeleton.java プロジェクト: GEFFROY/Quercus
  private static XMLOutputFactory getXMLOutputFactory() throws XMLStreamException {
    if (_outputFactory == null) {
      _outputFactory = XMLOutputFactory.newInstance();
      _outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    }

    return _outputFactory;
  }
コード例 #8
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testPolygon()
      throws XMLStreamException, FactoryConfigurationError, IOException, UnknownCRSException,
          TransformationException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + POLYGON_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Polygon"), xmlReader.getName());

    Polygon polygon = new GML2GeometryReader().parsePolygon(xmlReader, null);
    Assert.assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Polygon"), xmlReader.getName());

    Points points = polygon.getExteriorRing().getControlPoints();
    comparePoint(0.0, 0.0, points.get(0));
    comparePoint(100.0, 0.0, points.get(1));
    comparePoint(100.0, 100.0, points.get(2));
    comparePoint(0.0, 100.0, points.get(3));
    comparePoint(0.0, 0.0, points.get(4));

    List<Points> innerPoints = polygon.getInteriorRingsCoordinates();
    Points points1 = innerPoints.get(0);
    comparePoint(10.0, 10.0, points1.get(0));
    comparePoint(10.0, 40.0, points1.get(1));
    comparePoint(40.0, 40.0, points1.get(2));
    comparePoint(40.0, 10.0, points1.get(3));
    comparePoint(10.0, 10.0, points1.get(4));

    Points points2 = innerPoints.get(1);
    comparePoint(60.0, 60.0, points2.get(0));
    comparePoint(60.0, 90.0, points2.get(1));
    comparePoint(90.0, 90.0, points2.get(2));
    comparePoint(90.0, 60.0, points2.get(3));
    comparePoint(60.0, 60.0, points2.get(4));

    Assert.assertEquals(
        CRSRegistry.lookup("http://www.opengis.net/gml/srs/epsg.xml#4326"),
        polygon.getCoordinateSystem().getWrappedCRS());

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");
    writer.setPrefix("xlink", "http://www.w3.org/1999/xlink");

    exporter.export(polygon);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #9
0
 public static XMLStreamWriter create(OutputStream os) throws XmlSerializerException {
   try {
     XMLOutputFactory xof = XMLOutputFactory.newInstance();
     xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
     return xof.createXMLStreamWriter(os);
   } catch (XMLStreamException e) {
     throw new XmlSerializerException("Problem Creating a pull parser", e);
   }
 }
コード例 #10
0
  /**
   * Copies the current node and its children to the <code>sink</code>.
   *
   * @param sink the output sink
   */
  public void copyNode(OutputStream sink) throws XMLStreamException {
    Assertion.notNull(sink);

    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", isNsRepairing);
    final XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sink);

    final DcsStreamSupport dcsStream = new DcsStreamSupport();
    dcsStream.copyNode(in, writer);
    writer.flush();
    writer.close();
  }
コード例 #11
0
  private void serializeValue(final HttpServletResponse pResponse, Object value)
      throws TransformerException, IOException, FactoryConfigurationError {
    if (value instanceof Source) {
      setContentType(pResponse, "application/binary"); // Unknown content type
      Sources.writeToStream((Source) value, pResponse.getOutputStream());
    } else if (value instanceof Node) {
      pResponse.setContentType("text/xml");
      Sources.writeToStream(new DOMSource((Node) value), pResponse.getOutputStream());
    } else if (value instanceof XmlSerializable) {
      pResponse.setContentType("text/xml");
      XMLOutputFactory factory = XMLOutputFactory.newInstance();
      factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
      try {
        XmlWriter out =
            XmlStreaming.newWriter(
                pResponse.getOutputStream(), pResponse.getCharacterEncoding(), true);
        try {
          out.startDocument(null, null, null);
          ((XmlSerializable) value).serialize(out);
          out.endDocument();
        } finally {
          out.close();
        }
      } catch (XmlException e) {
        throw new TransformerException(e);
      }
    } else if (value instanceof Collection) {
      final XmlElementWrapper annotation = getElementWrapper();
      if (annotation != null) {
        setContentType(pResponse, "text/xml");
        try (OutputStream outStream = pResponse.getOutputStream()) {

          writeCollection(
              outStream, getGenericReturnType(), (Collection<?>) value, getQName(annotation));
        }
      }
    } else if (value instanceof CharSequence) {
      setContentType(pResponse, "text/plain");
      pResponse.getWriter().append((CharSequence) value);
    } else {
      if (value != null) {
        try {
          final JAXBContext jaxbContext = JAXBContext.newInstance(getReturnType());
          setContentType(pResponse, "text/xml");

          final JAXBSource jaxbSource = new JAXBSource(jaxbContext, value);
          Sources.writeToStream(jaxbSource, pResponse.getOutputStream());

        } catch (final JAXBException e) {
          throw new MessagingException(e);
        }
      }
    }
  }
コード例 #12
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testMultiGeometry()
      throws XMLStreamException, FactoryConfigurationError, IOException, TransformationException,
          UnknownCRSException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + MULTIGEOMETRY_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "MultiGeometry"), xmlReader.getName());

    MultiGeometry<?> multiGeometry = new GML2GeometryReader().parseMultiGeometry(xmlReader, null);
    assertEquals("c731", multiGeometry.getId());

    Point firstMember = (Point) multiGeometry.get(0);
    assertEquals("P6776", firstMember.getId());
    comparePoint(50.0, 50.0, firstMember);

    LineString secondMember = (LineString) multiGeometry.get(1);
    assertEquals("L21216", secondMember.getId());

    Points controlPoints = secondMember.getControlPoints();
    comparePoint(0.0, 0.0, controlPoints.get(0));
    comparePoint(0.0, 50.0, controlPoints.get(1));
    comparePoint(100.0, 50.0, controlPoints.get(2));

    Polygon thirdMember = (Polygon) multiGeometry.get(2);
    assertEquals("_877789", thirdMember.getId());

    Points points = thirdMember.getExteriorRing().getControlPoints();
    comparePoint(0.0, 0.0, points.get(0));
    comparePoint(100.0, 0.0, points.get(1));
    comparePoint(50.0, 100.0, points.get(2));
    comparePoint(0.0, 0.0, points.get(3));

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");
    writer.setPrefix("xlink", "http://www.w3.org/1999/xlink");

    exporter.export(multiGeometry);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #13
0
 protected static boolean setNamespaceAware(XMLOutputFactory f, boolean state)
     throws XMLStreamException {
   /* Let's not assert, but see if it sticks. Some implementations
    * might choose to silently ignore setting, at least for 'false'?
    */
   try {
     f.setProperty(XMLStreamProperties.XSP_NAMESPACE_AWARE, state ? Boolean.TRUE : Boolean.FALSE);
     return (isNamespaceAware(f) == state);
   } catch (IllegalArgumentException e) {
     /* Let's assume, then, that the property (or specific value for it)
      * is NOT supported...
      */
     return false;
   }
 }
コード例 #14
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testMultiLineString()
      throws XMLStreamException, FactoryConfigurationError, IOException, TransformationException,
          UnknownCRSException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + MULTILINESTRING_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "MultiLineString"), xmlReader.getName());

    MultiLineString multiLineString =
        new GML2GeometryReader().parseMultiLineString(xmlReader, null);
    LineString firstMember = multiLineString.get(0);

    Points controlPoints = firstMember.getControlPoints();
    comparePoint(56.1, 0.45, controlPoints.get(0));
    comparePoint(67.23, 0.98, controlPoints.get(1));

    LineString secondMember = multiLineString.get(1);

    controlPoints = secondMember.getControlPoints();
    comparePoint(46.71, 9.25, controlPoints.get(0));
    comparePoint(56.88, 10.44, controlPoints.get(1));

    LineString thirdMember = multiLineString.get(2);

    controlPoints = thirdMember.getControlPoints();
    comparePoint(324.1, 219.7, controlPoints.get(0));
    comparePoint(0.45, 4.56, controlPoints.get(1));

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");
    writer.setPrefix("xlink", "http://www.w3.org/1999/xlink");

    exporter.export(multiLineString);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #15
0
  public void testObjectToXMLStreamWriterRecord() throws Exception {
    if (XML_OUTPUT_FACTORY != null) {
      StringWriter writer = new StringWriter();

      XMLOutputFactory factory = XMLOutputFactory.newInstance();
      factory.setProperty(factory.IS_REPAIRING_NAMESPACES, new Boolean(false));
      XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);

      Object objectToWrite = getWriteControlObject();
      XMLDescriptor desc = null;
      if (objectToWrite instanceof XMLRoot) {
        desc =
            (XMLDescriptor)
                xmlContext
                    .getSession(0)
                    .getProject()
                    .getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
      } else {
        desc =
            (XMLDescriptor)
                xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass());
      }
      jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");

      int sizeBefore = getNamespaceResolverSize(desc);
      XMLStreamWriterRecord record = new XMLStreamWriterRecord(streamWriter);
      try {
        ((org.eclipse.persistence.jaxb.JAXBMarshaller) jaxbMarshaller)
            .marshal(objectToWrite, record);
      } catch (Exception e) {
        assertMarshalException(e);
        return;
      }
      if (expectsMarshalException) {
        fail("An exception should have occurred but didn't.");
        return;
      }

      streamWriter.flush();
      int sizeAfter = getNamespaceResolverSize(desc);

      assertEquals(sizeBefore, sizeAfter);

      Document testDocument = getTestDocument(writer.toString());
      writer.close();
      objectToXMLDocumentTest(testDocument);
    }
  }
コード例 #16
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testBox()
      throws XMLStreamException, FactoryConfigurationError, IOException, UnknownCRSException,
          TransformationException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + BOX_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Box"), xmlReader.getName());

    Envelope envelope = new GML2GeometryReader().parseEnvelope(xmlReader, null);
    Assert.assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Box"), xmlReader.getName());
    Assert.assertEquals(0.0, envelope.getMin().get0(), DELTA);
    Assert.assertEquals(0.0, envelope.getMin().get1(), DELTA);
    Assert.assertEquals(100.0, envelope.getMax().get0(), DELTA);
    Assert.assertEquals(100.0, envelope.getMax().get1(), DELTA);
    Assert.assertEquals(
        CRSRegistry.lookup("http://www.opengis.net/gml/srs/epsg.xml#4326"),
        envelope.getCoordinateSystem().getWrappedCRS());

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    // writer.setPrefix( "app", "http://www.deegree.org" );
    // writer.setPrefix( "app", "http://www.deegree.org/app" );
    writer.setPrefix("gml", "http://www.opengis.net/gml");
    // writer.setPrefix( "ogc", "http://www.opengis.net/ogc" );
    // writer.setPrefix( "wfs", "http://www.opengis.net/wfs" );
    // writer.setPrefix( "xlink", "http://www.w3.org/1999/xlink" );
    // writer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );

    exporter.export(envelope);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #17
0
ファイル: MembershipXMLEntity.java プロジェクト: binhnvexo/cs
 public void write(OutputStream out) throws IOException {
   try {
     XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
     outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
     XMLStreamWriter xsw = outputFactory.createXMLStreamWriter(out);
     xsw.writeStartDocument();
     xsw.writeStartElement("membership");
     xsw.writeDefaultNamespace(XMLContants.EXO_NAMESPACE_URL);
     xsw.writeNamespace(XMLContants.XLINK_PREFIX, XMLContants.XLINK_NAMESPACE_URL);
     xsw.writeAttribute(
         XMLContants.XLINK_NAMESPACE_URL,
         XMLContants.XLINK_HREF,
         baseURI_
             + "/organization/membership/"
             + membership_.getId()
             + "/?output=xml&command=view");
     xsw.writeStartElement("id");
     xsw.writeCharacters(membership_.getId());
     xsw.writeEndElement();
     xsw.writeStartElement("group-id");
     xsw.writeCharacters(membership_.getGroupId());
     xsw.writeEndElement();
     xsw.writeStartElement("user-name");
     xsw.writeCharacters(membership_.getUserName());
     xsw.writeEndElement();
     xsw.writeStartElement("membership-type");
     xsw.writeCharacters(membership_.getMembershipType());
     xsw.writeEndElement();
     xsw.writeEndDocument();
     xsw.flush();
     xsw.close();
   } catch (Exception e) {
     e.printStackTrace();
     throw new IOException(e.getMessage());
   }
 }
コード例 #18
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testPoint()
      throws XMLStreamException, FactoryConfigurationError, IOException, UnknownCRSException,
          TransformationException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + POINT_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Point"), xmlReader.getName());

    Point point = new GML2GeometryReader().parsePoint(xmlReader, null);
    Assert.assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "Point"), xmlReader.getName());
    Assert.assertEquals(5.0, point.get0(), DELTA);
    Assert.assertEquals(40.0, point.get1(), DELTA);
    Assert.assertEquals(
        CRSRegistry.lookup("http://www.opengis.net/gml/srs/epsg.xml#4326"),
        point.getCoordinateSystem().getWrappedCRS());

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");

    exporter.export(point);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #19
0
  /**
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   * @throws IOException
   * @throws UnknownCRSException
   * @throws TransformationException
   */
  @Test
  public void testMultiPoint()
      throws XMLStreamException, FactoryConfigurationError, IOException, TransformationException,
          UnknownCRSException {
    XMLStreamReaderWrapper xmlReader =
        new XMLStreamReaderWrapper(this.getClass().getResource(BASE_DIR + MULTIPOINT_FILE));
    xmlReader.nextTag();

    Assert.assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.getEventType());
    Assert.assertEquals(new QName(GML21NS, "MultiPoint"), xmlReader.getName());

    MultiPoint multiPoint = new GML2GeometryReader().parseMultiPoint(xmlReader, null);

    Point firstMember = multiPoint.get(0);
    comparePoint(5.0, 40.0, firstMember);

    Point secondMember = multiPoint.get(1);
    comparePoint(0.0, 0.0, secondMember);

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true));
    XMLMemoryStreamWriter memoryWriter = new XMLMemoryStreamWriter();

    SchemaLocationXMLStreamWriter writer =
        new SchemaLocationXMLStreamWriter(
            memoryWriter.getXMLStreamWriter(), SCHEMA_LOCATION_ATTRIBUTE);
    GML2GeometryWriter exporter = new GML2GeometryWriter(writer, null, null, new HashSet<String>());

    writer.setPrefix("gml", "http://www.opengis.net/gml");
    writer.setPrefix("xlink", "http://www.w3.org/1999/xlink");

    exporter.export(multiPoint);
    writer.flush();

    XMLAssert.assertValidity(memoryWriter.getReader(), SCHEMA_LOCATION);
  }
コード例 #20
0
 public void setDefinition(WbXmlDefinition def) {
   this.def = def;
   inFact.setProperty(WbXmlInputFactory.DEFINITION_PROPERTY, def);
   outFact.setProperty(WbXmlOutputFactory.DEFINITION_PROPERTY, def);
 }
コード例 #21
0
  public boolean execute() {
    attributeModel = workspace.getLookup().lookup(AttributeModel.class);
    graphModel = workspace.getLookup().lookup(GraphModel.class);
    HierarchicalGraph graph = null;
    if (exportVisible) {
      graph = graphModel.getHierarchicalGraphVisible();
    } else {
      graph = graphModel.getHierarchicalGraph();
    }
    Progress.start(progress);
    graph.readLock();

    // Options
    if (normalize) {
      calculateMinMax(graph);
    }

    // Calculate progress units count
    int max = 0;
    if (exportHierarchy) {
      for (Node n : graph.getNodesTree()) {
        max++;
      }
      for (Edge e : graph.getEdgesTree()) {
        max++;
      }
    } else {
      max = graph.getNodeCount();
      for (Edge e : graph.getEdgesAndMetaEdges()) {
        max++;
      }
    }
    Progress.switchToDeterminate(progress, max);

    try {
      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.FALSE);

      XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(writer);
      xmlWriter = new IndentingXMLStreamWriter(xmlWriter);

      xmlWriter.writeStartDocument("UTF-8", "1.0");
      xmlWriter.setPrefix("", GEXF_NAMESPACE);
      xmlWriter.writeStartElement(GEXF_NAMESPACE, GEXF);
      xmlWriter.writeNamespace("", GEXF_NAMESPACE);
      xmlWriter.writeAttribute(GEXF_VERSION, "1.1");

      if (exportColors || exportPosition || exportSize) {
        xmlWriter.writeNamespace(VIZ, VIZ_NAMESPACE);
      }
      xmlWriter.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      xmlWriter.writeAttribute("xsi:schemaLocation", GEXF_NAMESPACE_LOCATION);

      if (exportDynamic) {
        DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
        dynamicModel = dynamicController != null ? dynamicController.getModel(workspace) : null;
        visibleInterval =
            dynamicModel == null
                ? null
                : exportVisible
                    ? dynamicModel.getVisibleInterval()
                    : new TimeInterval(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
      }

      writeMeta(xmlWriter);
      writeGraph(xmlWriter, graph);

      xmlWriter.writeEndElement();
      xmlWriter.writeEndDocument();
      xmlWriter.close();

    } catch (Exception e) {
      graph.readUnlockAll();
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }
      throw new RuntimeException(e);
    }

    graph.readUnlock();

    Progress.finish(progress);
    return !cancel;
  }
コード例 #22
0
 private XmlStreamWriterBuilder() {
   System.setProperty(
       "javax.xml.stream.XMLOutputFactory", "com.sun.xml.internal.stream.XMLOutputFactoryImpl");
   factory = XMLOutputFactory.newFactory();
   factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
 }
コード例 #23
0
  public void createExternalBindingsFile(
      String sourceDir, String targetFile, String nameSpace, String referencedXSD)
      throws XMLStreamException, IOException {
    if (sourceDir == null) {
      System.out.println("No source directory given.");
      return;
    }
    if (targetFile == null) {
      System.out.println("No target file given.");
      return;
    }

    File source = new File(sourceDir);
    if (!source.exists()) {
      System.out.println(
          "Given source directory does not exist: "
              + source.getAbsolutePath()
              + "Please supply an existing source directory!");
      return;
    }
    File target = new File(targetFile);

    String name = source.getAbsolutePath();
    int index = name.indexOf("src");
    if (index == -1) {
      System.out.println("Found no src directory in your path, this is strange!");
      return;
    }
    String prefix = name.substring(0, index + 4);
    // src + 1
    String packageName = pathToPackage(name.substring(index + 4));

    List<StringPair> classes = findClasses(source, prefix);

    System.out.println("Found following classes: ");
    for (StringPair s : classes) {
      System.out.println(s);
    }

    // Get the reference file if any
    String targetSchemaFile = referencedXSD;
    if (referencedXSD == null) {
      targetSchemaFile = "../" + target.getName();
    }

    String ns = nameSpace;
    if (ns == null || "".equals(ns.trim())) {
      index = packageName.lastIndexOf('.');
      String nsName = null;
      if (index == -1) {
        nsName = packageName;
      } else {
        nsName = packageName.substring(index + 1);
      }
      ns = "http://www.deegree.org/" + nsName;
    }
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
    FileOutputStream fos = new FileOutputStream(targetFile);
    XMLStreamWriter writer =
        new IndentingXMLStreamWriter(
            factory.createXMLStreamWriter(new OutputStreamWriter(fos, "UTF-8")));
    writeBindings(classes, writer, targetSchemaFile, ns, packageName);
    writer.flush();
    System.out.println(
        "Successfully wrote external-bindings file: " + target.getAbsoluteFile().toString());
  }
コード例 #24
0
 protected static void setRepairing(XMLOutputFactory f, boolean state) {
   f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.valueOf(state));
 }