Skip to content

Commit

Permalink
Trigger validation when catalog/file associatiosn changed (see #143)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Nov 9, 2018
1 parent 1ef27e2 commit 160418f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,43 @@ public void doSave(ISaveContext context) {
}
} else {
// Settings
updateSettings(context.getSettings());
updateSettings(context);
}
}

private void updateSettings(Object initializationOptionsSettings) {
private void updateSettings(ISaveContext saveContext) {
Object initializationOptionsSettings = saveContext.getSettings();
ContentModelSettings cmSettings = ContentModelSettings.getSettings(initializationOptionsSettings);
if (cmSettings != null) {
updateSettings(cmSettings);
updateSettings(cmSettings, saveContext);
}
}

private void updateSettings(ContentModelSettings settings) {
private void updateSettings(ContentModelSettings settings, ISaveContext context) {
if (settings.getCatalogs() != null) {
// Update XML catalog settings
ContentModelManager.getInstance().setCatalogs(settings.getCatalogs());
boolean catalogPathsChanged = ContentModelManager.getInstance().setCatalogs(settings.getCatalogs());
if (catalogPathsChanged) {
// Validate all opened XML files
context.collectDocumentToValidate(d -> {
XMLDocument xml = context.getDocument(d.getDocumentURI());
xml.resetGrammar();
return true;
});
}
}
if (settings.getFileAssociations() != null) {
// Update XML file associations
ContentModelManager.getInstance().setFileAssociations(settings.getFileAssociations());
boolean fileAssociationsChanged = ContentModelManager.getInstance()
.setFileAssociations(settings.getFileAssociations());
if (fileAssociationsChanged) {
// Validate all opened XML files
context.collectDocumentToValidate(d -> {
XMLDocument xml = context.getDocument(d.getDocumentURI());
xml.resetGrammar();
return true;
});
}
}
// Update use cache, only if it is set in the settings.
Boolean useCache = settings.isUseCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ private boolean isCacheable(String uri) {
* Set up XML catalogs.
*
* @param catalogs list of XML catalog files.
* @return true if catalogs changed and false otherwise
*/
public void setCatalogs(String[] catalogs) {
catalogResolverExtension.setCatalogs(catalogs);
public boolean setCatalogs(String[] catalogs) {
return catalogResolverExtension.setCatalogs(catalogs);
}

/**
Expand All @@ -174,9 +175,10 @@ public void refreshCatalogs() {
* Set file associations.
*
* @param fileAssociations
* @return true if file associations changed and false otherwise
*/
public void setFileAssociations(XMLFileAssociation[] fileAssociations) {
this.fileAssociationResolver.setFileAssociations(fileAssociations);
public boolean setFileAssociations(XMLFileAssociation[] fileAssociations) {
return this.fileAssociationResolver.setFileAssociations(fileAssociations);
}

public void setRootURI(String rootUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,34 @@ public Map<String, String> getExternalSchemaLocation() {
return externalSchemaLocation;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pattern == null) ? 0 : pattern.hashCode());
result = prime * result + ((systemId == null) ? 0 : systemId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XMLFileAssociation other = (XMLFileAssociation) obj;
if (pattern == null) {
if (other.pattern != null)
return false;
} else if (!pattern.equals(other.pattern))
return false;
if (systemId == null) {
if (other.systemId != null)
return false;
} else if (!systemId.equals(other.systemId))
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

import org.apache.xerces.impl.XMLEntityManager;
Expand Down Expand Up @@ -76,8 +77,10 @@ public void setRootUri(String rootUri) {
* Initialize catalogs path.
*
* @param catalogs the catalog path array.
* @return true if catalogs changed and false otherwise
*/
public void setCatalogs(String[] catalogs) {
public boolean setCatalogs(String[] catalogs) {
String[] oldCatalogs = catalogResolver != null ? catalogResolver.getCatalogList() : null;
if (catalogs != null) {
List<String> xmlCatalogFiles = new ArrayList<>();
for (String catalogPath : catalogs) {
Expand All @@ -103,6 +106,8 @@ public void setCatalogs(String[] catalogs) {
} else {
setCatalogResolver(null);
}
String[] newCatalogs = catalogResolver != null ? catalogResolver.getCatalogList() : null;
return !Objects.equals(oldCatalogs, newCatalogs);
}

private String expandSystemId(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.net.URI;
import java.util.Map;
import java.util.Objects;

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
Expand All @@ -29,9 +30,16 @@ public class XMLFileAssociationResolverExtension implements URIResolverExtension

private XMLFileAssociation[] fileAssociations;

public void setFileAssociations(XMLFileAssociation[] fileAssociations) {
/**
*
* @param fileAssociations
* @return true if file associations changed and false otherwise
*/
public boolean setFileAssociations(XMLFileAssociation[] fileAssociations) {
XMLFileAssociation[] oldFileAssociations = this.fileAssociations;
this.fileAssociations = fileAssociations;
expandSystemId();
return !Objects.equals(oldFileAssociations, fileAssociations);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ void registerExtension(IXMLExtension extension) {
try {
extensions.add(extension);
extension.start(params, this);
extension.doSave(initialSaveContext);
if (initialSaveContext != null) {
extension.doSave(initialSaveContext);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while initializing extension <" + extension.getClass().getName() + ">", e);
}
Expand Down

0 comments on commit 160418f

Please sign in to comment.