Skip to content

Commit 8f5418d

Browse files
committed
Extracted localizable strings
1 parent 046ca51 commit 8f5418d

12 files changed

Lines changed: 105 additions & 31 deletions

lib/xmljava.jar

1000 Bytes
Binary file not shown.

src/com/maxprograms/xml/Catalog.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.MalformedURLException;
2020
import java.net.URI;
2121
import java.net.URISyntaxException;
22+
import java.text.MessageFormat;
2223
import java.util.Hashtable;
2324
import java.util.Iterator;
2425
import java.util.List;
@@ -445,13 +446,15 @@ public void parseDTD(String publicId) {
445446
Iterator<EntityDecl> it = entities.iterator();
446447
while (it.hasNext()) {
447448
EntityDecl entity = it.next();
448-
String path = XMLUtils.getAbsolutePath(dtdFile.getParentFile().getAbsolutePath(), entity.getValue());
449+
String path = XMLUtils.getAbsolutePath(dtdFile.getParentFile().getAbsolutePath(),
450+
entity.getValue());
449451
addDtdEntity(entity.getPublicId(), path);
450452
}
451453
} catch (IOException | SAXException e) {
452454
// do nothing
453455
Logger logger = System.getLogger(Catalog.class.getName());
454-
logger.log(Level.WARNING, "Error parsing DTD " + publicId);
456+
MessageFormat mf = new MessageFormat(Messages.getString("Catalog.0"));
457+
logger.log(Level.WARNING, mf.format(new String[] { publicId }));
455458
}
456459
parsedDTDs.add(dtd);
457460
}

src/com/maxprograms/xml/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
public class Constants {
1616

17-
public static final String VERSION = "1.0.0";
18-
public static final String BUILD = "20230109_0740";
17+
public static final String VERSION = "1.1.0";
18+
public static final String BUILD = "20230227_1324";
1919
}

src/com/maxprograms/xml/CustomContentHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
9494
}
9595
current = stack.pop();
9696
} catch (EmptyStackException es) {
97-
throw new SAXException("Malformed content found.");
97+
throw new SAXException(Messages.getString("CustomContentHandler.0"));
9898
}
9999
}
100100

src/com/maxprograms/xml/DTDParser.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.lang.System.Logger;
1919
import java.lang.System.Logger.Level;
20+
import java.text.MessageFormat;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223

