-
Notifications
You must be signed in to change notification settings - Fork 171
Initial Implementation of XML Format #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c5b5cce
Initial Commit
JemDay 7b306cf
tweaks
JemDay 71c8e5b
More negative tests
JemDay b78186d
More Tests
JemDay ba689ae
Adopted (unratified) chnages to types and namespaces
JemDay 5e833d8
More cleanups
JemDay 4acbd5d
reformatting and comment cleanups
JemDay 91b2b38
Minor tweak to trigger build
JemDay 87b4992
Add checks for duplicated CE atrtributes in XML document
JemDay 9c857c0
Address review comments
JemDay dec4019
JavaDoc now builds, fixed typos
JemDay 492de41
Minor docs tweak
JemDay de0442f
Address Review Comments
JemDay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| title: CloudEvents XML Format | ||
| nav_order: 4 | ||
| --- | ||
|
|
||
| # CloudEvents XML Format | ||
|
|
||
| [](http://www.javadoc.io/doc/io.cloudevents/cloudevents-xml) | ||
|
|
||
| This module provides and `EventFormat` implementation that adheres | ||
| to the CloudEvent XML Format specification. | ||
|
|
||
| This format also supports specialized handling for XML CloudEvent `data`. | ||
|
|
||
| For Maven based projects, use the following dependency: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>io.cloudevents</groupId> | ||
| <artifactId>cloudevents-xml</artifactId> | ||
| <version>2.4.0</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| ## Using the XML Event Format | ||
|
|
||
| You don't need to perform any operation to configure the module, more than | ||
| adding the dependency to your project: | ||
|
|
||
| ```java | ||
| import io.cloudevents.CloudEvent; | ||
| import io.cloudevents.core.format.EventFormatProvider; | ||
| import io.cloudevents.core.builder.CloudEventBuilder; | ||
| import io.cloudevents.xml.XMLFormat; | ||
|
|
||
| CloudEvent event = CloudEventBuilder.v1() | ||
| .withId("hello") | ||
| .withType("example.xml") | ||
| .withSource(URI.create("http://localhost")) | ||
| .build(); | ||
|
|
||
| byte[] serialized = EventFormatProvider | ||
| .getInstance() | ||
| .resolveFormat(XMLFormat.CONTENT_TYPE) | ||
| .serialize(event); | ||
| ``` | ||
|
|
||
| The `EventFormatProvider` will resolve automatically the `XMLFormat` using the | ||
| `ServiceLoader` APIs. | ||
|
|
||
| XML Document data handling is supported via the `XMLCloudEventData` | ||
| facility. This convenience wrapper can be used with `any` other supported | ||
| format. | ||
|
|
||
| ```java | ||
| import org.w3c.dom.Document; | ||
JemDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import io.cloudevents.CloudEvent; | ||
| import io.cloudevents.core.builder.CloudEventBuilder; | ||
| import io.cloudevents.xml.XMLCloudEventData; | ||
|
|
||
| // Create the business event data. | ||
| Document xmlDoc = .... ; | ||
|
|
||
| // Wrap it into CloudEventData | ||
| CloudEventData myData = XMLCloudEventData.wrap(xmlDoc); | ||
|
|
||
| // Construct the event | ||
| CloudEvent event = CloudEventBuilder.v1() | ||
| .withId("hello") | ||
| .withType("example.xml") | ||
| .withSource(URI.create("http://localhost")) | ||
| .withData(myData) | ||
| .build(); | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Copyright 2021-Present The CloudEvents Authors | ||
| ~ <p> | ||
| ~ 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 | ||
| ~ <p> | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ <p> | ||
| ~ 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. | ||
| ~ | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>io.cloudevents</groupId> | ||
| <artifactId>cloudevents-parent</artifactId> | ||
| <version>2.5.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>cloudevents-xml</artifactId> | ||
| <name>CloudEvents - XML Format</name> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <properties> | ||
| <module-name>io.cloudevents.formats.xml</module-name> | ||
| <xmlunit.version>2.9.0</xmlunit.version> | ||
| <javax.xml.version>2.3.1</javax.xml.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <dependency> | ||
| <groupId>io.cloudevents</groupId> | ||
| <artifactId>cloudevents-core</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>javax.xml.bind</groupId> | ||
| <artifactId>jaxb-api</artifactId> | ||
| <version>${javax.xml.version}</version> | ||
| </dependency> | ||
|
|
||
| <!-- Test deps --> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>${junit-jupiter.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
| <version>${assertj-core.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>io.cloudevents</groupId> | ||
| <artifactId>cloudevents-core</artifactId> | ||
| <classifier>tests</classifier> | ||
| <type>test-jar</type> | ||
| <version>${project.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.xmlunit</groupId> | ||
| <artifactId>xmlunit-core</artifactId> | ||
| <version>${xmlunit.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| </project> |
50 changes: 50 additions & 0 deletions
50
formats/xml/src/main/java/io/cloudevents/xml/OccurrenceTracker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright 2018-Present The CloudEvents Authors | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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 io.cloudevents.xml; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Tracks the occurrences of a key to ensure only a single | ||
| * instance is allowed. | ||
| * | ||
| * Used to help ensure that each CloudEvent context attribute | ||
| * only occurs once in each CloudEvent element instance. | ||
| * | ||
| */ | ||
| class OccurrenceTracker { | ||
|
|
||
| private final Set<String> keySet; | ||
|
|
||
| OccurrenceTracker() { | ||
| keySet = new HashSet<>(10); | ||
| } | ||
|
|
||
| /** | ||
| * Record an occurrence of attribute name. | ||
| * @param name The name to track. | ||
| * @return boolean true => accepted, false => duplicate name. | ||
| */ | ||
| boolean trackOccurrence(String name) { | ||
|
|
||
| return keySet.add(name); | ||
|
|
||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
formats/xml/src/main/java/io/cloudevents/xml/XMLCloudEventData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2018-Present The CloudEvents Authors | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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 io.cloudevents.xml; | ||
|
|
||
| import io.cloudevents.CloudEventData; | ||
| import org.w3c.dom.Document; | ||
|
|
||
| /** | ||
| * A variant of {@link CloudEventData} that supports direct access | ||
| * to data as an XML {@link Document} | ||
| */ | ||
| public interface XMLCloudEventData extends CloudEventData { | ||
|
|
||
| /** | ||
| * Get an XML Document representation of the | ||
| * CloudEvent data. | ||
| * | ||
| * @return The {@link Document} representation. | ||
| */ | ||
| Document getDocument(); | ||
|
|
||
| /** | ||
| * Wraps an XML {@link Document} | ||
| * | ||
| * @param xmlDoc {@link Document} | ||
| * @return The wrapping {@link XMLCloudEventData} | ||
| */ | ||
| static CloudEventData wrap(Document xmlDoc) { | ||
| return new XMLDataWrapper(xmlDoc); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.