use of org.xml.sax.helpers.DefaultHandler in project intellij-community by JetBrains.
the class MavenUtil method crcWithoutSpaces.
public static int crcWithoutSpaces(@NotNull InputStream in) throws IOException {
try {
final CRC32 crc = new CRC32();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.getXMLReader().setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
parser.parse(in, new DefaultHandler() {
boolean textContentOccur = false;
int spacesCrc;
private void putString(@Nullable String string) {
if (string == null)
return;
for (int i = 0, end = string.length(); i < end; i++) {
crc.update(string.charAt(i));
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
textContentOccur = false;
crc.update(1);
putString(qName);
for (int i = 0; i < attributes.getLength(); i++) {
putString(attributes.getQName(i));
putString(attributes.getValue(i));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
textContentOccur = false;
crc.update(2);
putString(qName);
}
private void processTextOrSpaces(char[] ch, int start, int length) {
for (int i = start, end = start + length; i < end; i++) {
char a = ch[i];
if (Character.isWhitespace(a)) {
if (textContentOccur) {
spacesCrc = spacesCrc * 31 + a;
}
} else {
if (textContentOccur && spacesCrc != 0) {
crc.update(spacesCrc);
crc.update(spacesCrc >> 8);
}
crc.update(a);
textContentOccur = true;
spacesCrc = 0;
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
processTextOrSpaces(ch, start, length);
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
processTextOrSpaces(ch, start, length);
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
putString(target);
putString(data);
}
@Override
public void skippedEntity(String name) throws SAXException {
putString(name);
}
@Override
public void error(SAXParseException e) throws SAXException {
crc.update(100);
}
});
return (int) crc.getValue();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
return -1;
}
}
use of org.xml.sax.helpers.DefaultHandler in project zaproxy by zaproxy.
the class ZapXmlConfiguration method createDocumentBuilder.
@Override
protected DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
DocumentBuilderFactory factory = XmlUtils.newXxeDisabledDocumentBuilderFactory();
// Same behaviour as base method:
if (isValidating()) {
factory.setValidating(true);
if (isSchemaValidation()) {
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
}
}
DocumentBuilder result = factory.newDocumentBuilder();
result.setEntityResolver(getEntityResolver());
if (isValidating()) {
result.setErrorHandler(new DefaultHandler() {
@Override
public void error(SAXParseException ex) throws SAXException {
throw ex;
}
});
}
return result;
}
use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method discoverFeeds.
public List<DiscoveredFeed> discoverFeeds(final String query) throws ReaderAPIException, IOException, GRTokenExpiredException, ParserConfigurationException, SAXException, GRAnsweredBadRequestException {
Timing t = new Timing("discoverFeeds()", context);
final List<DiscoveredFeed> results = new ArrayList<DiscoveredFeed>();
if (query == null || query.length() == 0)
return results;
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
final String queryPath = "/reader/api/0/feed-finder?q=";
HttpRequestBase req = createGRRequest(httpClient, getGoogleHost() + queryPath + query);
HttpResponse response = executeGRRequest(httpClient, req, true);
throwExceptionWhenNotStatusOK(response);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
DefaultHandler handler = new SimpleStringExtractorHandler() {
private DiscoveredFeed discoveredFeed;
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
super.endElement(uri, localName, name);
if (discoveredFeed != null && "entry".equals(localName)) {
// System.out.println("Added discovered feed " +
// discoveredFeed);
results.add(discoveredFeed);
discoveredFeed = null;
}
}
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
// System.out.println("startElement=" + localName);
if ("entry".equals(localName)) {
discoveredFeed = new DiscoveredFeed();
// System.out.println("Created new Discovered Feed");
return;
}
if ("link".equals(localName)) {
String rel = attributes.getValue("rel");
String href = attributes.getValue("href");
if ("self".equals(rel))
return;
if (discoveredFeed != null) {
if (rel != null) {
if ("alternate".equals(rel))
discoveredFeed.alternateUrl = href;
else if ("http://www.google.com/reader/atom/relation/feed".equals(rel))
discoveredFeed.feedUrl = href;
}
} else {
DiscoveredFeed df = new DiscoveredFeed();
df.title = query;
df.feedUrl = href;
results.add(df);
}
}
// System.out.println("startElement2=" + localName);
}
@Override
public void receivedString(String localName, String fqn, String s) {
if (discoveredFeed == null)
return;
if ("title".equals(localName)) {
discoveredFeed.title = s;
} else if ("content".equals(localName)) {
discoveredFeed.snippet = s;
}
}
};
parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
return results;
} finally {
httpClient.close();
t.stop();
}
}
use of org.xml.sax.helpers.DefaultHandler in project kotlin by JetBrains.
the class ModuleXmlParser method parse.
private ModuleScriptData parse(@NotNull InputStream xml) {
try {
setCurrentState(initial);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(xml, new DelegatedSaxHandler() {
@NotNull
@Override
protected DefaultHandler getDelegate() {
return currentState;
}
});
return new ModuleScriptData(modules);
} catch (ParserConfigurationException e) {
MessageCollectorUtil.reportException(messageCollector, e);
} catch (SAXException e) {
messageCollector.report(ERROR, OutputMessageUtil.renderException(e), NO_LOCATION);
} catch (IOException e) {
MessageCollectorUtil.reportException(messageCollector, e);
}
return ModuleScriptData.EMPTY;
}
use of org.xml.sax.helpers.DefaultHandler in project intellij-community by JetBrains.
the class ValidateXmlActionHandler method doParse.
public void doParse() {
try {
myParser.parse(new InputSource(new StringReader(myFile.getText())), new DefaultHandler() {
@Override
public void warning(SAXParseException e) throws SAXException {
if (myErrorReporter.isUniqueProblem(e))
myErrorReporter.processError(e, ProblemType.WARNING);
}
@Override
public void error(SAXParseException e) throws SAXException {
if (myErrorReporter.isUniqueProblem(e))
myErrorReporter.processError(e, ProblemType.ERROR);
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
if (myErrorReporter.isUniqueProblem(e))
myErrorReporter.processError(e, ProblemType.FATAL);
}
@Override
public InputSource resolveEntity(String publicId, String systemId) {
final PsiFile psiFile = myXmlResourceResolver.resolve(null, systemId);
if (psiFile == null)
return null;
return new InputSource(new StringReader(psiFile.getText()));
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
myParser.setProperty(ENTITY_RESOLVER_PROPERTY_NAME, myXmlResourceResolver);
configureEntityManager(myFile, myParser);
}
});
final String[] resourcePaths = myXmlResourceResolver.getResourcePaths();
if (resourcePaths.length > 0) {
// if caches are used
final VirtualFile[] files = new VirtualFile[resourcePaths.length];
for (int i = 0; i < resourcePaths.length; ++i) {
files[i] = UriUtil.findRelativeFile(resourcePaths[i], null);
}
myFile.putUserData(DEPENDENT_FILES_KEY, files);
myFile.putUserData(GRAMMAR_POOL_TIME_STAMP_KEY, calculateTimeStamp(files, myProject));
}
myFile.putUserData(KNOWN_NAMESPACES_KEY, getNamespaces(myFile));
} catch (SAXException e) {
LOG.debug(e);
} catch (Exception exception) {
filterAppException(exception);
} catch (StackOverflowError error) {
// http://issues.apache.org/jira/browse/XERCESJ-589
}
}
Aggregations