@Nullable
 private SimpleNode visit(SimpleNode simpleNode, DomElement domElement) {
   boolean validCandidate = false;
   if (simpleNode instanceof AbstractDomElementNode) {
     final DomElement nodeElement = ((AbstractDomElementNode) simpleNode).getDomElement();
     if (nodeElement != null) {
       validCandidate = !(simpleNode instanceof DomElementsGroupNode);
       if (validCandidate && nodeElement.equals(domElement)) {
         return simpleNode;
       }
       if (!(nodeElement instanceof MergedObject) && !isParent(nodeElement, domElement)) {
         return null;
       }
     }
   }
   final Object[] childElements = myBuilder.getTreeStructure().getChildElements(simpleNode);
   if (childElements.length == 0 && validCandidate) { // leaf
     return simpleNode;
   }
   for (Object child : childElements) {
     SimpleNode result = visit((SimpleNode) child, domElement);
     if (result != null) {
       return result;
     }
   }
   return validCandidate ? simpleNode : null;
 }
Example #2
0
  public static List<DomElement> getDefinedChildren(
      @NotNull final DomElement parent, final boolean tags, final boolean attributes) {
    if (parent instanceof MergedObject) {
      final SmartList<DomElement> result = new SmartList<>();
      parent.acceptChildren(
          new DomElementVisitor() {
            @Override
            public void visitDomElement(final DomElement element) {
              if (hasXml(element)) {
                result.add(element);
              }
            }
          });
      return result;
    }

    ProgressManager.checkCanceled();

    if (parent instanceof GenericAttributeValue) return Collections.emptyList();

    if (parent instanceof DomFileElement) {
      final DomFileElement element = (DomFileElement) parent;
      return tags ? Arrays.asList(element.getRootElement()) : Collections.<DomElement>emptyList();
    }

    final XmlElement xmlElement = parent.getXmlElement();
    if (xmlElement instanceof XmlTag) {
      XmlTag tag = (XmlTag) xmlElement;
      final DomManager domManager = parent.getManager();
      final SmartList<DomElement> result = new SmartList<>();
      if (attributes) {
        for (final XmlAttribute attribute : tag.getAttributes()) {
          if (!attribute.isValid()) {
            LOG.error("Invalid attr: parent.valid=" + tag.isValid());
            continue;
          }
          GenericAttributeValue element = domManager.getDomElement(attribute);
          if (checkHasXml(attribute, element)) {
            ContainerUtil.addIfNotNull(result, element);
          }
        }
      }
      if (tags) {
        for (final XmlTag subTag : tag.getSubTags()) {
          if (!subTag.isValid()) {
            LOG.error("Invalid subtag: parent.valid=" + tag.isValid());
            continue;
          }
          DomElement element = domManager.getDomElement(subTag);
          if (checkHasXml(subTag, element)) {
            ContainerUtil.addIfNotNull(result, element);
          }
        }
      }
      return result;
    }
    return Collections.emptyList();
  }
  private static boolean isParent(final DomElement potentialParent, final DomElement domElement) {
    DomElement currParent = domElement;
    while (currParent != null) {
      if (currParent.equals(potentialParent)) return true;

      currParent = currParent.getParent();
    }
    return false;
  }
Example #4
0
 @NotNull
 public static List<XmlTag> getElementTags(@NotNull Collection<? extends DomElement> list) {
   ArrayList<XmlTag> result = new ArrayList<>(list.size());
   for (DomElement element : list) {
     XmlTag tag = element.getXmlTag();
     if (tag != null) {
       result.add(tag);
     }
   }
   return result;
 }
Example #5
0
 public static void acceptAvailableChildren(
     final DomElement element, final DomElementVisitor visitor) {
   final XmlTag tag = element.getXmlTag();
   if (tag != null) {
     for (XmlTag xmlTag : tag.getSubTags()) {
       final DomElement childElement = element.getManager().getDomElement(xmlTag);
       if (childElement != null) {
         childElement.accept(visitor);
       }
     }
   }
 }
Example #6
0
 @NotNull
 public static XmlTag[] getElementTags(@NotNull DomElement[] list) {
   XmlTag[] result = new XmlTag[list.length];
   int i = 0;
   for (DomElement element : list) {
     XmlTag tag = element.getXmlTag();
     if (tag != null) {
       result[i++] = tag;
     }
   }
   return result;
 }
