Changeset 22714


Ignore:
Timestamp:
03/09/10 17:02:37 (17 months ago)
Author:
mnw21
Message:

Changed xml format of multi value constraints

Location:
branches/mnw21/pathquery_refactor/intermine
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/mnw21/pathquery_refactor/intermine/api/main/src/org/intermine/api/xml/SavedQueryHandler.java

    r21699 r22714  
    6262     */ 
    6363    @Override 
    64     public void endElement(String uri, String localName, String qName) { 
     64    public void endElement(String uri, String localName, String qName) throws SAXException { 
    6565        super.endElement(uri, localName, qName); 
    6666        if ("saved-query".equals(qName)) { 
  • branches/mnw21/pathquery_refactor/intermine/api/main/src/org/intermine/api/xml/TemplateQueryHandler.java

    r21957 r22714  
    1313import java.util.ArrayList; 
    1414import java.util.HashMap; 
     15import java.util.LinkedHashSet; 
    1516import java.util.List; 
    1617import java.util.Map; 
     
    8788                            + templateName); 
    8889                } 
    89                 PathConstraint constraint = processConstraint(query, path, attrs); 
    90                 if ((code == null) || (constraint instanceof PathConstraintLoop)) { 
    91                     query.addConstraint(constraint); 
    92                 } else { 
    93                     query.addConstraint(constraint, code); 
     90                constraintPath = path; 
     91                constraintAttributes = new HashMap<String, String>(); 
     92                for (int i = 0; i < attrs.getLength(); i++) { 
     93                    constraintAttributes.put(attrs.getQName(i), attrs.getValue(i)); 
    9494                } 
    95                 String description = attrs.getValue("description"); 
    96                 String editable = attrs.getValue("editable"); 
    97                 if ("true".equals(editable)) { 
    98                     editableConstraints.add(constraint); 
    99                 } 
    100                 constraintDescriptions.put(constraint, description); 
    101                 String switchable = attrs.getValue("switchable"); 
    102                 if ("on".equals(switchable)) { 
    103                     constraintSwitchables.put(constraint, SwitchOffAbility.ON); 
    104                 } else if ("off".equals(switchable)) { 
    105                     constraintSwitchables.put(constraint, SwitchOffAbility.OFF); 
    106                 } 
     95                constraintValues = new LinkedHashSet<String>(); 
     96                constraintCode = code; 
     97 
    10798            } 
    10899        } else { 
     
    115106     */ 
    116107    @Override 
    117     public void endElement(String uri, String localName, String qName) { 
    118         super.endElement(uri, localName, qName); 
     108    public void endElement(String uri, String localName, String qName) throws SAXException { 
    119109        if ("template".equals(qName)) { 
    120110            TemplateQuery t = new TemplateQuery(templateName, templateTitle, templateComment, 
     
    130120            templates.put(templateName, t); 
    131121            reset(); 
     122        } else if ("constraint".equals(qName)) { 
     123            PathConstraint constraint = processConstraint(query, constraintPath, 
     124                    constraintAttributes, constraintValues); 
     125            if ((constraintCode == null) || (constraint instanceof PathConstraintLoop)) { 
     126                query.addConstraint(constraint); 
     127            } else { 
     128                query.addConstraint(constraint, constraintCode); 
     129            } 
     130            String description = constraintAttributes.get("description"); 
     131            String editable = constraintAttributes.get("editable"); 
     132            if ("true".equals(editable)) { 
     133                editableConstraints.add(constraint); 
     134            } 
     135            constraintDescriptions.put(constraint, description); 
     136            String switchable = constraintAttributes.get("switchable"); 
     137            if ("on".equals(switchable)) { 
     138                constraintSwitchables.put(constraint, SwitchOffAbility.ON); 
     139            } else if ("off".equals(switchable)) { 
     140                constraintSwitchables.put(constraint, SwitchOffAbility.OFF); 
     141            } 
     142            constraintPath = null; 
     143        } else { 
     144            super.endElement(uri, localName, qName); 
    132145        } 
    133146    } 
  • branches/mnw21/pathquery_refactor/intermine/pathquery/main/src/org/intermine/pathquery/PathQueryBinding.java

    r22151 r22714  
    181181            } else if (constraint.getKey() instanceof PathConstraintMultiValue) { 
    182182                writer.writeAttribute("op", "" + constraint.getKey().getOp()); 
    183                 StringBuilder sb = new StringBuilder(); 
    184                 boolean needComma = false; 
    185183                for (String value : ((PathConstraintMultiValue) constraint.getKey()).getValues()) { 
    186                     if (value.contains(",")) { 
    187                         throw new XMLStreamException("Value in MultiValue contains a comma - this " 
    188                                 + "query cannot be represented in XML"); 
    189                     } 
    190184                    if (!value.equals(value.trim())) { 
    191185                        throw new XMLStreamException("Value in MultiValue starts or ends with " 
    192186                                + "whitespace - this query cannot be represented in XML"); 
    193187                    } 
    194                     if (needComma) { 
    195                         sb.append(", "); 
    196                     } 
    197                     needComma = true; 
    198                     sb.append(value); 
    199                 } 
    200                 writer.writeAttribute("values", sb.toString()); 
     188                    writer.writeStartElement("value"); 
     189                    writer.writeCharacters(value); 
     190                    writer.writeEndElement(); 
     191                } 
    201192            } else if (constraint.getKey() instanceof PathConstraintLoop) { 
    202193                writer.writeAttribute("op", "" + constraint.getKey().getOp()); 
  • branches/mnw21/pathquery_refactor/intermine/pathquery/main/src/org/intermine/pathquery/PathQueryHandler.java

    r22186 r22714  
    4949                "float", "double", "short", "int", "long", "Boolean", "Float", "Double", "Short", 
    5050                "Integer", "Long", "BigDecimal", "Date", "String")); 
     51    private StringBuilder valueBuffer = null; 
     52    protected String constraintPath = null; 
     53    protected Map<String, String> constraintAttributes = null; 
     54    protected Collection<String> constraintValues = null; 
     55    protected String constraintCode = null; 
    5156 
    5257    /** 
     
    6671            @SuppressWarnings("unused") String localName, String qName, Attributes attrs) 
    6772        throws SAXException { 
     73        if (valueBuffer != null) { 
     74            throw new SAXException("Cannot have any tags inside a value tag"); 
     75        } 
     76        if ((constraintPath != null) && (!"value".equals(qName))) { 
     77            throw new SAXException("Cannot have anything other than value tag inside a constraint"); 
     78        } 
    6879        if ("query-list".equals(qName)) { 
    6980            // Do nothing 
     
    132143            } else { 
    133144                path = path.replace(':', '.'); 
    134                 PathConstraint constraint = processConstraint(query, path, attrs); 
    135                 if (code == null) { 
    136                     query.addConstraint(constraint); 
    137                 } else { 
    138                     query.addConstraint(constraint, code); 
    139                 } 
     145                constraintPath = path; 
     146                constraintAttributes = new HashMap<String, String>(); 
     147                for (int i = 0; i < attrs.getLength(); i++) { 
     148                    constraintAttributes.put(attrs.getQName(i), attrs.getValue(i)); 
     149                } 
     150                constraintValues = new LinkedHashSet<String>(); 
     151                constraintCode = code; 
    140152            } 
    141153        } else if ("pathDescription".equals(qName)) { 
     
    153165                throw new SAXException("Unknown join style " + type + " for path " + pathString); 
    154166            } 
     167        } else if ("value".equals(qName)) { 
     168            valueBuffer = new StringBuilder(); 
    155169        } 
    156170    } 
     
    162176     * @param path the path of the constraint to create 
    163177     * @param attrs the XML attributes 
     178     * @param values the enclosed values 
    164179     * @return a PathConstraint object 
    165180     * @throws SAXException if something is wrong 
    166181     */ 
    167182    public PathConstraint processConstraint(PathQuery query, String path, 
    168             Attributes attrs) throws SAXException { 
    169         ConstraintOp constraintOp = ConstraintOp.getConstraintOp(attrs.getValue("op")); 
     183            Map<String, String> attrs, Collection<String> values) throws SAXException { 
     184        ConstraintOp constraintOp = ConstraintOp.getConstraintOp(attrs.get("op")); 
    170185        if (ConstraintOp.CONTAINS.equals(constraintOp)) { 
    171186            constraintOp = ConstraintOp.MATCHES; 
     
    175190            if (PathConstraintLoop.VALID_OPS.contains(constraintOp)) { 
    176191                try { 
    177                     Path constraintPath = query.makePath(path); 
    178                     if (!constraintPath.endIsAttribute()) { 
     192                    Path constraintPath2 = query.makePath(path); 
     193                    if (!constraintPath2.endIsAttribute()) { 
    179194                        isLoop = true; 
    180195                    } 
     
    185200            } 
    186201            if (isLoop) { 
    187                 String loopPath = attrs.getValue("loopPath"); 
     202                String loopPath = attrs.get("loopPath"); 
    188203                if (loopPath == null) { 
    189                     loopPath = attrs.getValue("value"); 
     204                    loopPath = attrs.get("value"); 
    190205                } 
    191206                loopPath = loopPath.replace(':', '.'); 
    192207                return new PathConstraintLoop(path, constraintOp, loopPath); 
    193208            } else { 
    194                 String constraintValue = attrs.getValue("value"); 
     209                String constraintValue = attrs.get("value"); 
    195210                return new PathConstraintAttribute(path, constraintOp, constraintValue); 
    196211            } 
     
    198213            return new PathConstraintNull(path, constraintOp); 
    199214        } else if (PathConstraintBag.VALID_OPS.contains(constraintOp)) { 
    200             String bag = attrs.getValue("value"); 
    201             String ids = attrs.getValue("ids"); 
     215            String bag = attrs.get("value"); 
     216            String ids = attrs.get("ids"); 
    202217            if (bag != null) { 
    203218                return new PathConstraintBag(path, constraintOp, bag); 
     
    214229                return new PathConstraintIds(path, constraintOp, idsCollection); 
    215230            } else { 
    216                 String values = attrs.getValue("values"); 
    217                 String[] valueArray = values.split(","); 
    218231                Collection<String> valuesCollection = new LinkedHashSet<String>(); 
    219                 for (String value : valueArray) { 
     232                for (String value : values) { 
    220233                    valuesCollection.add(value.trim()); 
    221234                } 
     
    223236            } 
    224237        } else if (ConstraintOp.LOOKUP.equals(constraintOp)) { 
    225             String lookup = attrs.getValue("value"); 
    226             String extraValue = attrs.getValue("extraValue"); 
     238            String lookup = attrs.get("value"); 
     239            String extraValue = attrs.get("extraValue"); 
    227240            return new PathConstraintLookup(path, lookup, extraValue); 
    228241        } else { 
    229242            throw new SAXException("Invalid operation type: " + constraintOp 
    230                     + " (from text \"" + attrs.getValue("op") + "\""); 
     243                    + " (from text \"" + attrs.get("op") + "\", attributes: " + attrs + ")"); 
    231244        } 
    232245    } 
     
    236249     */ 
    237250    @Override public void endElement(@SuppressWarnings("unused") String uri, 
    238                            @SuppressWarnings("unused") String localName, String qName) { 
     251            @SuppressWarnings("unused") String localName, String qName) throws SAXException { 
    239252        if ("query".equals(qName)) { 
    240253            if (constraintLogic != null) { 
     
    263276        } else if ("node".equals(qName)) { 
    264277            currentNodePath = null; 
     278        } else if ("constraint".equals(qName) && (constraintPath != null)) { 
     279            PathConstraint constraint = processConstraint(query, constraintPath, 
     280                    constraintAttributes, constraintValues); 
     281            if (constraintCode == null) { 
     282                query.addConstraint(constraint); 
     283            } else { 
     284                query.addConstraint(constraint, constraintCode); 
     285            } 
     286            constraintPath = null; 
     287        } else if ("value".equals(qName)) { 
     288            if (valueBuffer == null) { 
     289                throw new NullPointerException("valueBuffer is null while closing value tag"); 
     290            } 
     291            constraintValues.add(valueBuffer.toString()); 
     292        } 
     293    } 
     294 
     295    /** 
     296     * {@inheritDoc} 
     297     */ 
     298    @Override 
     299    public void characters(char[] ch, int start, int length) { 
     300        if (valueBuffer != null) { 
     301            valueBuffer.append(ch, start, length); 
    265302        } 
    266303    } 
Note: See TracChangeset for help on using the changeset viewer.