Пример #1
0
  public String formatAsXHTML(String xhtml) throws IOException, JDOMException {
    DocType doctype =
        new DocType(
            "html", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
    Namespace namespace = Namespace.getNamespace("http://www.w3.org/1999/xhtml");

    org.jdom2.Document document = XHTMLUtils.parseXHTMLDocument(xhtml);

    org.jdom2.Element root = document.getRootElement();
    root.setNamespace(namespace);
    root.addNamespaceDeclaration(namespace);
    IteratorIterable<org.jdom2.Element> elements = root.getDescendants(Filters.element());
    for (org.jdom2.Element element : elements) {
      if (element.getNamespace() == null) {
        element.setNamespace(Constants.NAMESPACE_XHTML);
      }
    }
    document.setDocType(doctype);

    XMLOutputter outputter = new XMLOutputter();
    Format xmlFormat = Format.getPrettyFormat();
    outputter.setFormat(xmlFormat);
    outputter.setXMLOutputProcessor(new XHTMLOutputProcessor());
    String result = outputter.outputString(document);
    return result;
  }
Пример #2
0
  public void scrollTo(Deque<ElementPosition> nodeChain) {
    if (currentEditor.getValue().getMediaType().equals(MediaType.XHTML)) {
      XHTMLCodeEditor xhtmlCodeEditor = (XHTMLCodeEditor) currentEditor.getValue();
      String code = xhtmlCodeEditor.getCode();
      /*            int index = code.indexOf(html);
      logger.info("index of clicked html " + index + " html: " + html);
      xhtmlCodeEditor.scrollTo(index);*/
      LocatedJDOMFactory factory = new LocatedJDOMFactory();
      try {
        org.jdom2.Document document = XHTMLUtils.parseXHTMLDocument(code, factory);
        org.jdom2.Element currentElement = document.getRootElement();
        ElementPosition currentElementPosition = nodeChain.pop();
        while (currentElementPosition != null) {
          IteratorIterable<org.jdom2.Element> children;
          if (StringUtils.isNotEmpty(currentElementPosition.getNamespaceUri())) {
            List<Namespace> namespaces = currentElement.getNamespacesInScope();
            Namespace currentNamespace = null;
            for (Namespace namespace : namespaces) {
              if (namespace.getURI().equals(currentElementPosition.getNamespaceUri())) {
                currentNamespace = namespace;
                break;
              }
            }
            Filter<org.jdom2.Element> filter =
                Filters.element(currentElementPosition.getNodeName(), currentNamespace);
            children = currentElement.getDescendants(filter);
          } else {
            Filter<org.jdom2.Element> filter =
                Filters.element(currentElementPosition.getNodeName());
            children = currentElement.getDescendants(filter);
          }

          int currentNumber = 0;
          for (org.jdom2.Element child : children) {
            if (currentNumber == currentElementPosition.getPosition()) {
              currentElement = child;
              break;
            }
            currentNumber++;
          }

          try {
            currentElementPosition = nodeChain.pop();
          } catch (NoSuchElementException e) {
            logger.info("no more element in node chain");
            currentElementPosition = null;
          }
        }

        LocatedElement locatedElement = (LocatedElement) currentElement;
        EditorPosition pos =
            new EditorPosition(locatedElement.getLine() - 1, locatedElement.getColumn());
        logger.info("pos for scrolling to is " + pos.toJson());
        xhtmlCodeEditor.scrollTo(pos);
      } catch (IOException | JDOMException e) {
        logger.error("", e);
      }
    }
  }
Пример #3
0
 public String repairXHTML(String xhtml) throws IOException, JDOMException {
   return XHTMLUtils.repair(xhtml);
 }
Пример #4
0
  public boolean splitXHTMLFile() {
    boolean result = false;
    if (currentEditor.getValue().getMediaType().equals(MediaType.XHTML)) {
      XHTMLCodeEditor xhtmlCodeEditor = (XHTMLCodeEditor) currentEditor.getValue();
      EditorPosition pos = xhtmlCodeEditor.getEditorCursorPosition();
      XMLTagPair pair =
          xhtmlCodeEditor.findSurroundingTags(
              token -> {
                String type = token.getType();
                if ("tag".equals(type)) {
                  String content = token.getContent();
                  if ("head".equals(content) || "body".equals(content) || "html".equals(content)) {
                    return true;
                  }
                }
                return false;
              });
      if (pair == null
          || "head".equals(pair.getTagName())
          || "html".equals(pair.getTagName())
          || StringUtils.isEmpty(pair.getTagName())) {
        Dialogs.create()
            .owner(tabPane)
            .title("Teilung nicht möglich")
            .message(
                "Kann Datei nicht an dieser Position teilen. Eine Teilung ist nur innerhalb des XHTML-Bodys möglich.")
            .showWarning();
        return false;
      }
      logger.debug("umgebendes pair " + pair);
      // wir sind innerhalb des Body
      int index = xhtmlCodeEditor.getIndexFromPosition(pos);
      try {
        String originalCode = xhtmlCodeEditor.getCode();
        org.jdom2.Document originalDocument = XHTMLUtils.parseXHTMLDocument(originalCode);
        List<Content> originalHeadContent = getOriginalHeadContent(originalDocument);

        byte[] frontPart = originalCode.substring(0, index).getBytes("UTF-8");
        Resource oldResource = currentXHTMLResource.getValue();
        oldResource.setData(frontPart);
        HtmlCleanerBookProcessor processor = new HtmlCleanerBookProcessor();
        processor.processResource(oldResource);
        xhtmlCodeEditor.setCode(new String(oldResource.getData(), "UTF-8"));

        byte[] backPart =
            originalCode.substring(index, originalCode.length() - 1).getBytes("UTF-8");
        String fileName = book.getNextStandardFileName(MediaType.XHTML);
        Resource resource = MediaType.XHTML.getResourceFactory().createResource("Text/" + fileName);
        byte[] backPartXHTML = XHTMLUtils.repairWithHead(backPart, originalHeadContent);
        resource.setData(backPartXHTML);

        int spineIndex = book.getSpine().getResourceIndex(oldResource);
        book.addSpineResource(resource, spineIndex + 1);
        openFileInEditor(resource, MediaType.XHTML);

        bookBrowserManager.refreshBookBrowser();
        needsRefresh.setValue(true);
        needsRefresh.setValue(false);
      } catch (IOException | JDOMException | ResourceDataException e) {
        logger.error("", e);
        Dialogs.create()
            .owner(tabPane)
            .title("Teilung nicht möglich")
            .message("Kann Datei nicht teilen. Bitte Fehlermeldung an den Hersteller übermitteln.")
            .showException(e);
      }

      result = true;
    }
    return result;
  }