Author: scabanovich
Date: 2007-07-20 09:07:24 -0400 (Fri, 20 Jul 2007)
New Revision: 2539
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractExtendedXMLFileImpl.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractXMLFileImpl.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FileSystemsLoader.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/loaders/impl/SimpleWebFileLoader.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XMLUtil.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XModelObjectLoaderUtil.java
Log:
EXIN-86
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractExtendedXMLFileImpl.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractExtendedXMLFileImpl.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractExtendedXMLFileImpl.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -129,7 +129,8 @@
super.set(name, value);
if("incorrectBody".equals(name) && value.length() > 0) {
set("isIncorrect", "yes");
- setErrors(value, hasDTD(), !hasDTD()); //schema!
+// setErrors(value, hasDTD(), !hasDTD()); //never validate dtd
+ setErrors(value, false, false);
}
}
@@ -163,6 +164,7 @@
boolean errors1 = ("yes".equals(get("_hasErrors_")));
AbstractExtendedXMLFileImpl f = getUpdatedFile(body, true);
if(f == null) return;
+ f.getChildren();
XModelObjectImpl p = (XModelObjectImpl)getParent();
XModelImpl m = (XModelImpl)getModel();
@@ -170,11 +172,13 @@
if(!isOverlapped) getResourceMarkers().clear();
if(f.isIncorrect()) {
getChildren();
+ boolean fire = this.loaderError == null && f.loaderError != null;
+ this.loaderError = f.loaderError;
super.set("incorrectBody", f.get("incorrectBody"));
super.set("isIncorrect", "yes");
if(f.get("errors") != null) super.set("errors",
f.get("errors"));
- f.getChildren();
+ if(fire) changeTimeStamp();
if(f.get("actualBodyTimeStamp") != null &&
!"-1".equals(f.get("actualBodyTimeStamp"))) {
mergeAll(f, update);
}
@@ -219,7 +223,9 @@
private AbstractExtendedXMLFileImpl getUpdatedFile(String body, boolean fire) {
boolean errors1 = ("yes".equals(get("_hasErrors_")));
- setErrors(body, hasDTD(), !hasDTD()); ///schema!
+ loaderError = null;
+// setErrors(body, hasDTD(), !hasDTD()); //never validate dtd
+ setErrors(body, false, false);
boolean errors2 = (get("errors") != null &&
get("errors").length() > 0);
if(errors1 && errors2) {
super.set("incorrectBody".intern(), body);
@@ -245,7 +251,11 @@
super.set("incorrectBody", "");
super.set("isIncorrect","no");
super.set("errors", "");
+ loaderError = null;
}
+ if(update instanceof AbstractExtendedXMLFileImpl) {
+ loaderError = ((AbstractExtendedXMLFileImpl)update).loaderError;
+ }
Map<String,XModelObject> map = getChildrenForSaveAsMap();
Set<String> set = null;
XModelObject[] cs = update.getChildren();
@@ -270,7 +280,7 @@
}
map.remove(c.getPathPart());
}
- Iterator it = map.values().iterator();
+ Iterator<XModelObject> it = map.values().iterator();
while(it.hasNext()) {
((XModelObject)it.next()).removeFromParent();
}
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractXMLFileImpl.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractXMLFileImpl.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/AbstractXMLFileImpl.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -17,7 +17,6 @@
import org.jboss.tools.common.model.filesystems.impl.RecognizedFileImpl;
import org.jboss.tools.common.model.util.FindObjectHelper;
import org.jboss.tools.common.model.util.XMLUtil;
-import org.jboss.tools.common.xml.SAXValidator;
public class AbstractXMLFileImpl extends RecognizedFileImpl {
private static final long serialVersionUID = 3794621010387468744L;
@@ -25,6 +24,8 @@
protected String[] _errors = new String[0];
protected String[] errors = new String[0];
+ protected String loaderError = null;
+
/**
* Licence does not allows for distributing copies of dtds.
*/
@@ -41,11 +42,20 @@
public String[] getErrors() {
return _errors;
}
+
+ public void setLoaderError(String loaderError) {
+ this.loaderError = loaderError;
+ }
+
+ public String getLoaderError() {
+ return loaderError;
+ }
protected final void setErrors(String body, boolean checkDTD, boolean checkSchema) {
- String[] errors = (body.length() == 0) ? null
- : (checkSchema) ? new SAXValidator().getXMLErrors(new java.io.StringReader(body))
- : XMLUtil.getXMLErrors(new java.io.StringReader(body), checkDTD);
+ String[] errors = (body.length() == 0) ? null : XMLUtil.getXMLErrors(new
java.io.StringReader(body), checkDTD, checkSchema);
+ if(errors == null || errors.length == 0) {
+ if(loaderError != null) errors = new String[]{loaderError};
+ }
setErrors(body, errors);
}
@@ -81,7 +91,7 @@
if(s.equals(get("errors"))) return;
super.set("incorrectBody", body);
set("errors", s);
- setAttributeValue("isIncorrect", (errors.length == 0) ? "no" :
"yes");
+ setAttributeValue("isIncorrect", (errors.length == 0 && loaderError
== null) ? "no" : "yes");
if(!isOverlapped()) markers.update();
}
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FileSystemsLoader.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FileSystemsLoader.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FileSystemsLoader.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -12,9 +12,11 @@
import java.io.File;
import java.util.*;
+
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.w3c.dom.*;
+import org.jboss.tools.common.meta.XModelEntity;
import org.jboss.tools.common.model.*;
import org.jboss.tools.common.model.loaders.*;
import org.jboss.tools.common.model.loaders.impl.*;
@@ -129,7 +131,7 @@
}
XModelObject[] js = lib.getChildren();
String loc = lib.getAttributeValue("location");
- List paths = null;
+ List<String> paths = null;
try {
paths = EclipseResourceUtil.getClassPath(project);
} catch (Exception e) {
@@ -191,7 +193,24 @@
}
class FileSystemsLoaderUtil extends XModelObjectLoaderUtil {
- public boolean saveChildren(Element element, XModelObject o) {
+
+ protected Set<String> getAllowedChildren(XModelEntity entity) {
+ Set<String> children = super.getAllowedChildren(entity);
+ if("FILESYSTEMS".equals(entity.getXMLSubPath())) {
+ children.add("WEB");
+ }
+ return children;
+ }
+
+ protected Set<String> getAllowedAttributes(XModelEntity entity) {
+ Set<String> attributes = super.getAllowedAttributes(entity);
+ if("FILESYSTEMS".equals(entity.getXMLSubPath())) {
+ attributes.add("WORKSPACE_HOME");
+ }
+ return attributes;
+ }
+
+ public boolean saveChildren(Element element, XModelObject o) {
boolean b = super.saveChildren(element, o);
if(b && "FILESYSTEMS".equals(element.getNodeName())) {
saveWorkspaceHomeAttr(element, o);
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/loaders/impl/SimpleWebFileLoader.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/loaders/impl/SimpleWebFileLoader.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/loaders/impl/SimpleWebFileLoader.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -62,18 +62,25 @@
element.setAttribute("EXTENSION" + postfix,
object.getAttributeValue("extension"));
util.load(element, object);
+ String loadingError = util.getError();
setEncoding(object, body);
loadPublicId(object, doc);
object.set("actualBodyTimeStamp", "" + object.getTimeStamp());
+
+ ((AbstractXMLFileImpl)object).setLoaderError(loadingError);
+ if(!((AbstractXMLFileImpl)object).isIncorrect() && loadingError != null) {
+ object.setAttributeValue("isIncorrect", "yes");
+ object.setAttributeValue("incorrectBody", body);
+ object.set("actualBodyTimeStamp", "" + object.getTimeStamp());
+ }
}
protected Document loadDocument(XModelObject object, String body) {
- String[] errors = XMLUtil.getXMLErrors(new StringReader(body), isCheckingDTD());
- if(isCheckingSchema()) {
- errors = new SAXValidator().getXMLErrors(new StringReader(body));
- }
+ String[] errors =
+// XMLUtil.getXMLErrors(new StringReader(body), isCheckingDTD(),
isCheckingSchema());
+ XMLUtil.getXMLErrors(new StringReader(body), false, false);
if(errors != null && errors.length > 0) {
object.setAttributeValue("isIncorrect", "yes");
object.set("correctBody", "");
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XMLUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XMLUtil.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XMLUtil.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -14,6 +14,7 @@
import org.w3c.dom.*;
import org.xml.sax.*;
+import org.jboss.tools.common.xml.SAXValidator;
import org.jboss.tools.common.xml.XMLEntityResolver;
import org.jboss.tools.common.xml.XMLUtilities;
import javax.xml.parsers.*;
@@ -103,5 +104,10 @@
public static String[] getXMLErrors(org.xml.sax.InputSource is, boolean checkDTD) {
return XMLUtilities.getXMLErrors(is, checkDTD, XMLEntityResolver.getInstance());
}
+
+ public static String[] getXMLErrors(Reader reader, boolean checkDTD, boolean
checkSchema) {
+ return (checkSchema) ? new SAXValidator().getXMLErrors(reader)
+ : getXMLErrors(reader, checkDTD);
+ }
}
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XModelObjectLoaderUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XModelObjectLoaderUtil.java 2007-07-20
12:26:52 UTC (rev 2538)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/XModelObjectLoaderUtil.java 2007-07-20
13:07:24 UTC (rev 2539)
@@ -17,9 +17,12 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
+import java.util.Map;
import java.util.Properties;
+import java.util.Set;
import java.util.StringTokenizer;
import org.apache.xml.serialize.OutputFormat;
@@ -49,18 +52,25 @@
public class XModelObjectLoaderUtil {
public static String ATTR_ID_NAME = "_id_";
- private Hashtable singular = null;
+ private Hashtable<String,String> singular = null;
private boolean saveentity = true;
private String namespace = null;
private NamespaceMapping namespaceMapping = null;
+
+ protected String error = null;
public XModelObjectLoaderUtil() {}
- public void setup(Hashtable singular, boolean saveentity) {
+ public void setup(Hashtable<String,String> singular, boolean saveentity) {
this.singular = singular;
this.saveentity = saveentity;
+ error = null;
}
+ public String getError() {
+ return error;
+ }
+
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@@ -79,9 +89,98 @@
} else {
loadAttributes(element, o);
loadChildren(element, o);
+
+ if(error == null) {
+ Set<String> allowed = getAllowedChildren(o.getModelEntity());
+ if(allowed != null && hasUnallowedChildren(element, allowed));
+ }
+ if(error == null) {
+ Set<String> allowed = getAllowedAttributes(o.getModelEntity());
+ if(allowed != null && hasUnallowedAttributes(element, allowed));
+ }
+
}
}
+
+ static Map<XModelEntity, Set<String>> allowedChildren = new
HashMap<XModelEntity, Set<String>>();
+
+ protected Set<String> getAllowedChildren(XModelEntity entity) {
+ if(entity.getChild("AnyElement") != null) return null;
+ Set<String> x = allowedChildren.get(entity);
+ if(x != null) return x;
+ Set<String> children = new HashSet<String>();
+ XAttribute[] as = entity.getAttributes();
+ for (int i = 0; i < as.length; i++) {
+ String xml = as[i].getXMLName();
+ if(xml == null) continue;
+ int k = xml.indexOf('.');
+ if(k < 0) continue;
+ children.add(xml.substring(0, k));
+ }
+ XChild[] cs = entity.getChildren();
+ for (int i = 0; i < cs.length; i++) {
+ XModelEntity c = entity.getMetaModel().getEntity(cs[i].getName());
+ if(c == null) continue;
+ String xml = c.getXMLSubPath();
+ if(xml == null || xml.length() == 0) {
+ if(cs[i].isRequired()) {
+ Set<String> a1 = getAllowedChildren(c);
+ if(a1 == null) return null;
+ children.addAll(a1);
+ }
+ }
+ int k = xml.indexOf('.');
+ if(k >= 0) xml = xml.substring(0, k);
+ children.add(xml);
+ }
+ if(children != null) allowedChildren.put(entity, children);
+ return children;
+ }
+ protected Set<String> getAllowedAttributes(XModelEntity entity) {
+ if(entity.getChild("AnyElement") != null) return null;
+ Set<String> attributes = new HashSet<String>();
+ if(saveentity) attributes.add("ENTITY");
+ XAttribute[] as = entity.getAttributes();
+ for (int i = 0; i < as.length; i++) {
+ String xml = as[i].getXMLName();
+ if(xml == null) continue;
+ int k = xml.indexOf('.');
+ if(k >= 0) continue;
+ attributes.add(xml);
+ }
+ return attributes;
+ }
+
+ private boolean hasUnallowedChildren(Element element, Set<String> allowed) {
+ NodeList nl = element.getChildNodes();
+ for (int i = 0; i < nl.getLength(); i++) {
+ Node n = nl.item(i);
+ if(n.getNodeType() == Node.ELEMENT_NODE) {
+ String name = n.getNodeName();
+ if(allowed.contains(name)) continue;
+ error = "Unallowed child " + name + " of " +
element.getNodeName() + ":0:0";
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean hasUnallowedAttributes(Element element, Set<String> allowed) {
+ NamedNodeMap nl = element.getAttributes();
+ for (int i = 0; i < nl.getLength(); i++) {
+ Node n = nl.item(i);
+ if(n.getNodeType() == Node.ATTRIBUTE_NODE) {
+ String name = n.getNodeName();
+ if(allowed.contains(name)) continue;
+ if(name.startsWith("xmlns")) continue;
+ error = "Unallowed attribute " + name + " of " +
element.getNodeName() + ":0:0";;
+ return true;
+ }
+ }
+ return false;
+ }
+
public void loadAttributes(Element element, XModelObject o) {
XModelEntity entity = o.getModelEntity();
XAttribute[] as = entity.getAttributes();
@@ -606,7 +705,7 @@
public static XObjectLoader getObjectLoader(XModelObject object) {
try {
- Class c = object.getModelEntity().getLoadingClass();
+ Class<?> c = object.getModelEntity().getLoadingClass();
if (c != null) {
return (XObjectLoader)c.newInstance();
}