@@ -48,32 +49,35 @@ public Grammar parse(File file) throws SAXException, IOException {
4849
// Parameter-entity references
4950
int index = source.indexOf(";", pointer);
5051
if (index == -1) {
51-
throw new SAXException("Malformed entity reference");
52+
throw new SAXException(Messages.getString("DTDParser.0"));
5253
}
5354
String entityName = source.substring(pointer + "%".length(), index);
5455
if (!entitiesMap.containsKey(entityName)) {
55-
throw new SAXException("Referenced undeclared entity " + entityName);
56+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.1"));
57+
throw new SAXException(mf.format(new String[] { entityName }));
5658
}
5759
EntityDecl entity = entitiesMap.get(entityName);
5860
String module = entity.getValue();
5961
if (module == null || module.isBlank()) {
60-
throw new IOException("Error parsing referenced entity %" + entityName + ";");
62+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.2"));
63+
throw new IOException(mf.format(new String[] { entityName }));
6164
}
6265
String path = XMLUtils.getAbsolutePath(file.getParentFile().getAbsolutePath(), module);
6366
File mod = new File(path);
6467
if (mod.exists()) {
6568
parse(mod);
6669
} else {
6770
if (debug) {
68-
logger.log(Level.WARNING, "Module \"" + mod.getAbsolutePath() + "\" not found");
71+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.3"));
72+
logger.log(Level.WARNING, mf.format(new String[] { mod.getAbsolutePath() }));
6973
}
7074
}
7175
pointer += "%".length() + entityName.length() + ";".length();
7276
}
7377
if (lookingAt("<!ELEMENT", source, pointer)) {
7478
int index = source.indexOf(">", pointer);
7579
if (index == -1) {
76-
throw new SAXException("Malformed element declaration");
80+
throw new SAXException(Messages.getString("DTDParser.4"));
7781
}
7882
String elementText = source.substring(pointer, index + ">".length());
7983
ElementDecl elementDecl = new ElementDecl(elementText);
@@ -84,7 +88,7 @@ public Grammar parse(File file) throws SAXException, IOException {
8488
if (lookingAt("<!ATTLIST", source, pointer)) {
8589
int index = source.indexOf(">", pointer);
8690
if (index == -1) {
87-
throw new SAXException("Malformed attribute declaration");
91+
throw new SAXException(Messages.getString("DTDParser.5"));
8892
}
8993
String attListText = source.substring(pointer, index + ">".length());
9094
AttlistDecl attList = new AttlistDecl(attListText);
@@ -95,15 +99,16 @@ public Grammar parse(File file) throws SAXException, IOException {
9599
if (lookingAt("<!ENTITY", source, pointer)) {
96100
int index = source.indexOf(">", pointer);
97101
if (index == -1) {
98-
throw new SAXException("Malformed entity declaration");
102+
throw new SAXException(Messages.getString("DTDParser.6"));
99103
}
100104
String entityDeclText = source.substring(pointer, index + ">".length());
101105
EntityDecl entityDecl = new EntityDecl(entityDeclText);
102106
if (!entitiesMap.containsKey(entityDecl.getName())) {
103107
entitiesMap.put(entityDecl.getName(), entityDecl);
104108
} else {
105109
if (debug) {
106-
logger.log(Level.WARNING, "Duplicated entity declaration " + entityDecl);
110+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.7"));
111+
logger.log(Level.WARNING, mf.format(new String[] { entityDecl.getName() }));
107112
}
108113
}
109114
pointer += entityDeclText.length();
@@ -112,7 +117,7 @@ public Grammar parse(File file) throws SAXException, IOException {
112117
if (lookingAt("<!NOTATION", source, pointer)) {
113118
int index = source.indexOf(">", pointer);
114119
if (index == -1) {
115-
throw new SAXException("Malformed notation declaration");
120+
throw new SAXException(Messages.getString("DTDParser.8"));
116121
}
117122
String notationDeclText = source.substring(pointer, index + ">".length());
118123
NotationDecl notation = new NotationDecl(notationDeclText);
@@ -125,7 +130,7 @@ public Grammar parse(File file) throws SAXException, IOException {
125130
if (lookingAt("<?", source, pointer)) {
126131
int index = source.indexOf("?>", pointer);
127132
if (index == -1) {
128-
throw new SAXException("Malformed processing instruction");
133+
throw new SAXException(Messages.getString("DTDParser.9"));
129134
}
130135
String piText = source.substring(pointer, index + "?>".length());
131136
// ignore processing instructions
@@ -135,7 +140,7 @@ public Grammar parse(File file) throws SAXException, IOException {
135140
if (lookingAt("<!--", source, pointer)) {
136141
int index = source.indexOf("-->", pointer);
137142
if (index == -1) {
138-
throw new SAXException("Malformed comment");
143+
throw new SAXException(Messages.getString("DTDParser.10"));
139144
}
140145
String commentText = source.substring(pointer, index);
141146
// ignore comments
@@ -160,15 +165,15 @@ public Grammar parse(File file) throws SAXException, IOException {
160165
if ("INCLUDE".equals(type)) {
161166
int sectionStart = source.indexOf("[", pointer + "<![".length());
162167
if (sectionStart == -1) {
163-
throw new SAXException("Malformed conditional section");
168+
throw new SAXException(Messages.getString("DTDParser.11"));
164169
}
165170
String skip = source.substring(pointer, sectionStart + "[".length());
166171
pointer += skip.length();
167172
} else if ("IGNORE".equals(type)) {
168173
pointer += section.length();
169174
continue;
170175
} else {
171-
throw new SAXException("Malformed conditional section");
176+
throw new SAXException(Messages.getString("DTDParser.11"));
172177
}
173178
}
174179
if (pointer < source.length()) {
@@ -177,9 +182,12 @@ public Grammar parse(File file) throws SAXException, IOException {
177182
pointer++;
178183
continue;
179184
}
180-
logger.log(Level.ERROR, "Text before: " + source.substring(pointer - 20, pointer));
181-
logger.log(Level.ERROR, "Text after: " + source.substring(pointer, pointer + 20));
182-
throw new SAXException("Error parsing DTD " + file.getAbsolutePath());
185+
MessageFormat before = new MessageFormat(Messages.getString("DTDParser.12"));
186+
logger.log(Level.ERROR, before.format(new String[] { source.substring(pointer - 20, pointer) }));
187+
MessageFormat after = new MessageFormat(Messages.getString("DTDParser.13"));
188+
logger.log(Level.ERROR, after.format(new String[] { source.substring(pointer, pointer + 20) }));
189+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.14"));
190+
throw new SAXException(mf.format(new String[] { file.getAbsolutePath() }));
183191
}
184192
}
185193
return new Grammar(elementDeclMap, attributeListMap, entitiesMap, notationsMap);
@@ -215,7 +223,8 @@ private String getSectionType(String section) throws SAXException {
215223
while ((type.startsWith("%") || type.startsWith("&")) && type.endsWith(";")) {
216224
String entityName = type.substring(1, type.length() - 1);
217225
if (!entitiesMap.containsKey(entityName)) {
218-
throw new SAXException("Referenced undeclared entity " + entityName);
226+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.1"));
227+
throw new SAXException(mf.format(new String[] { entityName }));
219228
}
220229
EntityDecl entity = entitiesMap.get(entityName);
221230
type = entity.getValue();

src/com/maxprograms/xml/Document.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected Document(String namespaceURI, String qualifiedName, List<XMLNode> prol
9191
break;
9292
default:
9393
// should never happen
94-
logger.log(Level.WARNING, "Prolog contains wrong content type.");
94+
logger.log(Level.WARNING, Messages.getString("Document.0"));
9595
}
9696
}
9797
}
@@ -264,7 +264,7 @@ protected void setProlog(List<XMLNode> prolog) {
264264
break;
265265
default:
266266
// should never happen
267-
logger.log(Level.WARNING, "Prolog contains wrong content type.");
267+
logger.log(Level.WARNING, Messages.getString("Document.0"));
268268
}
269269
}
270270
}

src/com/maxprograms/xml/Element.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void clone(Element src) {
109109
break;
110110
default:
111111
// should never happen
112-
logger.log(Level.WARNING, "Element contains wrong content type.");
112+
logger.log(Level.WARNING, Messages.getString("Element.0"));
113113
}
114114
}
115115
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Maxprograms.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 1.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/org/documents/epl-v10.html
8+
*
9+
* Contributors:
10+
* Maxprograms - initial API and implementation
11+
*******************************************************************************/
12+
package com.maxprograms.xml;
13+
14+
import java.util.MissingResourceException;
15+
import java.util.ResourceBundle;
16+
17+
public class Messages {
18+
private static final String BUNDLE_NAME = Messages.class.getPackageName() + ".xmljava";
19+
20+
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
21+
22+
private Messages() {
23+
}
24+
25+
public static String getString(String key) {
26+
try {
27+
return RESOURCE_BUNDLE.getString(key);
28+
} catch (MissingResourceException e) {
29+
return '!' + key + '!';
30+
}
31+
}
32+
}

src/com/maxprograms/xml/RelaxNGParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.File;
1515
import java.io.IOException;
16+
import java.text.MessageFormat;
1617
import java.util.HashMap;
1718
import java.util.Hashtable;
1819
import java.util.Iterator;
@@ -214,7 +215,8 @@ private void replaceExternalRef(Element e) throws SAXException, IOException, Par
214215
RelaxNGParser parser = new RelaxNGParser(system, catalog);
215216
newContent.add(parser.getRootElement());
216217
} else {
217-
throw new SAXException("Missing " + child.getAttributeValue("href") + " in <externalRef>");
218+
MessageFormat mf = new MessageFormat(Messages.getString("RelaxNGParser.1"));
219+
throw new SAXException(mf.format(new String[] { child.getAttributeValue("href") }));
218220
}
219221
continue;
220222
}
@@ -260,7 +262,8 @@ private void replaceIncludes(Element e) throws SAXException, IOException, Parser
260262
}
261263
newContent.add(div);
262264
} else {
263-
throw new SAXException("Missing " + child.getAttributeValue("href") + " in <include>");
265+
MessageFormat mf = new MessageFormat(Messages.getString("RelaxNGParser.0"));
266+
throw new SAXException(mf.format(new String[] { child.getAttributeValue("href") }));
264267
}
265268
continue;
266269
}

src/com/maxprograms/xml/SAXBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.URI;
1919
import java.net.URL;
2020
import java.nio.charset.Charset;
21+
import java.text.MessageFormat;
2122
import java.util.Iterator;
2223
import java.util.List;
2324
import java.util.Map;
@@ -53,7 +54,8 @@ public SAXBuilder(boolean validating) {
5354
public Document build(String filename) throws SAXException, IOException, ParserConfigurationException {
5455
File f = new File(filename);
5556
if (!f.exists()) {
56-
throw new IOException("File '" + filename + "' does not exist.");
57+
MessageFormat mf = new MessageFormat(Messages.getString("SAXBuilder.1"));
58+
throw new IOException(mf.format(new String[] { filename }));
5759
}
5860
return build(f.toURI().toURL());
5961
}
@@ -64,7 +66,8 @@ public Document build(URI uri) throws SAXException, IOException, ParserConfigura
6466

6567
public Document build(File file) throws SAXException, IOException, ParserConfigurationException {
6668
if (!file.exists()) {
67-
throw new IOException("File '" + file.getAbsolutePath() + "' does not exist.");
69+
MessageFormat mf = new MessageFormat(Messages.getString("SAXBuilder.1"));
70+
throw new IOException(mf.format(new String[] { file.getAbsolutePath() }));
6871
}
6972
return build(file.toURI().toURL());
7073
}

0 commit comments

Comments
 (0)