Example #7
0
 @Nullable
 public static <T> T getParentOfType(
     final DomElement element, final Class<T> requiredClass, final boolean strict) {
   for (DomElement curElement = strict && element != null ? element.getParent() : element;
       curElement != null;
       curElement = curElement.getParent()) {
     if (requiredClass.isInstance(curElement)) {
       return (T) curElement;
     }
   }
   return null;
 }
Example #8
0
 @NotNull
 public static String[] getElementNames(@NotNull Collection<? extends DomElement> list) {
   ArrayList<String> result = new ArrayList<>(list.size());
   if (list.size() > 0) {
     for (DomElement element : list) {
       String name = element.getGenericInfo().getElementName(element);
       if (name != null) {
         result.add(name);
       }
     }
   }
   return ArrayUtil.toStringArray(result);
 }
Example #9
0
 /** {@inheritDoc} */
 @Override
 public Album createItemFromElement(final DomElement element) {
   if (element == null) {
     return null;
   }
   final Album album = new Album(null, null, null);
   MusicEntry.loadStandardInfo(album, element);
   if (element.hasChild("artist")) {
     album.artist = element.getChild("artist").getChildText("name");
     if (album.artist == null) {
       album.artist = element.getChildText("artist");
     }
   }
   return album;
 }
Example #10
0
  public static <T extends DomElement> DomFileElement<T> getFileElement(
      @NotNull DomElement element) {

    if (element instanceof DomFileElement) {
      return (DomFileElement) element;
    }
    DomFileElement fileElement = element.getUserData(FILE_ELEMENT_KEY);
    if (fileElement == null) {
      DomElement parent = element.getParent();
      if (parent != null) {
        fileElement = getFileElement(parent);
      }
      element.putUserData(FILE_ELEMENT_KEY, fileElement);
    }
    return fileElement;
  }
 public DomFileEditor(final DomElement element, final String name, final T component) {
   this(
       element.getManager().getProject(),
       DomUtil.getFile(element).getVirtualFile(),
       name,
       component);
 }
Example #12
0
 private void renderSource(DomElement element, WAbstractMedia.Source source, boolean isLast) {
   element.setAttribute("src", resolveRelativeUrl(source.link.getUrl()));
   if (!source.type.equals("")) {
     element.setAttribute("type", source.type);
   }
   if (!source.media.equals("")) {
     element.setAttribute("media", source.media);
   }
   if (isLast && this.alternative_ != null) {
     element.setAttribute(
         "onerror",
         "var media = this.parentNode;if(media){while (media && media.children.length)if (Wt3_2_3.hasTag(media.firstChild,'SOURCE')){media.removeChild(media.firstChild);}else{media.parentNode.insertBefore(media.firstChild, media);}media.style.display= 'none';}");
   } else {
     element.setAttribute("onerror", "");
   }
 }
Example #13
0
 /**
  * Use the last.fm corrections data to check whether the supplied artist has a correction to a
  * canonical artist. This method returns a new {@link Artist} object containing the corrected
  * data, or <code>null</code> if the supplied Artist was not found.
  *
  * @param artist The artist name to correct
  * @return a new {@link Artist}, or <code>null</code>
  */
 public static final Artist getCorrection(final Context context, final String artist) {
   Result result = null;
   try {
     result = Caller.getInstance(context).call("artist.getCorrection", "<key>", "artist", artist);
     if (!result.isSuccessful()) {
       return null;
     }
     final DomElement correctionElement = result.getContentElement().getChild("correction");
     if (correctionElement == null) {
       return new Artist(artist, null);
     }
     final DomElement artistElem = correctionElement.getChild("artist");
     return FACTORY.createItemFromElement(artistElem);
   } catch (final Exception ignored) {
     return null;
   }
 }
Example #14
0
 @Nullable
 public static List<JavaMethod> getFixedPath(DomElement element) {
   assert element.isValid();
   final LinkedList<JavaMethod> methods = new LinkedList<>();
   while (true) {
     final DomElement parent = element.getParent();
     if (parent instanceof DomFileElement) {
       break;
     }
     final JavaMethod method = getGetterMethod(element, parent);
     if (method == null) {
       return null;
     }
     methods.addFirst(method);
     element = element.getParent();
   }
   return methods;
 }
Example #15
0
  @Nullable
  public static <T extends DomElement> T findDomElement(
      @Nullable final PsiElement element, final Class<T> beanClass, boolean strict) {
    if (element == null) return null;

    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, strict);
    DomElement domElement;

    while (tag != null) {
      domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);

      if (domElement != null) {
        return domElement.getParentOfType(beanClass, false);
      }
      tag = tag.getParentTag();
    }
    return null;
  }
