Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlers #186

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ public void endElement(String uri, String localName, String qName) {
case "addAttributes" -> saxHandler.setState(this.completeState); // return to parentHandler
default -> String2.log("Unexpected end tag: " + localName);
}
content.setLength(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ public void endElement(String uri, String localName, String qName) throws Throwa
}
default -> String2.log("Unexpected end tag: " + localName);
}
content.setLength(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public void endElement(String uri, String localName, String qName) throws Throwa
}
default -> String2.log("Unexpected end tag: " + localName);
}
content.setLength(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package gov.noaa.pfel.erddap.handlers;

import static gov.noaa.pfel.erddap.dataset.EDDGrid.DEFAULT_MATCH_AXIS_N_DIGITS;

import com.cohort.array.StringArray;
import com.cohort.util.String2;
import gov.noaa.pfel.erddap.dataset.EDD;
import gov.noaa.pfel.erddap.dataset.EDDGrid;
import gov.noaa.pfel.erddap.dataset.EDDGridAggregateExistingDimension;
import gov.noaa.pfel.erddap.util.EDStatic;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class EDDGridAggregateExistingDimensionHandler extends State {
private StringBuilder content = new StringBuilder();
private State completeState;
private String datasetID;
private SaxParsingContext context;

public EDDGridAggregateExistingDimensionHandler(
SaxHandler saxHandler, String datasetID, State completeState, SaxParsingContext context) {
super(saxHandler);
this.completeState = completeState;
this.datasetID = datasetID;
this.context = context;
}

private EDDGrid firstChild = null;
private StringArray tLocalSourceUrls = new StringArray();
private String tAccessibleTo = null;
private String tGraphsAccessibleTo = null;
private boolean tAccessibleViaWMS = true;
private boolean tAccessibleViaFiles = EDStatic.defaultAccessibleViaFiles;
private StringArray tOnChange = new StringArray();
private String tFgdcFile = null;
private String tIso19115File = null;
private int tMatchAxisNDigits = DEFAULT_MATCH_AXIS_N_DIGITS;
private String tDefaultDataQuery = null;
private String tDefaultGraphQuery = null;
private int tnThreads = -1; // interpret invalid values (like -1) as EDStatic.nGridThreads
private boolean tDimensionValuesInMemory = true;

private String tSUServerType = null;
private String tSURegex = null;
private boolean tSURecursive = true;
private String tSUPathRegex = ".*";
private String tSU = null;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
switch (localName) {
case "dataset" -> {
if (firstChild == null) {
String tType = attributes.getValue("type");
if (tType == null || !tType.startsWith("EDDGrid")) {
throw new RuntimeException(
"Datasets.xml error: "
+ "The dataset defined in an "
+ "EDDGridAggregateExistingDimension must be a subclass of EDDGrid.");
}

String active = attributes.getValue("active");
String childDatasetID = attributes.getValue("datasetID");
State state =
HandlerFactory.getHandlerFor(
tType, childDatasetID, active, this, saxHandler, context);
saxHandler.setState(state);
} else {
throw new RuntimeException(
"Datasets.xml error: "
+ "There can be only one <dataset> defined within an "
+ "EDDGridAggregateExistingDimension <dataset>.");
}
}
case "sourceUrls" -> {
tSUServerType = attributes.getValue("serverType");
tSURegex = attributes.getValue("regex");
String tr = attributes.getValue("recursive");
tSUPathRegex = attributes.getValue("pathRegex");
tSURecursive = tr == null || String2.parseBoolean(tr);
}
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
content.append(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String qName) throws Throwable {
String contentStr = content.toString().trim();

switch (localName) {
case "sourceUrl" -> tLocalSourceUrls.add(contentStr);
case "sourceUrls" -> tSU = contentStr;
case "matchAxisNDigits" ->
tMatchAxisNDigits = String2.parseInt(contentStr, DEFAULT_MATCH_AXIS_N_DIGITS);
case "ensureAxisValuesAreEqual" ->
tMatchAxisNDigits = String2.parseBoolean(contentStr) ? 20 : 0;
case "accessibleTo" -> tAccessibleTo = contentStr;
case "graphsAccessibleTo" -> tGraphsAccessibleTo = contentStr;
case "accessibleViaWMS" -> tAccessibleViaWMS = String2.parseBoolean(contentStr);
case "accessibleViaFiles" -> tAccessibleViaFiles = String2.parseBoolean(contentStr);
case "onChange" -> tOnChange.add(contentStr);
case "fgdcFile" -> tFgdcFile = contentStr;
case "iso19115File" -> tIso19115File = contentStr;
case "defaultDataQuery" -> tDefaultDataQuery = contentStr;
case "defaultGraphQuery" -> tDefaultGraphQuery = contentStr;
case "nThreads" -> tnThreads = String2.parseInt(contentStr);
case "dimensionValuesInMemory" -> tDimensionValuesInMemory = String2.parseBoolean(contentStr);
case "dataset" -> {
EDD dataset =
new EDDGridAggregateExistingDimension(
datasetID,
tAccessibleTo,
tGraphsAccessibleTo,
tAccessibleViaWMS,
tAccessibleViaFiles,
tOnChange,
tFgdcFile,
tIso19115File,
tDefaultDataQuery,
tDefaultGraphQuery,
firstChild,
tLocalSourceUrls.toArray(),
tSUServerType,
tSURegex,
tSURecursive,
tSUPathRegex,
tSU,
tMatchAxisNDigits,
tnThreads,
tDimensionValuesInMemory);

this.completeState.handleDataset(dataset);
saxHandler.setState(this.completeState);
}
default -> String2.log("Unexpected end tag: " + localName);
}
content.setLength(0);
}

@Override
public void handleDataset(EDD dataset) {
firstChild = (EDDGrid) dataset;
}
}
125 changes: 125 additions & 0 deletions WEB-INF/classes/gov/noaa/pfel/erddap/handlers/EDDGridCopyHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package gov.noaa.pfel.erddap.handlers;

import static gov.noaa.pfel.erddap.dataset.EDDGrid.DEFAULT_MATCH_AXIS_N_DIGITS;

import com.cohort.array.StringArray;
import com.cohort.util.SimpleException;
import com.cohort.util.String2;
import gov.noaa.pfel.erddap.dataset.EDD;
import gov.noaa.pfel.erddap.dataset.EDDGrid;
import gov.noaa.pfel.erddap.dataset.EDDGridCopy;
import gov.noaa.pfel.erddap.util.EDStatic;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class EDDGridCopyHandler extends State {
private StringBuilder content = new StringBuilder();
private State completeState;
private String datasetID;
private SaxParsingContext context;

public EDDGridCopyHandler(
SaxHandler saxHandler, String datasetID, State completeState, SaxParsingContext context) {
super(saxHandler);
this.completeState = completeState;
this.datasetID = datasetID;
this.context = context;
}

private EDDGrid tSourceEdd = null;
private int tReloadEveryNMinutes = Integer.MAX_VALUE;
private String tAccessibleTo = null;
private String tGraphsAccessibleTo = null;
private boolean tAccessibleViaWMS = true;
private int tMatchAxisNDigits = DEFAULT_MATCH_AXIS_N_DIGITS;
private StringArray tOnChange = new StringArray();
private String tFgdcFile = null;
private String tIso19115File = null;
private boolean checkSourceData = true;
private boolean tFileTableInMemory = false;
private String tDefaultDataQuery = null;
private String tDefaultGraphQuery = null;
private int tnThreads = -1;
private boolean tAccessibleViaFiles = EDStatic.defaultAccessibleViaFiles;
private boolean tDimensionValuesInMemory = true;
private String tOnlySince = null;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (localName.equals("dataset")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this throw an exception if it sees a second dataset?

Copy link
Contributor Author

@ayushsingh01042003 ayushsingh01042003 Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the old code specifies throwing an exception on getting a second child dataset,
https://github.com/ERDDAP/erddap/blob/main/WEB-INF/classes/gov/noaa/pfel/erddap/dataset/EDDGridFromEDDTable.java#L113-L127

But I am guessing since the tEDDTable variable will update to the last child dataset which means there is no point in adding multiple, you are implying to add a check so that we only have one child dataset?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, add a check to make sure there's only one child dataset. Allowing multiple to be parsed could cause issues, and I think it's better to catch those early with explicit error messages.

if (tSourceEdd != null) {
throw new SimpleException("Cannot Have multiple Child datasets for" + datasetID);
}
String tType = attributes.getValue("type");
String active = attributes.getValue("active");
String childDatasetID = attributes.getValue("datasetID");
State state =
HandlerFactory.getHandlerFor(tType, childDatasetID, active, this, saxHandler, context);
saxHandler.setState(state);
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
content.append(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String qName) throws Throwable {
String contentStr = content.toString().trim();

switch (localName) {
case "accessibleTo" -> tAccessibleTo = contentStr;
case "graphsAccessibleTo" -> tGraphsAccessibleTo = contentStr;
case "accessibleViaWMS" -> tAccessibleViaWMS = String2.parseBoolean(contentStr);
case "matchAxisNDigits" ->
tMatchAxisNDigits = String2.parseInt(contentStr, DEFAULT_MATCH_AXIS_N_DIGITS);
case "ensureAxisValuesAreEqual" ->
tMatchAxisNDigits = String2.parseBoolean(contentStr) ? 20 : 0; // deprecated
case "onChange" -> tOnChange.add(contentStr);
case "fgdcFile" -> tFgdcFile = contentStr;
case "iso19115File" -> tIso19115File = contentStr;
case "reloadEveryNMinutes" -> tReloadEveryNMinutes = String2.parseInt(contentStr);
case "checkSourceData" -> checkSourceData = String2.parseBoolean(contentStr);
case "fileTableInMemory" -> tFileTableInMemory = String2.parseBoolean(contentStr);
case "defaultDataQuery" -> tDefaultDataQuery = contentStr;
case "defaultGraphQuery" -> tDefaultGraphQuery = contentStr;
case "nThreads" -> tnThreads = String2.parseInt(contentStr);
case "dimensionValuesInMemory" -> tDimensionValuesInMemory = String2.parseBoolean(contentStr);
case "accessibleViaFiles" -> tAccessibleViaFiles = String2.parseBoolean(contentStr);
case "onlySince" -> tOnlySince = contentStr;
case "dataset" -> {
EDD dataset =
new EDDGridCopy(
datasetID,
tAccessibleTo,
tGraphsAccessibleTo,
tAccessibleViaWMS,
tMatchAxisNDigits,
tOnChange,
tFgdcFile,
tIso19115File,
tDefaultDataQuery,
tDefaultGraphQuery,
tReloadEveryNMinutes,
tSourceEdd,
tFileTableInMemory,
tAccessibleViaFiles,
tOnlySince,
tnThreads,
tDimensionValuesInMemory);

this.completeState.handleDataset(dataset);
saxHandler.setState(this.completeState);
}
default -> String2.log("Unexpected end tag: " + localName);
}
content.setLength(0);
}

@Override
public void handleDataset(EDD dataset) {
tSourceEdd = (EDDGrid) dataset;
}
}
Loading