use of org.xml.sax.SAXException in project OpenAM by OpenRock.
the class ValidatorImpl method validate.
private boolean validate(Object o, boolean validateId) throws ValidationException {
try {
//ValidatableObject vo = Util.toValidatableObject(o);
ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o);
if (vo == null)
throw new ValidationException(Messages.format(Messages.NOT_VALIDATABLE));
EventInterceptor ei = new EventInterceptor(eventHandler);
ValidationContext context = new ValidationContext(jaxbContext, ei, validateId);
context.validate(vo);
context.reconcileIDs();
return !ei.hadError();
} catch (SAXException e) {
// we need a consistent mechanism to convert SAXException into JAXBException
Exception nested = e.getException();
if (nested != null) {
throw new ValidationException(nested);
} else {
throw new ValidationException(e);
}
//return false;
}
}
use of org.xml.sax.SAXException in project pcgen by PCGen.
the class ReadXML method readxmlFile.
/**
* Reads through the file and parses the XML.
* @param table the file that is the table.
*/
private void readxmlFile(File table) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Logging.debugPrint("readxmlFile called.");
try {
DocumentBuilder db = dbf.newDocumentBuilder();
/* The document used for XML parsing. */
final Document document = db.parse(table);
tableName = document.getElementsByTagName("lookuptable").item(0).getAttributes().getNamedItem("name").getNodeValue();
/* The rows of the table. */
final int rows = document.getElementsByTagName("row").getLength();
int items = document.getElementsByTagName("item").getLength();
/* The columns of a tabke. */
final int cols = items / rows;
vt.setName(table.getPath());
int pos = 0;
for (int x = 0; x < rows; x++) {
Collection<String> row = new ArrayList<>();
for (int y = 0; y < cols; y++) {
row.add(document.getElementsByTagName("item").item(pos).getChildNodes().item(0).getNodeValue());
pos++;
}
vt.add(row);
}
} catch (ParserConfigurationException | IllegalArgumentException | IOException | SAXException e) {
Logging.errorPrint(e.getLocalizedMessage());
Logging.errorPrint("Could not parse xml file " + table.getPath());
Logging.errorPrint("IO", e);
}
}
use of org.xml.sax.SAXException in project pcgen by PCGen.
the class ClassDataHandler method startElement.
/**
* @throws SAXException
* @throws IllegalArgumentException if the file being processed is not the
* same GameMode as requested.
*
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
@Override
public void startElement(final String uri, final String localName, final String aName, final Attributes anAttrs) throws SAXException {
if (//$NON-NLS-1$
theState == ParserState.INIT && "class_data".equals(aName)) {
if (anAttrs != null) {
//$NON-NLS-1$
final String gm = anAttrs.getValue("game_mode");
if (!SystemCollections.getGameModeNamed(gm).equals(theGameMode)) {
//$NON-NLS-1$
throw new IllegalArgumentException("Incorrect game mode");
}
theValidFlag = true;
}
return;
}
if (!theValidFlag) {
//$NON-NLS-1$
throw new SAXException("NPCGen.Options.InvalidFileFormat");
}
if (theState == ParserState.INIT) {
if (//$NON-NLS-1$
"class".equals(aName)) {
if (anAttrs != null) {
//$NON-NLS-1$
final String classKey = anAttrs.getValue("key");
final PCClass pcClass = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(PCClass.class, classKey);
if (pcClass == null) {
//$NON-NLS-1$
Logging.errorPrintLocalised("Exceptions.PCGenParser.ClassNotFound", classKey);
} else {
theCurrentData = new ClassData(pcClass);
theState = ParserState.CLASSDATA;
}
}
}
} else if (theState == ParserState.CLASSDATA) {
if (//$NON-NLS-1$
"stats".equals(aName)) {
theState = ParserState.STATDATA;
} else if (//$NON-NLS-1$
"skills".equals(aName)) {
theState = ParserState.SKILLDATA;
} else if (//$NON-NLS-1$
"abilities".equals(aName)) {
theState = ParserState.ABILITYDATA;
theCurrentCategory = AbilityCategory.FEAT;
if (anAttrs != null) {
//$NON-NLS-1$
final String catName = anAttrs.getValue("category");
if (catName != null) {
theCurrentCategory = SettingsHandler.getGame().getAbilityCategory(catName);
}
}
} else if (//$NON-NLS-1$
"spells".equals(aName)) {
theState = ParserState.SPELLDATA;
theCurrentSpellType = SpellType.KNOWN;
if (anAttrs != null) {
//$NON-NLS-1$
final String bookName = anAttrs.getValue("type");
if (bookName != null) {
if (//$NON-NLS-1$
"Prepared Spells".equals(bookName)) {
theCurrentSpellType = SpellType.PREPARED;
}
}
}
} else if (//$NON-NLS-1$
"subclasses".equals(aName)) {
theState = ParserState.SUBCLASSDATA;
}
} else if (theState == ParserState.STATDATA) {
if (//$NON-NLS-1$
"stat".equals(aName)) {
if (anAttrs != null) {
final int weight = getWeight(anAttrs);
//$NON-NLS-1$
final String statAbbr = anAttrs.getValue("value");
if (statAbbr != null) {
PCStat stat = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(PCStat.class, statAbbr);
theCurrentData.addStat(stat, weight);
}
}
}
} else if (theState == ParserState.SKILLDATA) {
if (//$NON-NLS-1$
"skill".equals(aName)) {
if (anAttrs != null) {
final int weight = getWeight(anAttrs);
//$NON-NLS-1$
final String key = anAttrs.getValue("value");
if (key != null) {
if (//$NON-NLS-1$
"*".equals(key)) {
remainingWeight = weight;
} else if (//$NON-NLS-1$
key.startsWith("TYPE")) {
final List<Skill> skillsOfType = Globals.getPObjectsOfType(Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Skill.class), key.substring(5));
if (skillsOfType.isEmpty()) {
//$NON-NLS-1$ //$NON-NLS-2$
Logging.debugPrint("NPCGenerator: No skills of type found (" + key + ")");
}
} else {
final Skill skill = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, key);
if (skill == null) {
//$NON-NLS-1$ //$NON-NLS-2$
Logging.debugPrint("NPCGenerator: Skill not found (" + key + ")");
}
}
if (//$NON-NLS-1$
weight > 0 && !key.equals("*")) {
theCurrentData.addSkill(key, weight);
} else {
removeList.add(key);
}
}
}
}
} else if (theState == ParserState.ABILITYDATA) {
if (//$NON-NLS-1$
"ability".equals(aName)) {
if (anAttrs != null) {
final int weight = getWeight(anAttrs);
//$NON-NLS-1$
final String key = anAttrs.getValue("value");
if (key != null) {
if (//$NON-NLS-1$
"*".equals(key)) {
remainingWeight = weight;
} else if (//$NON-NLS-1$
key.startsWith("TYPE")) {
Type type = Type.getConstant(key.substring(5));
for (final Ability ability : Globals.getContext().getReferenceContext().getManufacturer(Ability.class, theCurrentCategory).getAllObjects()) {
if (!ability.containsInList(ListKey.TYPE, type)) {
continue;
}
if (ability.getSafe(ObjectKey.VISIBILITY) == Visibility.DEFAULT) {
if (weight > 0) {
theCurrentData.addAbility(theCurrentCategory, ability, weight);
} else {
// We have to remove any feats of this
// type.
// TODO - This is a little goofy. We
// already have the feat but we will
// store the key and reretrieve it.
removeList.add(ability.getKeyName());
}
}
}
} else {
final Ability ability = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, theCurrentCategory, key);
if (ability == null) {
//$NON-NLS-1$ //$NON-NLS-2$
Logging.debugPrint("Ability (" + key + ") not found");
} else if (weight > 0) {
theCurrentData.addAbility(theCurrentCategory, ability, weight);
} else {
// We have to remove any feats of this
// type.
// TODO - This is a little goofy. We
// already have the feat but we will
// store the key and reretrieve it.
removeList.add(ability.getKeyName());
}
}
}
}
}
} else if (theState == ParserState.SPELLDATA) {
if (//$NON-NLS-1$
"level".equals(aName) && anAttrs != null) {
//$NON-NLS-1$
final String lvlStr = anAttrs.getValue("id");
if (lvlStr != null) {
theCurrentLevel = Integer.parseInt(lvlStr);
theState = ParserState.SPELLLEVELDATA;
}
}
} else if (theState == ParserState.SPELLLEVELDATA) {
if (//$NON-NLS-1$
"spell".equals(aName) && anAttrs != null) {
final int weight = getWeight(anAttrs);
//$NON-NLS-1$
final String key = anAttrs.getValue("name");
if (key != null) {
if (//$NON-NLS-1$
"*".equals(key)) {
remainingWeight = weight;
} else if (//$NON-NLS-1$
key.startsWith("SCHOOL")) {
// Not sure how to do this yet
} else {
final Spell spell = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Spell.class, key);
if (spell != null) {
if (theCurrentSpellType == SpellType.KNOWN) {
theCurrentData.addKnownSpell(theCurrentLevel, spell, weight);
} else if (theCurrentSpellType == SpellType.PREPARED) {
theCurrentData.addPreparedSpell(theCurrentLevel, spell, weight);
}
} else {
Logging.errorPrint("Spell \"" + key + "\" not found.");
}
}
}
}
} else if (theState == ParserState.SUBCLASSDATA) {
if (//$NON-NLS-1$
"subclass".equals(aName) && anAttrs != null) {
final int weight = getWeight(anAttrs);
//$NON-NLS-1$
final String key = anAttrs.getValue("value");
theCurrentData.addSubClass(key, weight);
}
}
}
use of org.xml.sax.SAXException in project android_frameworks_base by ResurrectionRemix.
the class MediaScanner method processWplPlayList.
private void processWplPlayList(String path, String playListDirectory, Uri uri, ContentValues values, Cursor fileList) {
FileInputStream fis = null;
try {
File f = new File(path);
if (f.exists()) {
fis = new FileInputStream(f);
mPlaylistEntries.clear();
Xml.parse(fis, Xml.findEncodingByName("UTF-8"), new WplHandler(playListDirectory, uri, fileList).getContentHandler());
processCachedPlaylist(fileList, values, uri);
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
Log.e(TAG, "IOException in MediaScanner.processWplPlayList()", e);
}
}
}
use of org.xml.sax.SAXException in project android_frameworks_base by ResurrectionRemix.
the class HtmlToSpannedConverter method convert.
public Spanned convert() {
mReader.setContentHandler(this);
try {
mReader.parse(new InputSource(new StringReader(mSource)));
} catch (IOException e) {
// We are reading from a string. There should not be IO problems.
throw new RuntimeException(e);
} catch (SAXException e) {
// TagSoup doesn't throw parse exceptions.
throw new RuntimeException(e);
}
// Fix flags and range for paragraph-type markup.
Object[] obj = mSpannableStringBuilder.getSpans(0, mSpannableStringBuilder.length(), ParagraphStyle.class);
for (int i = 0; i < obj.length; i++) {
int start = mSpannableStringBuilder.getSpanStart(obj[i]);
int end = mSpannableStringBuilder.getSpanEnd(obj[i]);
// If the last line of the range is blank, back off by one.
if (end - 2 >= 0) {
if (mSpannableStringBuilder.charAt(end - 1) == '\n' && mSpannableStringBuilder.charAt(end - 2) == '\n') {
end--;
}
}
if (end == start) {
mSpannableStringBuilder.removeSpan(obj[i]);
} else {
mSpannableStringBuilder.setSpan(obj[i], start, end, Spannable.SPAN_PARAGRAPH);
}
}
return mSpannableStringBuilder;
}
Aggregations