Example #16
0
  @Nullable
  private static JavaMethod getGetterMethod(final DomElement element, final DomElement parent) {
    final String xmlElementName = element.getXmlElementName();
    final String namespace = element.getXmlElementNamespaceKey();
    final DomGenericInfo genericInfo = parent.getGenericInfo();

    if (element instanceof GenericAttributeValue) {
      final DomAttributeChildDescription description =
          genericInfo.getAttributeChildDescription(xmlElementName, namespace);
      assert description != null;
      return description.getGetterMethod();
    }

    final DomFixedChildDescription description =
        genericInfo.getFixedChildDescription(xmlElementName, namespace);
    return description != null
        ? description.getGetterMethod(description.getValues(parent).indexOf(element))
        : null;
  }
 /**
  * @param holder
  * @param element
  */
 protected static void loadImages(final ImageHolder holder, final DomElement element) {
   final Collection<DomElement> images = element.getChildren("image");
   for (final DomElement image : images) {
     final String attribute = image.getAttribute("size");
     ImageSize size = null;
     if (attribute == null) {
       size = ImageSize.LARGESQUARE;
     } else {
       try {
         size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
       } catch (final IllegalArgumentException e) {
         // if they suddenly again introduce a new image size
       }
     }
     if (size != null) {
       holder.imageUrls.put(size, image.getText());
     }
   }
 }
Example #18
0
 /**
  * @param domElement DomElement to search root of
  * @return the topmost valid DomElement being a parent of the given one. May be and may be not
  *     DomFileElement. If root tag has changed, file may lose its domness, so there will be no
  *     DomFileElement, but the inner DomElement's will be still alive because the underlying XML
  *     tags are valid
  */
 @NotNull
 public static DomElement getRoot(@NotNull DomElement domElement) {
   while (true) {
     final DomElement parent = domElement.getParent();
     if (parent == null) {
       return domElement;
     }
     domElement = parent;
   }
 }
 @Override
 @Nullable
 public Object getData(String dataId) {
   if (DATA_KEY.is(dataId)) {
     return this;
   }
   final SimpleNode simpleNode = getTree().getSelectedNode();
   if (simpleNode instanceof AbstractDomElementNode) {
     final DomElement domElement = ((AbstractDomElementNode) simpleNode).getDomElement();
     if (domElement != null && domElement.isValid()) {
       if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
         final XmlElement tag = domElement.getXmlElement();
         if (tag instanceof Navigatable) {
           return tag;
         }
       }
     }
   }
   return null;
 }
Example #20
0
 private DomElement createRow(int row, boolean withIds, WApplication app) {
   DomElement tr = DomElement.createNew(DomElementType.DomElement_TR);
   if (withIds) {
     tr.setId(this.rows_.get(row).getId());
   }
   this.rows_.get(row).updateDom(tr, true);
   tr.setWasEmpty(false);
   int spanCounter = 0;
   for (int col = 0; col < this.getColumnCount(); ++col) {
     WTableRow.TableData d = this.itemAt(row, col);
     if (!d.overSpanned) {
       DomElement td = d.cell.createSDomElement(app);
       if (col < this.headerColumnCount_ || row < this.headerRowCount_) {
         tr.addChild(td);
       } else {
         tr.insertChildAt(td, col - spanCounter);
       }
       for (int i = 0; i < d.cell.getRowSpan(); ++i) {
         for (int j = 0; j < d.cell.getColumnSpan(); ++j) {
           if (i + j > 0) {
             this.itemAt(row + i, col + j).overSpanned = true;
             this.itemAt(row + i, col + j).cell.setRendered(false);
           }
         }
       }
     } else {
       spanCounter++;
     }
   }
   return tr;
 }
