Skip to content

Commit 0dd62cd

Browse files
Merge pull request #49 from Simon-Initiative/SPRINT-0.36.0
Sprint 0.36.0
2 parents ca4b24d + 2fdcba1 commit 0dd62cd

3 files changed

Lines changed: 71 additions & 51 deletions

File tree

src/main/java/edu/cmu/oli/content/resource/builders/ContentPkgJsonReader.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package edu.cmu.oli.content.resource.builders;
22

3+
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
7+
import javax.ws.rs.core.Response;
8+
39
import com.google.gson.JsonArray;
410
import com.google.gson.JsonElement;
511
import com.google.gson.JsonObject;
12+
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
616
import edu.cmu.oli.content.AppUtils;
717
import edu.cmu.oli.content.ResourceException;
818
import edu.cmu.oli.content.models.persistance.JsonWrapper;
919
import edu.cmu.oli.content.models.persistance.entities.ContentPackage;
1020
import edu.cmu.oli.content.models.persistance.entities.ErrorLevel;
1121
import edu.cmu.oli.content.models.persistance.entities.FileNode;
1222
import edu.cmu.oli.content.models.persistance.entities.WebContent;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
16-
import javax.ws.rs.core.Response;
17-
import java.io.File;
18-
import java.nio.file.Files;
19-
import java.nio.file.Paths;
2023

