Repository navigation
[Add] ExternalObject class to provide the capability to access external objects refernced in AttributeValueXHTML instances #19
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
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
60 changes: 60 additions & 0 deletions
60
ReqIFSharp.Tests/AttributeValueTests/ExternalObjectTestFixture.cs
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,60 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // <copyright file="ExternalObjectTestFixture.cs" company="RHEA System S.A."> | ||
| // | ||
| // Copyright 2017 RHEA System S.A. | ||
| // | ||
| // 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. | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| using System.IO; | ||
|
|
||
| namespace ReqIFSharp.Tests.AttributeValueTests | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| /// <summary> | ||
| /// Suite of tests for the <see cref="ExternalObject"/> class. | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class ExternalObjectTestFixture | ||
| { | ||
| private ExternalObject externalObject; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| this.externalObject = new ExternalObject(null); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Verfify_that_expected_exceptions_are_raised_when_QueryLocalData_is_called() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => this.externalObject.QueryLocalData(null)); | ||
|
|
||
| var stream = new MemoryStream(); | ||
|
|
||
| this.externalObject.Uri = "http"; | ||
| Assert.Throws<InvalidOperationException>(() => this.externalObject.QueryLocalData(stream)); | ||
|
|
||
| this.externalObject.Uri = "somfile"; | ||
| this.externalObject.ReqIFFilePath = ""; | ||
| Assert.Throws<InvalidOperationException>(() => this.externalObject.QueryLocalData(stream)); | ||
| } | ||
| } | ||
| } |
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
Binary file not shown.
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,135 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // <copyright file="ExternalObject.cs" company="RHEA System S.A."> | ||
| // | ||
| // Copyright 2017 RHEA System S.A. | ||
| // | ||
| // 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. | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| namespace ReqIFSharp | ||
| { | ||
| using System; | ||
| using System.IO; | ||
| using System.IO.Compression; | ||
|
|
||
| /// <summary> | ||
| /// External objects are referenced binary objects that are referenced using the XHTML object element from the XHTML Object Module. | ||
| /// The location of an external object MUST be specified via the data attribute which contains either | ||
| /// a) a URL relative to the location of the exchange XML document, or | ||
| /// b) an absolute URL. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The specification for the XTHML object element defines several XML attributes. For ReqIF, only a subset of these attributes is relevant and used. | ||
| /// </remarks> | ||
| public class ExternalObject | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ExternalObject"/> class. | ||
| /// </summary> | ||
| /// <param name="attributeValueXhtml"> | ||
| /// The owning <see cref="AttributeValueXHTML"/> | ||
| /// </param> | ||
| public ExternalObject(AttributeValueXHTML attributeValueXhtml) | ||
| { | ||
| this.Owner = attributeValueXhtml; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the owning <see cref="AttributeValueXHTML"/> | ||
| /// </summary> | ||
| public AttributeValueXHTML Owner { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the Uri of the <see cref="ExternalObject"/>, this may be a relative uri or an absolute uri | ||
| /// </summary> | ||
| public string Uri { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the MimeType of the external object represented as a string | ||
| /// </summary> | ||
| public string MimeType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the height of the external object in case it is an image | ||
| /// </summary> | ||
| public int? Height { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the width of the external object in case it is an image | ||
| /// </summary> | ||
| public int? Width { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the path of the reqif file that contains the data from which this <see cref="AttributeValueXHTML"/> | ||
| /// object was created/ | ||
| /// </summary> | ||
| internal string ReqIFFilePath { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Asserts whether the external object should be queried from the reqifz file or from | ||
| /// another (absolute) location such as a resource available via HTTP or HTTPS | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public bool IsDataLocal() | ||
| { | ||
| return !this.Uri.StartsWith("http"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Queries the local data from the reqifz file and writes the data to the provided target <see cref="Stream"/> | ||
| /// </summary> | ||
| /// <param name="target"> | ||
| /// The target <see cref="Stream"/> to which the data is written | ||
| /// </param> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// Thrown when the <paramref name="target"/> is null | ||
| /// </exception> | ||
| /// <exception cref="InvalidOperationException"> | ||
| /// thrown when the <see cref="Uri"/> is an absolute uri and not a relative Uri or | ||
| /// when the reqifz file is unknown | ||
| /// </exception> | ||
| public void QueryLocalData(Stream target) | ||
| { | ||
| if (target == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(target), "The target stream may not be null"); | ||
| } | ||
|
|
||
| if (!this.IsDataLocal()) | ||
| { | ||
| throw new InvalidOperationException("The Uri of the External Object is not a relative Uri that can be found in the reqifz file"); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(this.ReqIFFilePath)) | ||
| { | ||
| throw new InvalidOperationException("The local data cannot be queried, the path to the reqifz file is unknown"); | ||
| } | ||
|
|
||
| using (var reader = new FileStream(this.ReqIFFilePath, FileMode.Open)) | ||
|
samatstariongroup marked this conversation as resolved.
Resolved
|
||
| { | ||
| using (var archive = new ZipArchive(reader, ZipArchiveMode.Read)) | ||
| { | ||
| var zipArchiveEntry = archive.GetEntry(this.Uri); | ||
| if (zipArchiveEntry != null) | ||
| { | ||
| var sourceStream = zipArchiveEntry.Open(); | ||
| sourceStream.CopyTo(target); | ||
| sourceStream.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.