Example #21
0
 void updateMediaDom(DomElement element, boolean all) {
   if (all && this.alternative_ != null) {
     element.setAttribute(
         "onerror",
         "if(event.target.error && event.target.error.code==event.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED){while (this.hasChildNodes())if (Wt3_2_3.hasTag(this.firstChild,'SOURCE')){this.removeChild(this.firstChild);}else{this.parentNode.insertBefore(this.firstChild, this);}this.style.display= 'none';}");
   }
   if (all || this.flagsChanged_) {
     if (!all || !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Controls).isEmpty()) {
       element.setAttribute(
           "controls",
           !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Controls).isEmpty()
               ? "controls"
               : "");
     }
     if (!all || !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Autoplay).isEmpty()) {
       element.setAttribute(
           "autoplay",
           !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Autoplay).isEmpty()
               ? "autoplay"
               : "");
     }
     if (!all || !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Loop).isEmpty()) {
       element.setAttribute(
           "loop",
           !EnumUtils.mask(this.flags_, WAbstractMedia.Options.Loop).isEmpty() ? "loop" : "");
     }
   }
   if (all || this.preloadChanged_) {
     switch (this.preloadMode_) {
       case PreloadNone:
         element.setAttribute("preload", "none");
         break;
       default:
       case PreloadAuto:
         element.setAttribute("preload", "auto");
         break;
       case PreloadMetadata:
         element.setAttribute("preload", "metadata");
         break;
     }
   }
   this.updateEventSignals(element, all);
   if (all) {
     if (this.alternative_ != null) {
       element.addChild(this.alternative_.createSDomElement(WApplication.getInstance()));
     }
   }
   this.flagsChanged_ = this.preloadChanged_ = false;
 }
Example #22
0
 public static <T> List<T> getChildrenOfType(
     @NotNull final DomElement parent, final Class<T> type) {
   final List<T> result = new SmartList<>();
   parent.acceptChildren(
       new DomElementVisitor() {
         @Override
         public void visitDomElement(final DomElement element) {
           if (type.isInstance(element)) {
             result.add((T) element);
           }
         }
       });
   return result;
 }
Example #23
0
  @NotNull
  public static <T extends DomElement> T getOriginalElement(@NotNull final T domElement) {
    final XmlElement psiElement = domElement.getXmlElement();
    if (psiElement == null) return domElement;

    final PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
    final TextRange range = psiElement.getTextRange();
    final PsiElement element = psiFile.findElementAt(range.getStartOffset());
    final int maxLength = range.getLength();
    final boolean isAttribute = psiElement instanceof XmlAttribute;
    final Class<? extends XmlElement> clazz = isAttribute ? XmlAttribute.class : XmlTag.class;
    final DomManager domManager = domElement.getManager();
    DomElement current = null;
    for (XmlElement next = PsiTreeUtil.getParentOfType(element, clazz, false);
        next != null && next.getTextLength() <= maxLength;
        next = PsiTreeUtil.getParentOfType(next, clazz, true)) {
      current =
          isAttribute
              ? domManager.getDomElement((XmlAttribute) next)
              : domManager.getDomElement((XmlTag) next);
      if (current != null && domElement.getClass() != current.getClass()) current = null;
    }
    return (T) current;
  }
Example #24
0
 void getDomChanges(List<DomElement> result, WApplication app) {
   DomElement e = DomElement.getForUpdate(this, this.getDomElementType());
   if (!this.isStubbed() && this.flags_.get(BIT_GRID_CHANGED)) {
     DomElement newE = this.createDomElement(app);
     e.replaceWith(newE);
   } else {
     if (this.rowsChanged_ != null) {
       for (Iterator<WTableRow> i_it = this.rowsChanged_.iterator(); i_it.hasNext(); ) {
         WTableRow i = i_it.next();
         DomElement e2 = DomElement.getForUpdate(i, DomElementType.DomElement_TR);
         i.updateDom(e2, false);
         result.add(e2);
       }
       ;
       this.rowsChanged_ = null;
     }
     if (this.rowsAdded_ != 0) {
       DomElement etb =
           DomElement.getForUpdate(this.getId() + "tb", DomElementType.DomElement_TBODY);
       for (int i = 0; i < (int) this.rowsAdded_; ++i) {
         DomElement tr = this.createRow(this.getRowCount() - this.rowsAdded_ + i, true, app);
         etb.addChild(tr);
       }
       result.add(etb);
       this.rowsAdded_ = 0;
     }
     if (this.flags_.get(BIT_COLUMNS_CHANGED)) {
       for (int i = 0; i < this.columns_.size(); ++i) {
         DomElement e2 =
             DomElement.getForUpdate(this.columns_.get(i), DomElementType.DomElement_COL);
         this.columns_.get(i).updateDom(e2, false);
         result.add(e2);
       }
       this.flags_.clear(BIT_COLUMNS_CHANGED);
     }
     this.updateDom(e, false);
   }
   result.add(e);
 }
