1717import java .io .IOException ;
1818import java .lang .System .Logger ;
1919import java .lang .System .Logger .Level ;
20+ import java .text .MessageFormat ;
2021import java .util .HashMap ;
2122import 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 ();
0 commit comments