2124
/**
2225
* Methods for parsing a content package manifest from JSON.
@@ -75,14 +78,20 @@ public static void parsePackageJson(ContentPackage mnfst, JsonObject pkgJson, bo
7578
JsonElement preferences = pkgJson.get("preferences");
7679
mnfst.setOptions(new JsonWrapper(preferences));
7780

81+
String language = pkgJson.has("language") ? pkgJson.get("language").getAsString() : null;
82+
if (language != null) {
83+
mnfst.setLanguage(language);
84+
}
85+
7886
mnfst.setErrors(new JsonWrapper(errors));
7987

8088
if (pkgJson.has("icon")) {
8189
String iconHref = null;
8290
JsonElement iconJson = pkgJson.get("icon");
8391
if (iconJson.isJsonObject()) {
8492
if (pkgJson.get("icon").getAsJsonObject().has("fileNode")) {
85-
iconHref = pkgJson.get("icon").getAsJsonObject().getAsJsonObject("fileNode").get("pathTo").getAsString();
93+
iconHref = pkgJson.get("icon").getAsJsonObject().getAsJsonObject("fileNode").get("pathTo")
94+
.getAsString();
8695
}
8796
} else if (iconJson.isJsonPrimitive()) {
8897
iconHref = pkgJson.get("icon").getAsString();

src/main/java/edu/cmu/oli/content/resource/builders/ContentPkgXmlReader.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.google.gson.JsonElement;
44
import com.google.gson.JsonObject;
5+
56
import org.jdom2.Document;
67
import org.jdom2.Element;
78
import org.jdom2.Namespace;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011

11-
1212
/**
1313
* Methods for parsing a content package manifest from XML. The parser assumes
1414
* compliance with the OLI content package DTD. Both the simplified and complete
@@ -19,11 +19,11 @@
1919
public final class ContentPkgXmlReader {
2020
// Metadata and preference namespaces
2121

22-
private static final Namespace _METADATA_NS
23-
= Namespace.getNamespace("cmd", "http://oli.web.cmu.edu/content/metadata/");
22+
private static final Namespace _METADATA_NS = Namespace.getNamespace("cmd",
23+
"http://oli.web.cmu.edu/content/metadata/");
2424

25-
private static final Namespace _PREFERENCES_NS
26-
= Namespace.getNamespace("pref", "http://oli.web.cmu.edu/preferences/");
25+
private static final Namespace _PREFERENCES_NS = Namespace.getNamespace("pref",
26+
"http://oli.web.cmu.edu/preferences/");
2727

2828
private static final Logger log = LoggerFactory.getLogger(ContentPkgXmlReader.class);
2929

@@ -38,17 +38,16 @@ private ContentPkgXmlReader() {
3838
// =======================================================================
3939

4040
/**
41-
* Parses the supplied document into a simplified content package manifest.
42-
* This method assumes that the supplied XML has been validated against the
43-
* OLI simple content package DTD.
41+
* Parses the supplied document into a simplified content package manifest. This
42+
* method assumes that the supplied XML has been validated against the OLI
43+
* simple content package DTD.
4444
*
4545
* @param mnfstDoc manifest document
4646
* @return content package manifest
4747
* @throws NullPointerException if <tt>mnfstDoc</tt> is <tt>null</tt>
4848
* @throws BuildException if the content package manifest is not valid
4949
*/
50-
public static JsonObject documentToSimpleManifest(Document mnfstDoc)
51-
throws BuildException {
50+
public static JsonObject documentToSimpleManifest(Document mnfstDoc) throws BuildException {
5251

5352
// Parse common package markup
5453
Element pkgElmnt = mnfstDoc.getRootElement();
@@ -58,8 +57,7 @@ public static JsonObject documentToSimpleManifest(Document mnfstDoc)
5857
// =======================================================================
5958
// Private static methods
6059
// =======================================================================
61-
private static JsonObject parsePackageElement(Element pkgElmnt)
62-
throws BuildException {
60+
private static JsonObject parsePackageElement(Element pkgElmnt) throws BuildException {
6361

6462
// Does root element have correct local name?
6563
if (!"package".equals(pkgElmnt.getName())) {
@@ -73,7 +71,7 @@ private static JsonObject parsePackageElement(Element pkgElmnt)
7371
// Package ID and version
7472
String pkgId = pkgElmnt.getAttributeValue("id");
7573
String version = pkgElmnt.getAttributeValue("version");
76-
//ContentPackage mnfst = new ContentPackage(pkgId, version);
74+
// ContentPackage mnfst = new ContentPackage(pkgId, version);
7775
JsonObject mnfstBuilder = new JsonObject();
7876

7977
mnfstBuilder.addProperty("@id", pkgId);
@@ -104,6 +102,12 @@ private static JsonObject parsePackageElement(Element pkgElmnt)
104102
JsonElement elementToPreferenceSet = elementToPreferenceSet(prefsElmnt);
105103
mnfstBuilder.add("preferences", elementToPreferenceSet);
106104

105+
// Language
106+
Element pkgLanguageElmnt = pkgElmnt.getChild("language");
107+
if (pkgLanguageElmnt != null) {
108+
mnfstBuilder.addProperty("language", pkgLanguageElmnt == null ? null : pkgLanguageElmnt.getTextNormalize());
109+
}
110+
107111
return mnfstBuilder;
108112
}
109113

src/main/java/edu/cmu/oli/content/resource/builders/ContentPkgXmlWriter.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package edu.cmu.oli.content.resource.builders;
22

3-
import edu.cmu.oli.content.models.persistance.entities.ContentPackage;
4-
import edu.cmu.oli.content.models.persistance.entities.WebContent;
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
57
import org.jdom2.DocType;
68
import org.jdom2.Document;
79
import org.jdom2.Element;
@@ -11,9 +13,8 @@
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

14-
import java.io.FileOutputStream;
15-
import java.io.IOException;
16-
import java.io.OutputStream;
16+
import edu.cmu.oli.content.models.persistance.entities.ContentPackage;
17+
import edu.cmu.oli.content.models.persistance.entities.WebContent;
1718

1819
/**
1920
* Methods for generating content package manifest XML.
@@ -26,11 +27,11 @@ public final class ContentPkgXmlWriter {
2627
private static final String _SYSTEM_ID = "http://oli.cmu.edu/dtd/oli_content_package_simple_2_0.dtd";
2728
// Metadata and preference namespaces
2829

29-
private static final Namespace _METADATA_NS
30-
= Namespace.getNamespace("cmd", "http://oli.web.cmu.edu/content/metadata/");
30+
private static final Namespace _METADATA_NS = Namespace.getNamespace("cmd",
31+
"http://oli.web.cmu.edu/content/metadata/");
3132

32-
private static final Namespace _PREFERENCES_NS
33-
= Namespace.getNamespace("pref", "http://oli.web.cmu.edu/preferences/");
33+
private static final Namespace _PREFERENCES_NS = Namespace.getNamespace("pref",
34+
"http://oli.web.cmu.edu/preferences/");
3435

3536
private static final Logger log = LoggerFactory.getLogger(ContentPkgXmlWriter.class);
3637

@@ -46,39 +47,37 @@ private ContentPkgXmlWriter() {
4647

4748
/**
4849
* <p>
49-
* Converts the given content package manifest to an XML document and writes
50-
* the document to the specified file. The resulting XML will validate to
51-
* the OLI content package DTD, provided the package was constructed in
52-
* accordance with the general contract of said DTD.
50+
* Converts the given content package manifest to an XML document and writes the
51+
* document to the specified file. The resulting XML will validate to the OLI
52+
* content package DTD, provided the package was constructed in accordance with
53+
* the general contract of said DTD.
5354
*
5455
* @param mnfst content package manifest
5556
* @param dstFile destination file
5657
* @throws NullPointerException if either argument is <tt>null</tt>
5758
* @throws IOException if an error occurs while outputting the document
5859
*/
59-
public static void manifestToFile(ContentPackage mnfst, java.io.File dstFile)
60-
throws IOException {
60+
public static void manifestToFile(ContentPackage mnfst, java.io.File dstFile) throws IOException {
6161

6262
try ( // Convert package and output document
63-
FileOutputStream fos = new FileOutputStream(dstFile)) {
63+
FileOutputStream fos = new FileOutputStream(dstFile)) {
6464
manifestToStream(mnfst, fos);
6565
}
6666
}
6767

6868
/**
6969
* <p>
70-
* Converts the given content package manifest to an XML document and writes
71-
* the document to the specified output stream. The resulting XML will
72-
* validate to the OLI content package DTD, provided the package was
73-
* constructed in accordance with the general contract of said DTD.
70+
* Converts the given content package manifest to an XML document and writes the
71+
* document to the specified output stream. The resulting XML will validate to
72+
* the OLI content package DTD, provided the package was constructed in
73+
* accordance with the general contract of said DTD.
7474
*
7575
* @param mnfst content package manifest
7676
* @param os output stream
7777
* @throws NullPointerException if either argument is <tt>null</tt>
7878
* @throws IOException if an error occurs while outputting the document
7979
*/
80-
public static void manifestToStream(ContentPackage mnfst, OutputStream os)
81-
throws IOException {
80+
public static void manifestToStream(ContentPackage mnfst, OutputStream os) throws IOException {
8281

8382
if (mnfst == null) {
8483
throw (new NullPointerException("'mnfst' cannot be null"));
@@ -97,10 +96,9 @@ public static void manifestToStream(ContentPackage mnfst, OutputStream os)
9796

9897
/**
9998
* <p>
100-
* Converts the given content package manifest to an XML document. The
101-
* resulting XML will validate to the OLI content package DTD, provided the
102-
* package was constructed in accordance with the general contract of said
103-
* DTD.
99+
* Converts the given content package manifest to an XML document. The resulting
100+
* XML will validate to the OLI content package DTD, provided the package was
101+
* constructed in accordance with the general contract of said DTD.
104102
*
105103
* @param contentPkg content package manifest
106104
* @return XML document representation of the content package manifest
@@ -128,9 +126,9 @@ public static Document manifestToDocument(ContentPackage contentPkg) {
128126
// =======================================================================
129127
private static Element manifestToElement(ContentPackage contentPkg) {
130128

131-
Element pkgElmnt = new Element("package");//mnfstDoc.getRootElement();
129+
Element pkgElmnt = new Element("package");// mnfstDoc.getRootElement();
132130

133-
//ContentPackage mnfst = parsePackageJson(pkgElmnt);
131+
// ContentPackage mnfst = parsePackageJson(pkgElmnt);
134132

135133
// Package ID and version
136134
pkgElmnt.setAttribute("id", contentPkg.getId());
@@ -152,7 +150,8 @@ private static Element manifestToElement(ContentPackage contentPkg) {
152150

153151
// Metadata
154152
if (contentPkg.getMetadata() != null) {
155-
Element pkgMdElmnt = MetadataWriter.metadataToElement(contentPkg.getMetadata().getJsonObject(), _METADATA_NS);
153+
Element pkgMdElmnt = MetadataWriter.metadataToElement(contentPkg.getMetadata().getJsonObject(),
154+
_METADATA_NS);
156155
if (pkgMdElmnt != null) {
157156
pkgElmnt.addContent(pkgMdElmnt);
158157
}
@@ -167,12 +166,20 @@ private static Element manifestToElement(ContentPackage contentPkg) {
167166

168167
// Preferences
169168
if (contentPkg.getOptions() != null) {
170-
Element prefsElmnt = OptionsWriter.preferenceSetToElement(contentPkg.getOptions().getJsonObject(), _PREFERENCES_NS);
169+
Element prefsElmnt = OptionsWriter.preferenceSetToElement(contentPkg.getOptions().getJsonObject(),
170+
_PREFERENCES_NS);
171171
if (prefsElmnt != null) {
172172
pkgElmnt.addContent(prefsElmnt);
173173
}
174174
}
175175

176+
// Language
177+
if (contentPkg.getLanguage() != null) {
178+
Element pkgLanguageElmnt = new Element("langauge");
179+
pkgLanguageElmnt.setText(contentPkg.getLanguage());
180+
pkgElmnt.addContent(pkgLanguageElmnt);
181+
}
182+
176183
return pkgElmnt;
177184
}
178185
}

0 commit comments

Comments
 (0)