Skip to content

Commit 5fde7e3

Browse files
committed
create empty resource files for missing locales in ResourceSets
Since we know how empty resource files look, we can create them to prevent Exceptions when loading ResourceSets with new locales. Fixes #52
1 parent 0506326 commit 5fde7e3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/main/java/de/vogel612/helper/data/ResourceFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static Stream<ResourceFile> getResourceFiles(Path folder, String name) th
101101
}
102102

103103
public static Stream<ResourceFile> getResourceFiles(ResourceSet resourceSet) throws IOException {
104+
resourceSet.files().filter(p -> !p.toFile().exists()).forEach(DataUtilities::createEmptyResourceFile);
104105
return resourceSet.files().map(ResourceFile::new);
105106
}
106107
}

src/main/java/de/vogel612/helper/data/util/DataUtilities.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,69 @@
1515
* benefit from the abstraction
1616
*/
1717
public class DataUtilities {
18+
19+
private static final String EMPTY_RESOURCE_FILE = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
20+
"<root>\n" +
21+
" <xsd:schema id=\"root\" xmlns=\"\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\n" +
22+
" <xsd:import namespace=\"http://www.w3.org/XML/1998/namespace\" />\n" +
23+
" <xsd:element name=\"root\" msdata:IsDataSet=\"true\">\n" +
24+
" <xsd:complexType>\n" +
25+
" <xsd:choice maxOccurs=\"unbounded\">\n" +
26+
" <xsd:element name=\"metadata\">\n" +
27+
" <xsd:complexType>\n" +
28+
" <xsd:sequence>\n" +
29+
" <xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" />\n" +
30+
" </xsd:sequence>\n" +
31+
" <xsd:attribute name=\"name\" use=\"required\" type=\"xsd:string\" />\n" +
32+
" <xsd:attribute name=\"type\" type=\"xsd:string\" />\n" +
33+
" <xsd:attribute name=\"mimetype\" type=\"xsd:string\" />\n" +
34+
" <xsd:attribute ref=\"xml:space\" />\n" +
35+
" </xsd:complexType>\n" +
36+
" </xsd:element>\n" +
37+
" <xsd:element name=\"assembly\">\n" +
38+
" <xsd:complexType>\n" +
39+
" <xsd:attribute name=\"alias\" type=\"xsd:string\" />\n" +
40+
" <xsd:attribute name=\"name\" type=\"xsd:string\" />\n" +
41+
" </xsd:complexType>\n" +
42+
" </xsd:element>\n" +
43+
" <xsd:element name=\"data\">\n" +
44+
" <xsd:complexType>\n" +
45+
" <xsd:sequence>\n" +
46+
" <xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"1\" />\n" +
47+
" <xsd:element name=\"comment\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"2\" />\n" +
48+
" </xsd:sequence>\n" +
49+
" <xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\" msdata:Ordinal=\"1\" />\n" +
50+
" <xsd:attribute name=\"type\" type=\"xsd:string\" msdata:Ordinal=\"3\" />\n" +
51+
" <xsd:attribute name=\"mimetype\" type=\"xsd:string\" msdata:Ordinal=\"4\" />\n" +
52+
" <xsd:attribute ref=\"xml:space\" />\n" +
53+
" </xsd:complexType>\n" +
54+
" </xsd:element>\n" +
55+
" <xsd:element name=\"resheader\">\n" +
56+
" <xsd:complexType>\n" +
57+
" <xsd:sequence>\n" +
58+
" <xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"1\" />\n" +
59+
" </xsd:sequence>\n" +
60+
" <xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\" />\n" +
61+
" </xsd:complexType>\n" +
62+
" </xsd:element>\n" +
63+
" </xsd:choice>\n" +
64+
" </xsd:complexType>\n" +
65+
" </xsd:element>\n" +
66+
" </xsd:schema>\n" +
67+
" <resheader name=\"resmimetype\">\n" +
68+
" <value>text/microsoft-resx</value>\n" +
69+
" </resheader>\n" +
70+
" <resheader name=\"version\">\n" +
71+
" <value>2.0</value>\n" +
72+
" </resheader>\n" +
73+
" <resheader name=\"reader\">\n" +
74+
" <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>\n" +
75+
" </resheader>\n" +
76+
" <resheader name=\"writer\">\n" +
77+
" <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>\n" +
78+
" </resheader>\n" +
79+
"</data>";
80+
1881
public static final String SINGLE_TRUTH_LOCALE = "";
1982

2083
private static final String FILE_NAME_FORMAT = "%s%s.resx";
@@ -93,4 +156,22 @@ public static String getFileIdentifier(Path file) {
93156
throw new IllegalArgumentException("The resx file does not match our permissive regex");
94157
}
95158
}
159+
160+
/**
161+
* Creates an empty resource file at the given path, assuming the file does not exist already
162+
*
163+
* @param path
164+
* The Path where the resource file needs to be created.
165+
*/
166+
public static void createEmptyResourceFile(Path path) {
167+
if (path.toFile().exists()) {
168+
return;
169+
}
170+
try {
171+
Files.createFile(path);
172+
Files.write(path, EMPTY_RESOURCE_FILE.getBytes());
173+
} catch (IOException e) {
174+
e.printStackTrace();
175+
}
176+
}
96177
}

0 commit comments

Comments
 (0)