use of java.nio.charset.IllegalCharsetNameException in project maven-archetype by apache.
the class ArchetypeDescriptorBuilder method addSourceToDescriptor.
/**
* Adds the source element <code>source</code> to the list of sources in the
* <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
* <i>filtered</i> and with the encoding specified in the <code>encoding</code>
* attribute or the Java virtual machine's default if it is not defined.
*
* @param source a <code><source></code> element from the <code><sources></code>
* @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
* @throws XmlPullParserException if the encoding specified is not valid or supported.
*/
private static void addSourceToDescriptor(Xpp3Dom source, ArchetypeDescriptor descriptor) throws XmlPullParserException {
descriptor.addSource(source.getValue());
TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor(source.getValue());
sourceDesc.setFiltered(true);
if (source.getAttribute("encoding") != null) {
try {
sourceDesc.setEncoding(source.getAttribute("encoding"));
} catch (IllegalCharsetNameException icne) {
throw new XmlPullParserException(source.getAttribute("encoding") + " is not a valid encoding.");
} catch (UnsupportedCharsetException uce) {
throw new XmlPullParserException(source.getAttribute("encoding") + " is not a supported encoding.");
}
}
}
use of java.nio.charset.IllegalCharsetNameException in project maven-archetype by apache.
the class ArchetypeDescriptorBuilder method addResourceToDescriptor.
/**
* Adds the resource element <code>resource</code> to the list of resources in the
* <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
* <i>filtered</i> if the attribute <code>filtered</code> was not
* specified or its value is <code>"true"</code>, or <code>false</code>
* if its value is <code>"false"</code>, and the encoding specified
* in the <code>encoding</code> attribute or the Java virtual machine's default if
* it is not defined. If the <code>resource</code> is a property file (ends in
* <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
* even if some other encoding is specified in the attribute.
*
* @param resource a <code><resource></code> element from the <code><resources></code>
* @param descriptor the <code>ArchetypeDescriptor</code> to add the resource template to.
* @throws XmlPullParserException if the encoding specified is not valid or supported or if the
* value of the attribute <code>filtered</code> is no valid.
*/
private static void addResourceToDescriptor(Xpp3Dom resource, ArchetypeDescriptor descriptor) throws XmlPullParserException {
descriptor.addResource(resource.getValue());
if (resource.getAttribute("filtered") != null) {
TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor(resource.getValue());
try {
resourceDesc.setFiltered(getValueFilteredAttribute(resource.getAttribute("filtered")));
} catch (IllegalArgumentException iae) {
throw new XmlPullParserException(iae.getMessage());
}
}
if (resource.getAttribute("encoding") != null) {
TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor(resource.getValue());
try {
resourceDesc.setEncoding(resource.getAttribute("encoding"));
} catch (IllegalCharsetNameException icne) {
throw new XmlPullParserException(resource.getAttribute("encoding") + " is not a valid encoding.");
} catch (UnsupportedCharsetException uce) {
throw new XmlPullParserException(resource.getAttribute("encoding") + " is not a supported encoding.");
}
}
if (resource.getValue().endsWith(".properties")) {
TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor(resource.getValue());
resourceDesc.setEncoding("iso-8859-1");
}
}
use of java.nio.charset.IllegalCharsetNameException in project maven-archetype by apache.
the class ArchetypeDescriptorBuilder method addTestSourceToDescriptor.
/**
* Adds the test-source element <code>source</code> to the list of sources in the
* <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
* <i>filtered</i> and with the encoding specified in the <code>encoding</code>
* attribute or the Java virtual machine's default if it is not defined.
*
* @param testSource a <code><source></code> element from the <code><testSources></code>
* @param descriptor the <code>ArchetypeDescriptor</code> to add the test-source template to.
* @throws XmlPullParserException if the encoding specified is not valid or supported.
*/
private static void addTestSourceToDescriptor(Xpp3Dom testSource, ArchetypeDescriptor descriptor) throws XmlPullParserException {
descriptor.addTestSource(testSource.getValue());
TemplateDescriptor testSourceDesc = descriptor.getTestSourceDescriptor(testSource.getValue());
testSourceDesc.setFiltered(true);
if (testSource.getAttribute("encoding") != null) {
try {
testSourceDesc.setEncoding(testSource.getAttribute("encoding"));
} catch (IllegalCharsetNameException icne) {
throw new XmlPullParserException(testSource.getAttribute("encoding") + " is not a valid encoding.");
} catch (UnsupportedCharsetException uce) {
throw new XmlPullParserException(testSource.getAttribute("encoding") + " is not a supported encoding.");
}
}
}
use of java.nio.charset.IllegalCharsetNameException in project ceylon-compiler by ceylon.
the class RecognizedOptions method getAll.
/**
* Get all the recognized options.
* @param helper an {@code OptionHelper} to help when processing options
* @return an array of options
*/
public static Option[] getAll(final OptionHelper helper) {
return new Option[] { new Option(G, "opt.g"), new Option(G_NONE, "opt.g.none") {
@Override
public boolean process(Options options, String option) {
options.put("-g:", "none");
return false;
}
}, new Option(G_CUSTOM, "opt.g.lines.vars.source", Option.ChoiceKind.ANYOF, "lines", "vars", "source"), new XOption(XLINT, "opt.Xlint"), new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist", Option.ChoiceKind.ANYOF, getXLintChoices()), // -nowarn is retained for command-line backward compatibility
new Option(NOWARN, "opt.nowarn") {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:none", option);
return false;
}
}, new Option(VERBOSE, "opt.verbose"), new Option(VERBOSE_CUSTOM, "opt.verbose.suboptlist") {
public boolean matches(String s) {
return s.startsWith("-verbose:");
}
public boolean process(Options options, String option) {
String suboptions = option.substring(9);
options.put("-verbose:", suboptions);
// enter all the -verbose suboptions as "-verbose:suboption"
for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
String tok = t.nextToken();
// make sure all is an alias for --verbose
if (tok.equals("all"))
options.put(VERBOSE, "true");
String opt = "-verbose:" + tok;
options.put(opt, opt);
}
return false;
}
}, // -deprecation is retained for command-line backward compatibility
new Option(DEPRECATION, "opt.deprecation") {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:deprecation", option);
return false;
}
}, new Option(CLASSPATH, "opt.arg.path", "opt.classpath"), new Option(CP, "opt.arg.path", "opt.classpath") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-classpath", arg);
}
}, new COption(CEYLONCWD, "opt.arg.path", "opt.ceyloncwd"), new COption(CEYLONREPO, "opt.arg.url", "opt.ceylonrepo") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(CEYLONREPO, arg);
return false;
}
}, new COption(CEYLONSYSTEMREPO, "opt.arg.url", "opt.ceylonsystemrepo"), new COption(CEYLONCACHEREPO, "opt.arg.url", "opt.ceyloncacherepo"), new COption(CEYLONNODEFREPOS, "opt.ceylonnodefrepos"), new COption(CEYLONUSER, "opt.arg.value", "opt.ceylonuser"), new COption(CEYLONPASS, "opt.arg.value", "opt.ceylonpass"), new COption(CEYLONNOOSGI, "opt.ceylonnoosgi"), new COption(CEYLONOSGIPROVIDEDBUNDLES, "opt.arg.value", "opt.ceylonosgiprovidedbundles"), new COption(CEYLONNOPOM, "opt.ceylonnopom"), new COption(CEYLONPACK200, "opt.ceylonpack200"), new COption(CEYLONRESOURCEROOT, "opt.arg.path", "opt.ceylonresourceroot"), new COption(CEYLONDISABLEOPT, "opt.ceylondisableopt"), new COption(CEYLONDISABLEOPT_CUSTOM, "opt.ceylondisableopt.suboptlist"), new COption(CEYLONSUPPRESSWARNINGS, "opt.arg.value", "opt.ceylonsuppresswarnings"), new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(SOURCEPATH, arg);
return false;
}
}, new COption(CEYLONSOURCEPATH, "opt.arg.directory", "opt.ceylonsourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(SOURCEPATH, arg);
return false;
}
}, new COption(CEYLONRESOURCEPATH, "opt.arg.url", "opt.ceylonresourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(CEYLONRESOURCEPATH, arg);
return false;
}
}, new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
@Override
public boolean process(Options options, String option, String arg) {
options.remove("-Xbootclasspath/p:");
options.remove("-Xbootclasspath/a:");
return super.process(options, option, arg);
}
}, new XOption(XBOOTCLASSPATH_PREPEND, "opt.arg.path", "opt.Xbootclasspath.p"), new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"), new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
@Override
public boolean process(Options options, String option, String arg) {
options.remove("-Xbootclasspath/p:");
options.remove("-Xbootclasspath/a:");
return super.process(options, "-bootclasspath", arg);
}
}, new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"), new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-extdirs", arg);
}
}, new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"), new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-endorseddirs", arg);
}
}, new Option(PROC, "opt.proc.none.only", Option.ChoiceKind.ONEOF, "none", "only"), new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"), new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"), new Option(D, "opt.arg.directory", "opt.d"), new COption(CEYLONOUT, "opt.arg.url", "opt.ceylonout") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-d", arg);
}
}, new COption(CEYLONOFFLINE, "opt.ceylonoffline"), new COption(CEYLONTIMEOUT, "opt.arg.number", "opt.ceylontimeout"), new COption(CEYLONCONTINUE, "opt.ceyloncontinue"), new COption(CEYLONPROGRESS, "opt.ceylonprogress"), new COption(CEYLONAUTOEXPORTMAVENDEPENDENCIES, "opt.ceylonautoexportmavendependencies"), new COption(CEYLONFLATCLASSPATH, "opt.ceylonflatclasspath"), new COption(CEYLONOVERRIDES, "opt.arg.url", "opt.ceylonoverrides"), // backwards-compat
new COption(CEYLONMAVENOVERRIDES, "opt.arg.url", "opt.ceylonoverrides") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-overrides", arg);
}
}, new Option(S, "opt.arg.directory", "opt.sourceDest"), new Option(IMPLICIT, "opt.implicit", Option.ChoiceKind.ONEOF, "none", "class"), new Option(ENCODING, "opt.arg.encoding", "opt.encoding") {
@Override
public boolean process(Options options, String option, String operand) {
try {
Charset.forName(operand);
options.put(option, operand);
return false;
} catch (UnsupportedCharsetException e) {
helper.error("err.unsupported.encoding", operand);
return true;
} catch (IllegalCharsetNameException e) {
helper.error("err.unsupported.encoding", operand);
return true;
}
}
}, new Option(SOURCE, "opt.arg.release", "opt.source") {
@Override
public boolean process(Options options, String option, String operand) {
Source source = Source.lookup(operand);
if (source == null) {
helper.error("err.invalid.source", operand);
return true;
}
return super.process(options, option, operand);
}
}, new Option(TARGET, "opt.arg.release", "opt.target") {
@Override
public boolean process(Options options, String option, String operand) {
Target target = Target.lookup(operand);
if (target == null) {
helper.error("err.invalid.target", operand);
return true;
}
return super.process(options, option, operand);
}
}, new COption(VERSION, "opt.version") {
@Override
public boolean process(Options options, String option) {
helper.printVersion();
return super.process(options, option);
}
}, new HiddenOption(FULLVERSION) {
@Override
public boolean process(Options options, String option) {
helper.printFullVersion();
return super.process(options, option);
}
}, new HiddenOption(DIAGS) {
@Override
public boolean process(Options options, String option) {
Option xd = getOptions(helper, EnumSet.of(XD))[0];
option = option.substring(option.indexOf('=') + 1);
String diagsOption = option.contains("%") ? "-XDdiagsFormat=" : "-XDdiags=";
diagsOption += option;
if (xd.matches(diagsOption))
return xd.process(options, diagsOption);
else
return false;
}
}, new COption(HELP, "opt.help") {
@Override
public boolean process(Options options, String option) {
helper.printHelp();
return super.process(options, option);
}
}, new Option(A, "opt.arg.key.equals.value", "opt.A") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean matches(String arg) {
return arg.startsWith("-A");
}
@Override
public boolean hasArg() {
return false;
}
// Mapping for processor options created in
// JavacProcessingEnvironment
@Override
public boolean process(Options options, String option) {
int argLength = option.length();
if (argLength == 2) {
helper.error("err.empty.A.argument");
return true;
}
int sepIndex = option.indexOf('=');
String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength));
if (!JavacProcessingEnvironment.isValidOptionName(key)) {
helper.error("err.invalid.A.key", option);
return true;
}
return process(options, option, option);
}
}, new Option(X, "opt.X") {
@Override
public boolean process(Options options, String option) {
helper.printXhelp();
return super.process(options, option);
}
}, // It's actually implemented by the launcher.
new Option(J, "opt.arg.flag", "opt.J") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean process(Options options, String option) {
throw new AssertionError("the -J flag should be caught by the launcher.");
}
}, // new Option("-moreinfo", "opt.moreinfo") {
new HiddenOption(MOREINFO) {
@Override
public boolean process(Options options, String option) {
Type.moreInfo = true;
return super.process(options, option);
}
}, // treat warnings as errors
new Option(WERROR, "opt.Werror"), new Option(SRC, "opt.arg.src", "opt.src") {
public boolean process(Options options, String option, String arg) {
return super.process(options, "-src", arg);
}
}, // use complex inference from context in the position of a method call argument
new HiddenOption(COMPLEXINFERENCE), // new Option("-prompt", "opt.prompt"),
new HiddenOption(PROMPT), // dump stack on error
new HiddenOption(DOE), // new Option("-s", "opt.s"),
new HiddenOption(PRINTSOURCE), // allow us to compile ceylon.language
new HiddenOption(BOOTSTRAPCEYLON), // display warnings for generic unchecked operations
new HiddenOption(WARNUNCHECKED) {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:unchecked", option);
return false;
}
}, new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"), new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"), new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
@Override
public boolean process(Options options, String option, String arg) {
try {
helper.setOut(new PrintWriter(new FileWriter(arg), true));
} catch (java.io.IOException e) {
helper.error("err.error.writing.file", arg, e);
return true;
}
return super.process(options, option, arg);
}
}, new XOption(XPRINT, "opt.print"), new XOption(XPRINTROUNDS, "opt.printRounds"), new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"), new XOption(XPREFER, "opt.prefer", Option.ChoiceKind.ONEOF, "source", "newer"), new XOption(XPKGINFO, "opt.pkginfo", Option.ChoiceKind.ONEOF, "always", "legacy", "nonempty"), /* -O is a no-op, accepted for backward compatibility. */
new HiddenOption(O), /* -Xjcov produces tables to support the code coverage tool jcov. */
new HiddenOption(XJCOV), /* This is a back door to the compiler's option table.
* -XDx=y sets the option x to the value y.
* -XDx sets the option x to the value x.
*/
new HiddenOption(XD) {
String s;
@Override
public boolean matches(String s) {
this.s = s;
return s.startsWith(name.optionName);
}
@Override
public boolean process(Options options, String option) {
s = s.substring(name.optionName.length());
int eq = s.indexOf('=');
String key = (eq < 0) ? s : s.substring(0, eq);
String value = (eq < 0) ? s : s.substring(eq + 1);
options.put(key, value);
return false;
}
}, // It's actually implemented by the CommandLine class.
new Option(AT, "opt.arg.file", "opt.AT") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean process(Options options, String option) {
throw new AssertionError("the @ flag should be caught by CommandLine.");
}
}, /*
* TODO: With apt, the matches method accepts anything if
* -XclassAsDecls is used; code elsewhere does the lookup to
* see if the class name is both legal and found.
*
* In apt, the process method adds the candidate class file
* name to a separate list.
*/
new HiddenOption(SOURCEFILE) {
String s;
@Override
public boolean matches(String s) {
this.s = s;
return // Java source file
s.endsWith(".java") || // FIXME: Should be a FileManager query
s.endsWith(".ceylon") || // FIX for ceylon because default is not a valid name for Java
"default".equals(s) || // Legal type name for Ceylon
isCeylonName(s) || // Possibly a resource file
(new File(s)).isFile();
}
@Override
public boolean process(Options options, String option) {
File f = new File(s);
if (s.endsWith(".java") || // FIXME: Should be a FileManager query
s.endsWith(".ceylon")) {
// Most likely a source file
if (!f.isFile()) {
// -sourcepath not -src because the COption for
// CEYLONSOURCEPATH puts it in the options map as -sourcepath
List<String> sourcePaths = options.getMulti("-sourcepath");
if (sourcePaths.isEmpty())
sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
if (checkIfModule(sourcePaths, s)) {
// A Ceylon module name that ends with .ceylon or .java
helper.addClassName(s);
return false;
}
if (f.exists()) {
helper.error("err.file.not.file", f);
return true;
}
}
if (!f.exists()) {
helper.error("err.file.not.found", f);
return true;
}
helper.addFile(f);
}
if (f.isFile()) {
// Most likely a resource file
helper.addFile(f);
} else {
// the default module is always allowed, it doesn't need to have any folder
if (s.equals(Module.DEFAULT_MODULE_NAME)) {
helper.addClassName(s);
return false;
}
// find a corresponding physical module in the source path
List<String> sourcePaths = options.getMulti("-sourcepath");
if (sourcePaths.isEmpty())
sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
if (checkIfModule(sourcePaths, s)) {
helper.addClassName(s);
return false;
}
String paths = sourcePaths.toString();
helper.error("err.module.not.found", s, paths.substring(1, paths.length() - 1));
return true;
}
return false;
}
private boolean checkIfModule(List<String> paths, String moduleName) {
String moduleDirName = moduleName.replace(".", File.separator);
// walk every path arg
for (String path : paths) {
// split the path
for (String part : path.split("\\" + File.pathSeparator)) {
// try to see if it's a module folder
File moduleFolder = new File(part, moduleDirName);
if (moduleFolder.isDirectory()) {
return true;
}
}
}
return false;
}
} };
}
use of java.nio.charset.IllegalCharsetNameException in project BibleMultiConverter by schierlm.
the class USFM method doImportBook.
private ParatextBook doImportBook(File inputFile, Charset charset) throws Exception {
KNOWN_CHARACTER_TAGS.addAll(AUTO_CLOSING_TAGS.keySet());
if (!inputFile.getName().toLowerCase().endsWith(".usfm") && !inputFile.getName().toLowerCase().endsWith(".sfm"))
return null;
String data = TextUtilities.usfmWhitespaceNormalization(new String(Files.readAllBytes(inputFile.toPath()), charset), preserveSpacesAtEndOfLines) + "\\$EOF$";
if (!data.startsWith("\\id ")) {
System.out.println("WARNING: Skipping malformed file " + inputFile);
return null;
}
int startPos = data.indexOf("\\", 2);
int finalPos = data.length() - "\\$EOF$".length();
String[] idParts = data.substring(4, startPos).trim().split(" ", 2);
ParatextID id = ParatextID.fromIdentifier(idParts[0].toUpperCase());
if (id == null) {
System.out.println("WARNING: Skipping book with unknown ID: " + idParts[0]);
return null;
}
ParatextBook result = new ParatextBook(id, idParts.length == 1 ? "" : idParts[1]);
List<ParatextCharacterContentContainer> containerStack = new ArrayList<>();
boolean ignoreAutoClosingTags = Boolean.getBoolean("biblemulticonverter.usfm.ignoreautoclosingtags");
VerseStart openVerse = null;
ChapterStart openChapter = null;
while (startPos < finalPos) {
if (data.charAt(startPos) != '\\')
throw new IllegalStateException();
int pos = data.indexOf('\\', startPos + 1);
String textPart = data.substring(startPos + 1, pos);
startPos = pos;
pos = Math.min(textPart.length(), 1 + Math.min((textPart + " ").indexOf(' '), (textPart + "*").indexOf('*')));
String tag = textPart.substring(0, pos).trim().toLowerCase();
textPart = textPart.substring(pos);
if (textPart.endsWith(" ")) {
String nextTag = data.substring(startPos + 1, Math.min(data.length(), startPos + 10)) + " *\\";
pos = Math.min(nextTag.indexOf('\\'), Math.min(nextTag.indexOf(' '), nextTag.indexOf('*')));
if (!KNOWN_CHARACTER_TAGS.contains(nextTag.substring(0, pos))) {
textPart = textPart.substring(0, textPart.length() - 1);
}
}
if (containerStack.isEmpty() && (AUTO_CLOSING_TAGS.containsKey(tag) || tag.equals("v") || FOOTNOTE_XREF_TAGS.containsKey(tag))) {
ParatextCharacterContent container = new ParatextCharacterContent();
result.getContent().add(container);
containerStack.add(container);
}
boolean closeCharacterAttributes = false;
if (PARAGRAPH_TAGS.containsKey(tag)) {
ParagraphKind kind = PARAGRAPH_TAGS.get(tag);
// if (kind.getCategory() != ParatextBook.ParagraphKindCategory.TEXT) {
// Close any open verse
// openVerse = closeOpenVerse(result, openVerse, false);
// }
result.getContent().add(new ParagraphStart(kind));
closeCharacterAttributes = true;
} else if (tag.endsWith("*")) {
String rawTag = tag.substring(0, tag.length() - 1);
while (!containerStack.isEmpty() && containerStack.get(containerStack.size() - 1) instanceof AutoClosingFormatting) {
AutoClosingFormatting acc = (AutoClosingFormatting) containerStack.get(containerStack.size() - 1);
if (acc.getUsedTag().equals(rawTag))
break;
containerStack.remove(containerStack.size() - 1);
}
boolean found = false;
if (AUTO_CLOSING_TAGS.containsKey(rawTag)) {
if (!containerStack.isEmpty() && containerStack.get(containerStack.size() - 1) instanceof AutoClosingFormatting) {
AutoClosingFormatting acc = (AutoClosingFormatting) containerStack.get(containerStack.size() - 1);
found = acc.getUsedTag().equals(rawTag);
}
} else if (FOOTNOTE_XREF_TAGS.containsKey(rawTag)) {
if (!containerStack.isEmpty() && containerStack.get(containerStack.size() - 1) instanceof FootnoteXref) {
FootnoteXref fx = (FootnoteXref) containerStack.get(containerStack.size() - 1);
found = fx.getKind().getTag().equals(rawTag);
}
} else {
System.out.println("WARNING: Skipping unknown end tag \\" + tag);
}
if (found) {
containerStack.remove(containerStack.size() - 1);
} else {
System.out.println("WARNING: Skipping mismatched end tag \\" + tag);
}
} else if (AUTO_CLOSING_TAGS.containsKey(tag)) {
if (!tag.startsWith("+") && !ignoreAutoClosingTags) {
while (!containerStack.isEmpty() && containerStack.get(containerStack.size() - 1) instanceof AutoClosingFormatting) {
containerStack.remove(containerStack.size() - 1);
}
}
AutoClosingFormatting nextContainer = new AutoClosingFormatting(AUTO_CLOSING_TAGS.get(tag), tag.startsWith("+"));
containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
containerStack.add(nextContainer);
if (nextContainer.getKind().getDefaultAttributes() != null && data.startsWith("\\" + tag + "*", startPos) && textPart.contains("|")) {
String[] defaultAttributes = nextContainer.getKind().getDefaultAttributes();
String[] parts = textPart.split("\\|");
for (int i = 1; i < parts.length; i++) {
if (parts[i].contains("=")) {
String attList = parts[i];
while (attList.contains("=")) {
pos = attList.indexOf('=');
String key = attList.substring(0, pos).trim();
attList = attList.substring(pos + 1).trim();
if (attList.startsWith("\"")) {
pos = attList.indexOf('"', 1);
nextContainer.getAttributes().put(key, attList.substring(1, pos));
attList = attList.substring(pos + 1).trim();
} else {
nextContainer.getAttributes().put(key, attList);
attList = "";
}
}
} else if (i - 1 < defaultAttributes.length) {
nextContainer.getAttributes().put(defaultAttributes[i - 1], parts[i]);
}
}
textPart = parts[0];
if (textPart.endsWith(" ")) {
textPart = textPart.substring(0, textPart.length() - 1);
}
}
} else if (tag.equals("v")) {
ImportUtilities.closeOpenVerse(result, openVerse);
String[] parts = textPart.split(" ", 2);
ChapterStart chapter = result.findLastBookContent(ChapterStart.class);
if (chapter == null) {
throw new IllegalStateException("Verse \\v found before chapter start milestone");
}
// A verse number in USFM 2 may be in the format 6-7, 6a or even 6-7a.
// Attempt to parse these numbers by first adding the book and chapter and then parsing it as a whole.
VerseIdentifier location = VerseIdentifier.fromStringOrThrow(openChapter.getLocation() + ":" + parts[0]);
openVerse = new VerseStart(location, parts[0]);
containerStack.get(containerStack.size() - 1).getContent().add(openVerse);
textPart = parts.length == 1 ? "" : parts[1];
} else if (tag.equals("c")) {
ImportUtilities.closeOpenVerse(result, openVerse);
openVerse = null;
// There is not really a good way to accurately determine where the end of a chapter should be placed
// based on USFM 2 content. Maybe a title above this chapter marker was already intended to be part of
// this chapter. This is basically a best guess. This should not really matter when converting from
// USFM 2 to USX 2 or USFX (which is based on USFM 2), however when up-converting to USX 3 this might
// lead to unexpected results.
ImportUtilities.closeOpenChapter(result, openChapter);
String[] parts = textPart.split(" ", 2);
if (!parts[0].matches("[0-9]+"))
throw new NumberFormatException("Invalid chapter number in \\c " + textPart);
openChapter = new ChapterStart(new ChapterIdentifier(id, Integer.parseInt(parts[0])));
result.getContent().add(openChapter);
closeCharacterAttributes = true;
textPart = parts.length == 1 ? "" : parts[1];
} else if (tag.matches("t[hc]r?[0-9]+")) {
result.getContent().add(new TableCellStart(tag));
closeCharacterAttributes = true;
} else if (FOOTNOTE_XREF_TAGS.containsKey(tag)) {
String[] parts = textPart.split(" ", 2);
FootnoteXref nextContainer = new FootnoteXref(FOOTNOTE_XREF_TAGS.get(tag), parts[0]);
containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
containerStack.add(nextContainer);
textPart = parts.length == 1 ? "" : parts[1];
} else if (tag.equals("id")) {
System.out.println("WARNING: Skipping duplicate \\id tag");
textPart = "";
} else if (tag.equals("ide")) {
Charset correctCharset;
try {
if (textPart.matches("[0-9]+ - .*")) {
int codepage = Integer.parseInt(textPart.replaceAll(" - .*", ""));
correctCharset = codepage == 65001 ? StandardCharsets.UTF_8 : Charset.forName("windows-" + codepage);
} else {
correctCharset = Charset.forName(textPart);
}
} catch (UnsupportedCharsetException | IllegalCharsetNameException ex) {
System.out.println("WARNING: Unknown charset " + textPart + " specified, falling back to ISO-8859-1");
correctCharset = StandardCharsets.ISO_8859_1;
}
if (!correctCharset.equals(charset)) {
if (!charset.equals(StandardCharsets.UTF_8)) {
throw new IOException("Two charsets specified: " + charset + " and " + correctCharset);
}
return doImportBook(inputFile, correctCharset);
} else {
result.getAttributes().put(tag, textPart);
}
textPart = "";
} else if (BOOK_HEADER_ATTRIBUTE_TAGS.contains(tag)) {
result.getAttributes().put(tag, textPart);
textPart = "";
} else {
System.out.println("WARNING: Skipping unknown tag \\" + tag);
}
if (closeCharacterAttributes) {
containerStack.clear();
}
textPart = textPart.replace(" // ", " ").replace("~", "\u00A0");
ParatextCharacterContent.Text text = ParatextCharacterContent.Text.from(textPart);
if (text != null) {
if (containerStack.isEmpty()) {
ParatextCharacterContent container = new ParatextCharacterContent();
containerStack.add(container);
result.getContent().add(container);
}
containerStack.get(containerStack.size() - 1).getContent().add(text);
}
}
ImportUtilities.closeOpenVerse(result, openVerse);
ImportUtilities.closeOpenChapter(result, openChapter);
return result;
}
Aggregations