1 /*
2 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xml.internal.serializer;
22
23 import java.io.IOException;
24 import java.util.Properties;
25
26 import javax.xml.transform.Result;
27
28 import org.xml.sax.Attributes;
29 import org.xml.sax.SAXException;
30
31 import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
32 import com.sun.org.apache.xml.internal.serializer.utils.Utils;
33
34 /**
35 * This serializer takes a series of SAX or
36 * SAX-like events and writes its output
37 * to the given stream.
38 *
39 * This class is not a public API, it is public
40 * because it is used from another package.
41 *
42 * @xsl.usage internal
43 * @LastModified: Sept 2018
44 */
45 public final class ToHTMLStream extends ToStream
46 {
47
48 /** This flag is set while receiving events from the DTD */
49 protected boolean m_inDTD = false;
50
51 /** True if the previous element is a block element. */
52 private boolean m_isprevblock = false;
53
54 /**
55 * Map that tells which XML characters should have special treatment, and it
56 * provides character to entity name lookup.
57 */
58 private static final CharInfo m_htmlcharInfo =
59 // new CharInfo(CharInfo.HTML_ENTITIES_RESOURCE);
60 CharInfo.getCharInfoInternal(CharInfo.HTML_ENTITIES_RESOURCE, Method.HTML);
61
62 /** A digital search trie for fast, case insensitive lookup of ElemDesc objects. */
63 static final Trie m_elementFlags = new Trie();
702 {
703 throw new SAXException(e);
704 }
705 }
706 }
707
708 m_needToOutputDocTypeDecl = false;
709 }
710
711 /**
712 * Receive notification of the end of a document.
713 *
714 * @throws org.xml.sax.SAXException Any SAX exception, possibly
715 * wrapping another exception.
716 *
717 * @throws org.xml.sax.SAXException
718 */
719 public final void endDocument() throws org.xml.sax.SAXException
720 {
721 if (m_doIndent) {
722 flushCharactersBuffer();
723 }
724 flushPending();
725 if (m_doIndent && !m_isprevtext)
726 {
727 try
728 {
729 outputLineSep();
730 }
731 catch(IOException e)
732 {
733 throw new SAXException(e);
734 }
735 }
736
737 flushWriter();
738 if (m_tracer != null)
739 super.fireEndDoc();
740 }
741
742 /**
765 * @param localName
766 * @param name
767 * The element type name.
768 * @param atts
769 * The attributes attached to the element, if any.
770 * @throws org.xml.sax.SAXException
771 * Any SAX exception, possibly wrapping another exception.
772 * @see #endElement
773 * @see org.xml.sax.AttributeList
774 */
775 public void startElement(
776 String namespaceURI,
777 String localName,
778 String name,
779 Attributes atts)
780 throws SAXException
781 {
782 if (m_doIndent) {
783 // will add extra one if having namespace but no matter
784 m_childNodeNum++;
785 flushCharactersBuffer();
786 }
787 ElemContext elemContext = m_elemContext;
788
789 // clean up any pending things first
790 if (elemContext.m_startTagOpen)
791 {
792 closeStartTag();
793 elemContext.m_startTagOpen = false;
794 }
795 else if (m_cdataTagOpen)
796 {
797 closeCDATA();
798 m_cdataTagOpen = false;
799 }
800 else if (m_needToCallStartDocument)
801 {
802 startDocumentInternal();
803 m_needToCallStartDocument = false;
804 }
805
906 }
907 }
908
909 /**
910 * Receive notification of the end of an element.
911 *
912 *
913 * @param namespaceURI
914 * @param localName
915 * @param name The element type name
916 * @throws org.xml.sax.SAXException Any SAX exception, possibly
917 * wrapping another exception.
918 */
919 public final void endElement(
920 final String namespaceURI,
921 final String localName,
922 final String name)
923 throws org.xml.sax.SAXException
924 {
925 if (m_doIndent) {
926 flushCharactersBuffer();
927 }
928 // deal with any pending issues
929 if (m_cdataTagOpen)
930 closeCDATA();
931
932 // if the element has a namespace, treat it like XML, not HTML
933 if (null != namespaceURI && namespaceURI.length() > 0)
934 {
935 super.endElement(namespaceURI, localName, name);
936
937 return;
938 }
939
940 try
941 {
942
943 ElemContext elemContext = m_elemContext;
944 final ElemDesc elemDesc = elemContext.m_elementDesc;
945 final int elemFlags = elemDesc.getFlags();
946 final boolean elemEmpty = (elemFlags & ElemDesc.EMPTY) != 0;
1628 super.cdata(ch, start, length);
1629 }
1630 }
1631
1632 /**
1633 * Receive notification of a processing instruction.
1634 *
1635 * @param target The processing instruction target.
1636 * @param data The processing instruction data, or null if
1637 * none was supplied.
1638 * @throws org.xml.sax.SAXException Any SAX exception, possibly
1639 * wrapping another exception.
1640 *
1641 * @throws org.xml.sax.SAXException
1642 */
1643 public void processingInstruction(String target, String data)
1644 throws org.xml.sax.SAXException
1645 {
1646 if (m_doIndent) {
1647 m_childNodeNum++;
1648 flushCharactersBuffer();
1649 }
1650 // Process any pending starDocument and startElement first.
1651 flushPending();
1652
1653 // Use a fairly nasty hack to tell if the next node is supposed to be
1654 // unescaped text.
1655 if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING))
1656 {
1657 startNonEscaping();
1658 }
1659 else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING))
1660 {
1661 endNonEscaping();
1662 }
1663 else
1664 {
1665 try
1666 {
1667 if (m_elemContext.m_startTagOpen)
1668 {
|
1 /*
2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xml.internal.serializer;
22
23 import java.io.IOException;
24 import java.util.Properties;
25
26 import javax.xml.transform.Result;
27
28 import org.xml.sax.Attributes;
29 import org.xml.sax.SAXException;
30
31 import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
32 import com.sun.org.apache.xml.internal.serializer.utils.Utils;
33
34 /**
35 * This serializer takes a series of SAX or
36 * SAX-like events and writes its output
37 * to the given stream.
38 *
39 * This class is not a public API, it is public
40 * because it is used from another package.
41 *
42 * @xsl.usage internal
43 * @LastModified: July 2019
44 */
45 public final class ToHTMLStream extends ToStream
46 {
47
48 /** This flag is set while receiving events from the DTD */
49 protected boolean m_inDTD = false;
50
51 /** True if the previous element is a block element. */
52 private boolean m_isprevblock = false;
53
54 /**
55 * Map that tells which XML characters should have special treatment, and it
56 * provides character to entity name lookup.
57 */
58 private static final CharInfo m_htmlcharInfo =
59 // new CharInfo(CharInfo.HTML_ENTITIES_RESOURCE);
60 CharInfo.getCharInfoInternal(CharInfo.HTML_ENTITIES_RESOURCE, Method.HTML);
61
62 /** A digital search trie for fast, case insensitive lookup of ElemDesc objects. */
63 static final Trie m_elementFlags = new Trie();
702 {
703 throw new SAXException(e);
704 }
705 }
706 }
707
708 m_needToOutputDocTypeDecl = false;
709 }
710
711 /**
712 * Receive notification of the end of a document.
713 *
714 * @throws org.xml.sax.SAXException Any SAX exception, possibly
715 * wrapping another exception.
716 *
717 * @throws org.xml.sax.SAXException
718 */
719 public final void endDocument() throws org.xml.sax.SAXException
720 {
721 if (m_doIndent) {
722 flushCharactersBuffer(false);
723 }
724 flushPending();
725 if (m_doIndent && !m_isprevtext)
726 {
727 try
728 {
729 outputLineSep();
730 }
731 catch(IOException e)
732 {
733 throw new SAXException(e);
734 }
735 }
736
737 flushWriter();
738 if (m_tracer != null)
739 super.fireEndDoc();
740 }
741
742 /**
765 * @param localName
766 * @param name
767 * The element type name.
768 * @param atts
769 * The attributes attached to the element, if any.
770 * @throws org.xml.sax.SAXException
771 * Any SAX exception, possibly wrapping another exception.
772 * @see #endElement
773 * @see org.xml.sax.AttributeList
774 */
775 public void startElement(
776 String namespaceURI,
777 String localName,
778 String name,
779 Attributes atts)
780 throws SAXException
781 {
782 if (m_doIndent) {
783 // will add extra one if having namespace but no matter
784 m_childNodeNum++;
785 flushCharactersBuffer(false);
786 }
787 ElemContext elemContext = m_elemContext;
788
789 // clean up any pending things first
790 if (elemContext.m_startTagOpen)
791 {
792 closeStartTag();
793 elemContext.m_startTagOpen = false;
794 }
795 else if (m_cdataTagOpen)
796 {
797 closeCDATA();
798 m_cdataTagOpen = false;
799 }
800 else if (m_needToCallStartDocument)
801 {
802 startDocumentInternal();
803 m_needToCallStartDocument = false;
804 }
805
906 }
907 }
908
909 /**
910 * Receive notification of the end of an element.
911 *
912 *
913 * @param namespaceURI
914 * @param localName
915 * @param name The element type name
916 * @throws org.xml.sax.SAXException Any SAX exception, possibly
917 * wrapping another exception.
918 */
919 public final void endElement(
920 final String namespaceURI,
921 final String localName,
922 final String name)
923 throws org.xml.sax.SAXException
924 {
925 if (m_doIndent) {
926 flushCharactersBuffer(false);
927 }
928 // deal with any pending issues
929 if (m_cdataTagOpen)
930 closeCDATA();
931
932 // if the element has a namespace, treat it like XML, not HTML
933 if (null != namespaceURI && namespaceURI.length() > 0)
934 {
935 super.endElement(namespaceURI, localName, name);
936
937 return;
938 }
939
940 try
941 {
942
943 ElemContext elemContext = m_elemContext;
944 final ElemDesc elemDesc = elemContext.m_elementDesc;
945 final int elemFlags = elemDesc.getFlags();
946 final boolean elemEmpty = (elemFlags & ElemDesc.EMPTY) != 0;
1628 super.cdata(ch, start, length);
1629 }
1630 }
1631
1632 /**
1633 * Receive notification of a processing instruction.
1634 *
1635 * @param target The processing instruction target.
1636 * @param data The processing instruction data, or null if
1637 * none was supplied.
1638 * @throws org.xml.sax.SAXException Any SAX exception, possibly
1639 * wrapping another exception.
1640 *
1641 * @throws org.xml.sax.SAXException
1642 */
1643 public void processingInstruction(String target, String data)
1644 throws org.xml.sax.SAXException
1645 {
1646 if (m_doIndent) {
1647 m_childNodeNum++;
1648 flushCharactersBuffer(false);
1649 }
1650 // Process any pending starDocument and startElement first.
1651 flushPending();
1652
1653 // Use a fairly nasty hack to tell if the next node is supposed to be
1654 // unescaped text.
1655 if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING))
1656 {
1657 startNonEscaping();
1658 }
1659 else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING))
1660 {
1661 endNonEscaping();
1662 }
1663 else
1664 {
1665 try
1666 {
1667 if (m_elemContext.m_startTagOpen)
1668 {
|