Example #25
0
 @SuppressWarnings("ForLoopReplaceableByForEach")
 public static <T extends DomElement> List<T> getChildrenOf(
     DomElement parent, final Class<T> type) {
   final List<T> list = new SmartList<>();
   List<? extends AbstractDomChildrenDescription> descriptions =
       parent.getGenericInfo().getChildrenDescriptions();
   for (int i = 0, descriptionsSize = descriptions.size(); i < descriptionsSize; i++) {
     AbstractDomChildrenDescription description = descriptions.get(i);
     if (description.getType() instanceof Class
         && type.isAssignableFrom((Class<?>) description.getType())) {
       List<T> values = (List<T>) description.getValues(parent);
       for (int j = 0, valuesSize = values.size(); j < valuesSize; j++) {
         T value = values.get(j);
         if (value.exists()) {
           list.add(value);
         }
       }
     }
   }
   return list;
 }
  public static BasicDomElementComponent createComponentWithCaption(
      final CommittablePanel committablePanel,
      final CaptionComponent captionComponent,
      final DomElement element) {
    final JPanel panel = new JPanel(new BorderLayout());
    panel.add(captionComponent, BorderLayout.NORTH);
    panel.add(
        element.isValid() ? committablePanel.getComponent() : new JPanel(), BorderLayout.CENTER);

    BasicDomElementComponent component =
        new BasicDomElementComponent(element) {
          @Override
          public JComponent getComponent() {
            return panel;
          }
        };

    component.addComponent(committablePanel);
    component.addComponent(captionComponent);
    return component;
  }
Example #27
0
 void getDomChanges(List<DomElement> result, WApplication app) {
   if (this.mediaId_.length() != 0) {
     DomElement media = DomElement.getForUpdate(this.mediaId_, DomElementType.DomElement_DIV);
     this.updateMediaDom(media, false);
     if (this.sourcesChanged_) {
       for (int i = 0; i < this.sourcesRendered_; ++i) {
         media.callJavaScript(
             "Wt3_2_3.remove('" + this.mediaId_ + "s" + String.valueOf(i) + "');", true);
       }
       this.sourcesRendered_ = 0;
       for (int i = 0; i < this.sources_.size(); ++i) {
         DomElement src = DomElement.createNew(DomElementType.DomElement_SOURCE);
         src.setId(this.mediaId_ + "s" + String.valueOf(i));
         this.renderSource(src, this.sources_.get(i), i + 1 >= this.sources_.size());
         media.addChild(src);
       }
       this.sourcesRendered_ = this.sources_.size();
       this.sourcesChanged_ = false;
       media.callJavaScript(this.getJsMediaRef() + ".load();");
     }
     result.add(media);
   }
   super.getDomChanges(result, app);
 }
  @Nullable
  public static XmlTag findTag(@NotNull DomElement domElement, @NotNull String path) {
    List<String> elements = StringUtil.split(path, ".");
    if (elements.isEmpty()) return null;

    Pair<String, Integer> nameAndIndex = translateTagName(elements.get(0));
    String name = nameAndIndex.first;
    Integer index = nameAndIndex.second;

    XmlTag result = domElement.getXmlTag();
    if (result == null || !name.equals(result.getName())) return null;
    result = getIndexedTag(result, index);

    for (String each : elements.subList(1, elements.size())) {
      nameAndIndex = translateTagName(each);
      name = nameAndIndex.first;
      index = nameAndIndex.second;

      result = result.findFirstSubTag(name);
      if (result == null) return null;
      result = getIndexedTag(result, index);
    }
    return result;
  }
Example #29
0
  public static List<? extends DomElement> getIdentitySiblings(DomElement element) {
    final GenericDomValue nameDomElement = element.getGenericInfo().getNameDomElement(element);
    if (nameDomElement == null) return Collections.emptyList();

    final NameValue nameValue = nameDomElement.getAnnotation(NameValue.class);
    if (nameValue == null || !nameValue.unique()) return Collections.emptyList();

    final String stringValue = ElementPresentationManager.getElementName(element);
    if (stringValue == null) return Collections.emptyList();

    final DomElement scope = element.getManager().getIdentityScope(element);
    if (scope == null) return Collections.emptyList();

    final DomGenericInfo domGenericInfo = scope.getGenericInfo();
    final String tagName = element.getXmlElementName();
    final DomCollectionChildDescription childDescription =
        domGenericInfo.getCollectionChildDescription(tagName, element.getXmlElementNamespaceKey());
    if (childDescription != null) {
      final ArrayList<DomElement> list = new ArrayList<>(childDescription.getValues(scope));
      list.remove(element);
      return list;
    }
    return Collections.emptyList();
  }
 @Nullable
 public static MavenProject findContainingProject(@NotNull DomElement element) {
   PsiElement psi = element.getXmlElement();
   return psi == null ? null : findContainingProject(psi);
 }