Package dk.brics.xmlgraph

Examples of dk.brics.xmlgraph.XMLGraph


    g.sharpen();
    return g;
  }
 
  public XMLGraph transferRemove(final RemoveStm s, final XMLGraph base) {
    final XMLGraph g = base.clone();
    if (g.isUnknown()) {
      return g;
    }
    StatusMap smt;
    smt = evaluateXPathOrRoot(s.getXPath(), base); //evaluator.evaluate(base, s.getXPath(), dummy_root, dummy_root_content);
    final StatusMap sm = smt;
    checkXPathEmpty(s, base, sm);
    g.processRoots(new ReachableNodesProcessor(g) { 
      @Override
      public Object process(ChoiceNode n) { // all concrete nodes have a choice node parent
        Collection<Integer> cs = new LinkedHashSet<Integer>();
        boolean modified = false;
        boolean add_emptyseq = false;
        for (int i : n.getContents()) {
          boolean keep = true;
          if (base.getNode(i) instanceof ConcreteNode) {
            switch (sm.get(i)) {
            case ALL:
            case DEFINITE:
              add_emptyseq = true;
              if (g.getGapTypeMap().isEmpty()) {
                  keep = false;
              }
              modified = true;
              break;
            case SOME:
            case DONTKNOW:
              add_emptyseq = true;
              modified = true;
              break;
            case NEVER:
            case NONE:
              break;
            }
          }
          if (keep)
            cs.add(i);
        }
        if (add_emptyseq)
          cs.add(stm_nodes.getRemoveSequenceNode(s,g).getIndex());
        if (modified)
          n.setContent(cs, g);
        return null;
      }
    });
    g.sharpen();
    return g;
  }
View Full Code Here


    g.sharpen();
    return g;
  }

  public XMLGraph transferSet(SetStm s, XMLGraph base, final XMLGraph xmlsrc) {
    final XMLGraph g = base.clone();
    if (g.isUnknown() || xmlsrc.isUnknown()) {
      g.setUnknown();
      return g;
    }

    final StatusMap stm = evaluateXPathOrRoot(s.getXPath(), g);

    boolean empty = checkXPathEmpty(s, g, stm);
    if (empty)
      return g;

    g.merge(xmlsrc);
    g.getRoots().retainAll(base.getRoots());

    final TextNode text = stm_nodes.getSetTextNode(s, g);
    text.replaceText(s.getStringSource(), g);

    // TODO avoid recursive replacement for nodes that will not actually be replaced
    // because its ancestor got replaced instead.
    switch (s.getKind()) {
    case SET:
      // set(XPath, Object)
      g.processReachableNodes(new NodeProcessor<Object>() {
        @Override
        public Object process(ChoiceNode n) {
          LinkedHashSet<Integer> cs = new LinkedHashSet<Integer>(n.getContents());
          for (int child : n.getContents()) {
            Node node = g.getNode(child);
            if (node instanceof ConcreteNode) {
              switch (stm.get(child)) {
              case ALL:
              case DEFINITE:
                  if (g.getGapTypeMap().isEmpty()) {
                      cs.remove(child);
                  }
                cs.addAll(xmlsrc.getRoots());
                cs.add(text.getIndex());
                break;
              case SOME:
              case DONTKNOW:
                cs.addAll(xmlsrc.getRoots());
                cs.add(text.getIndex());
                break;
              case NONE:
              case NEVER:
                break;
              }
            }
          }
          n.setContent(cs, g);
          return null;
        }
      });
      break;

    case SETCONTENT:
      g.processReachableNodes(new NodeProcessor<Object>() {
        @Override
        public Object process(ElementNode n) {
          ChoiceNode ch = (ChoiceNode)g.getNode(n.getContent());
          LinkedHashSet<Integer> c = new LinkedHashSet<Integer>(ch.getContents());
          switch (stm.get(n.getIndex())) {
          case ALL:
          case DEFINITE:
              if (g.getGapTypeMap().isEmpty()) {
                  c.clear();
              }
            c.addAll(xmlsrc.getRoots());
            c.add(text.getIndex());
            break;

          case SOME:
          case DONTKNOW:
            c.addAll(xmlsrc.getRoots());
            c.add(text.getIndex());
            break;

          case NONE:
          case NEVER:
            break;
          }
          ch.setContent(c, g);
          return null;
        }
      });
      break;

    default:
      throw new RuntimeException("unknown set kind");
    }

    g.sharpen();

    return g;
  }
View Full Code Here

    return g;
  }

  public XMLGraph transferUnknown(UnknownStm s) {
    XMLGraph g = global_xg.clone();
    g.setUnknown(); // TODO: use an ordinary 'any' schema instead?
    return g;
  }
View Full Code Here

    return g;
  }

  public XMLGraph transferValidate(final ValidateStm s, XMLGraph base) {
    final SchemaType schema = s.getSchema();
    final XMLGraph g = global_xg.clone();
    g.useFragment(new XMLGraphFragment(schema.getTypeNode(), schema.getTemplateGaps(), schema.getAttributeGaps(), schema.getGapTypes()));
    g.sharpen();
    return g;
  }
View Full Code Here

  /**
   * Checks that the given template is valid according to the given schema type.
   * @throws XMLValidationException if the template is invalid
   */
  public static void validate(XML x, String type, Origin origin) throws XMLValidationException {
    XMLGraph xg = XMLGraphConverter.convert(x, XMLGraphConverter.GapConversion.IGNORE, false); // TODO: make variant of validate that treats gaps optimistically instead of just ignoring them
    //new dk.brics.xmlgraph.converter.XMLGraph2Dot(System.out).print(xg);
    XMLGraph t = null;
    int i = 0;
    String expanded = expandQName(type, origin);
    for (ConvertedSchema s : schemas) {
      SequenceNode n = s.types.get(expanded);
      if (n != null) {
View Full Code Here

  }
 
  private XMLGraph getGapAnnotatedGraph(final XMLGraph graph, final SchemaType schema, final XMLGraphBuilder b, final Origin origin) {
    //if (gaptypes == null || gaptypes.size() == 0)
    //  return graph;
    final XMLGraph g = graph.clone();
    g.processReachableNodes(new NodeProcessor<Object>() {
      @Override
      public Object process(ChoiceNode n) {
        if (!n.isGap() || !n.isOpen())
          return n;
        //String type = gaptypes == null ? null : gaptypes.get(n.getName());
        Node typeNode = schema.getGapTypeNodes().get(n.getName());
        Set<Integer> cs = new LinkedHashSet<Integer>(n.getContents());
        if (typeNode == null) {
          // close the gap if no annotation is present
          if (g.getOpenTemplateGaps().remove(n.getName())) {
            cs.add(b.getEmptySequence().getIndex());
            g.getClosedTemplateGaps().add(n.getName());
            n.setContentAndStatus(false, n.isRemoved(), cs, g);
          }
        } else {
          cs.add(typeNode.getIndex());
          if (g.getOpenAttributeGaps().remove(n.getName())) {
            g.getClosedAttributeGaps().add(n.getName());
          }
          if (g.getOpenTemplateGaps().remove(n.getName())) {
            g.getClosedTemplateGaps().add(n.getName());
          }
         
          XMLGraph sub = b.getGlobalXMLGraph().clone();
          sub.useFragment(new XMLGraphFragment(typeNode, null, null, null));
          g.merge(sub);
         
          n.setContentAndStatus(false, n.isRemoved(), cs, g);
         
          // get the original roots back
View Full Code Here

        }

        @Override
        public void visitGetStm(GetStm s) {
          printProgress(s);
          final XMLGraph g = b.getOut(s, s.getDest());
          boolean empty;
          if (g.isUnknown()) {
            empty = false;
          } else {
            empty = true;
            for (int i : g.getRoots()) {
                // TODO use Emptiness instead, or upgrade sharpen() so we can just test
                // if roots are empty
              // true=definitely empty, false=maybe empty
              empty &= g.getNode(i).process(new CachedNodeProcessor<Boolean>() {
                @Override
                public Boolean cycle() {
                  return false;
                }
                @Override
                public Boolean process(AttributeNode n) {
                  return false;
                }
                @Override
                public Boolean process(ElementNode n) {
                  return false;
                }
                @Override
                public Boolean process(TextNode n) {
                  if (n.getText().isEmpty())
                    return true;
                  else
                    return false;
                }
                @Override
                public Boolean process(OneOrMoreNode n) {
                  return g.getNode(n.getContent()).process(this);
                }
                @Override
                public Boolean process(ChoiceNode n) {
                  if (n.isGap() && n.isOpen())
                    return false; // gaps are not to be considered "empty content"
                  else
                    return null;
                }
                @Override
                public Boolean process(MultiContentNode n) {
                  boolean empty = true;
                  for (int child : n.getContents()) {
                    empty &= g.getNode(child).process(this);
                  }
                  return empty;
                }
              });
            }
          }
          if (empty)
            error(s, ErrorType.EMPTY_RESULT, "'%s' has empty result", s.getOpName());
        }

        @Override
        public void visitInsertStm(InsertStm s) {
          printProgress(s);
          checkNonEmpty(s);
        }

        @Override
        public void visitNodeStm(NodeStm s) {
          printProgress(s);
          // TODO: check NodeStm     
        }

        @Override
        public void visitPlugStm(final PlugStm s) {
          printProgress(s);
          XMLGraph g = b.getIn(s, s.getBase());
          if (g.isUnknown())
              return;
          switch (s.getKind()) {
          case PLUG:
          case PLUGMULTI:
          case PLUGWRAP: {
              if (!g.getOpenAttributeGaps().contains(s.getGapName())
                && !g.getOpenTemplateGaps().contains(s.getGapName()))
                error(s, ErrorType.MISSING_GAP, "the gap '%s' is absent", s.getGapName());
              if (g.getOpenAttributeGaps().contains(s.getGapName())
                  && !b.getIn(s, s.getXMLSource()).getRoots().isEmpty())
                error(s, ErrorType.XML_IN_ATTRIBUTE, "maybe plugging XML data into attribute gap '%s'", s.getGapName());
              final String gapType = g.getGapTypeMap().get(s.getGapName());
              if (gapType != null) {
                  XMLGraph value_xg = b.getIn(s, s.getXMLSource());
                  XMLGraph type_xg = b.getGlobalXMLGraph().clone();
//                  SequenceNode n = b.getSchemaTypes().get(gapType);
                  Node n = fg.getTypemap().get(gapType);
                  type_xg.useFragment(new XMLGraphFragment(n, null, null, null));
                  Validator validator = new Validator(new ValidationErrorHandler() {
                                public boolean error(ElementNode n, Origin origin, String msg,
                                        String example, Origin schema) {
                                    if (example == null)
                                         example = "";
                                    XMLGraphChecker.this.error(
                                            s,
                                            ErrorType.INVALID_PLUG_TYPE,
                                            "Plug statement violates the gap type %s\n" +
                                            "because of %s created at %s\n" +
                                            "%s %s",
                                            gapType,
                                            formatNode(n),
                                            origin,
                                            msg,
                                            formatExample(example));
                                    return true;
                                }
                            });
                  validator.validate(value_xg, type_xg, -1);
              }
              break;
          }
             
          case CLOSE:
              if (g.getGapTypeMap().isEmpty())
                  return;
              XMLGraph empty_xg = b.getGlobalXMLGraph().clone();
              empty_xg.useFragment(new XMLGraphFragment(b.getEmptySequence(), null, null, null));
                        XMLGraph type_xg = b.getGlobalXMLGraph().clone();
              for (final Map.Entry<String,String> gapentry : g.getGapTypeMap().entrySet()) {
                  final String gapName = gapentry.getKey();
                  final String gapType = gapentry.getValue();
                          Node n = fg.getTypemap().get(gapType);
                          type_xg.useFragment(new XMLGraphFragment(n, null, null, null));
                  Validator validator = new Validator(new ValidationErrorHandler() {
                                public boolean error(ElementNode n, Origin origin, String msg,
                                        String example, Origin schema) {
                                    if (example == null)
                                         example = "";
                                    XMLGraphChecker.this.error(
                                            s,
                                            ErrorType.INVALID_PLUG_TYPE,
                                            "Close statement violates the type of gap %s\n" +
                                            "%s does not permit an empty sequence\n" +
                                            "Perhaps add '?' or '*' quantifier?",
                                            gapName,
                                            gapType);
                                    return true;
                                }
                            });
                            validator.validate(empty_xg, type_xg, -1);
              }
              break;
          }
        }
       
        @Override
        public void visitRemoveStm(RemoveStm s) {
          printProgress(s);
          checkNonEmpty(s);
        }

        @Override
        public void visitSetStm(SetStm s) {
          printProgress(s);
          checkNonEmpty(s);
        }
      });
    }
    for (Statement s : fg.getNodes()) {
      s.visitBy(new BasicStatementVisitor() {

        @Override
        public void visitAnalyzeStm(final AnalyzeStm s) {
          if (s.getKind() == AnalyzeStm.Kind.HOTSPOT)
            return; // hotspots should not be analyzed here
          printProgress(s);
          XMLGraph value_xg = b.getIn(s, s.getBase());
          if (value_xg.isUnknown()) {
              String message;
              if (s.getKind() == AnalyzeStm.Kind.TYPEANNOTATION) {
                  message = "Non-validated XML data assigned to type-annotated variable";
              } else {
                  message = "Non-validated XML data at 'analyze' statement";
              }
            error(s, ErrorType.INVALID, message);
          } else {
            Validator validator = new Validator(new ValidationErrorHandler() {
              public boolean error(ElementNode n, Origin origin, String msg,
                  String example, Origin schema) {
                XMLGraphChecker.this.error(
                        s.getOrigin(),
                        ErrorType.INVALID,
                        "Problem in %s created at %s\n" +
                        "%s %s",
                                        formatNode(n),
                                        origin,
                        msg,
                        formatExample(example));
                return true;
              }
            });
            XMLGraph type_xg = b.getGlobalXMLGraph().clone();
            SchemaType schema = s.getSchema();
            String type = schema.getType();
            Node n = fg.getTypemap().get(type);
            if (n == null)
              throw new XMLAnalysisException("No schema definition found for " + type, s.getOrigin());
            type_xg.useFragment(new XMLGraphFragment(n, null, null, null));
            Map<String,String> gaptypes = schema.getGapTypes();
            if (gaptypes == null)
                gaptypes = Collections.emptyMap();
            for (Map.Entry<String,String> valueGapType : value_xg.getGapTypeMap().entrySet()) {
                String typeGapType = gaptypes.get(valueGapType.getKey());
View Full Code Here

    VariableAnalysisElement<XMLGraph> vae = da.getFlowOut(s);
    if (vae == null) {
      Debug.println(10, true, "getFlowOut(" + s + ")==null");
      return global_xg;
    }
    XMLGraph g = vae.get(v);
    if (g == null) {
      Debug.println(10, true, "getFlowOut(" + s + ")(" + v + ")==null");
      return global_xg;
    }
    return g;
View Full Code Here

    //throw new RuntimeException();
//    XMLGraphDebug.dumpXMLGraph(g, s);
  }

  public XMLGraph transferCast(final CastStm s, final XMLGraph base) {
    final XMLGraph g = base.clone();
    if (g.isUnknown()) {
      return g;
    }
    final Emptiness emptiness;
    switch (s.getKind()) {
    case ASATTRIBUTE:
        emptiness = new Emptiness(g, null, EPresence.BOTTOM, EPresence.BOTTOM, EPresence.BOTTOM);
        break;
    case ASELEMENT:
        emptiness = new Emptiness(g, EPresence.BOTTOM, null, EPresence.BOTTOM, EPresence.BOTTOM);
        break;
    case ASTEXT:
        emptiness = new Emptiness(g, EPresence.BOTTOM, EPresence.BOTTOM, null, EPresence.BOTTOM);
        break;
    case ASCOMMENT:
    case ASPI:
    case ASNSDECL:
    default:
        return g;
    }
    final FirstRootAnalysis first = new FirstRootAnalysis(g, emptiness, s.getKind() == CastStm.Kind.ASTEXT);
   
    g.processReachableNodes(new NodeProcessor<Object>() {
        @Override
        public Object process(ChoiceNode n) {
            if (!first.get(n.getIndex()).definitely())
                return this;
            LinkedList<Integer> remove = new LinkedList<Integer>();
            for (int child : n.getContents()) {
                if (first.get(child).definitelyNot()) {
                    remove.add(child);
                }
            }
            if (!remove.isEmpty()) {
                LinkedHashSet<Integer> cs = new LinkedHashSet<Integer>(n.getContents());
                cs.removeAll(remove);
                n.setContent(cs, g);
            }
            return this;
        }
    });
    LinkedList<Integer> remove = new LinkedList<Integer>();
    for (int root : g.getRoots()) {
        if (first.get(root).definitelyNot()) {
            remove.add(root);
        }
    }
    g.getRoots().removeAll(remove);
   
    return g;
  }
View Full Code Here

      break;
    }
  }
 
  public XMLGraph transferConcat(ConcatStm s, XMLGraph xmlsource) {
    XMLGraph g = xmlsource.clone();
    if (g.isUnknown()) {
      return g;
    }
    ChoiceNode cn = stm_nodes.getConcatChoiceNode(s,g);
    Set<Integer> cs = new LinkedHashSet<Integer>();
    //cs.addAll(cn.getContents());
    stm_nodes.getConcatTextNode(s, g).replaceText(s.getStringSource(), g);
    if (!s.getStringSource().isEmpty()) {
      cs.add(stm_nodes.getConcatTextNode(s, g).getIndex());
    }
    cs.add(stm_nodes.getConcatSequenceNode(s, g).getIndex());
    cs.addAll(g.getRoots());
    cn.setContent(cs, g);
    g.getRoots().clear();
    g.getRoots().add(stm_nodes.getConcatOneOrMoreNode(s, g).getIndex());
    //stm_nodes.getConcatTextNode(s,g).replaceText(s.getStringSource(), g);
    g.sharpen();
    return g;
  }
View Full Code Here

TOP

Related Classes of dk.brics.xmlgraph.XMLGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.