1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components/declaration/common/declaration_creator_manager.h"
17
18 #include "core/components/declaration/badge/badge_declaration.h"
19 #include "core/components/declaration/button/button_declaration.h"
20 #include "core/components/declaration/canvas/canvas_declaration.h"
21 #include "core/components/declaration/clock/clock_declaration.h"
22 #include "core/components/declaration/image/image_animator_declaration.h"
23 #include "core/components/declaration/piece/piece_declaration.h"
24 #include "core/components/declaration/qrcode/qrcode_declaration.h"
25 #include "core/components/declaration/richtext/rich_text_declaration.h"
26 #include "core/components/declaration/span/span_declaration.h"
27 #include "core/components/declaration/svg/svg_animate_declaration.h"
28 #include "core/components/declaration/svg/svg_circle_declaration.h"
29 #include "core/components/declaration/svg/svg_ellipse_declaration.h"
30 #include "core/components/declaration/svg/svg_fe_colormatrix_declaration.h"
31 #include "core/components/declaration/svg/svg_fe_composite_declaration.h"
32 #include "core/components/declaration/svg/svg_fe_gaussianblur_declaration.h"
33 #include "core/components/declaration/svg/svg_fe_offset_declaration.h"
34 #include "core/components/declaration/svg/svg_filter_declaration.h"
35 #include "core/components/declaration/svg/svg_gradient_declaration.h"
36 #include "core/components/declaration/svg/svg_line_declaration.h"
37 #include "core/components/declaration/svg/svg_mask_declaration.h"
38 #include "core/components/declaration/svg/svg_path_declaration.h"
39 #include "core/components/declaration/svg/svg_polygon_declaration.h"
40 #include "core/components/declaration/svg/svg_rect_declaration.h"
41 #include "core/components/declaration/svg/svg_stop_declaration.h"
42 #include "core/components/declaration/svg/svg_text_declaration.h"
43 #include "core/components/declaration/svg/svg_text_path_declaration.h"
44 #include "core/components/declaration/swiper/swiper_declaration.h"
45 #include "core/components/declaration/text/text_declaration.h"
46 #include "core/components/declaration/web/web_declaration.h"
47 #include "core/components/declaration/xcomponent/xcomponent_declaration.h"
48
49 namespace OHOS::Ace {
50
51 DeclarationCreatorManager::DeclarationCreatorManager() = default;
52 DeclarationCreatorManager::~DeclarationCreatorManager() = default;
53
54 template<class T>
DeclarationCreator(const std::string & tag)55 RefPtr<Declaration> DeclarationCreator(const std::string& tag)
56 {
57 return AceType::MakeRefPtr<T>();
58 }
59
CreateDeclaration(const std::string & tag)60 const RefPtr<Declaration> DeclarationCreatorManager::CreateDeclaration(const std::string& tag)
61 {
62 static const LinearMapNode<std::function<RefPtr<Declaration>(const std::string&)>> declarationCreators[] = {
63 { DOM_NODE_TAG_ANIMATE, DeclarationCreator<SvgAnimateDeclaration> },
64 { DOM_NODE_TAG_ANIMATE_MOTION, DeclarationCreator<SvgAnimateDeclaration> },
65 { DOM_NODE_TAG_ANIMATE_TRANSFORM, DeclarationCreator<SvgAnimateDeclaration> },
66 { DOM_NODE_TAG_BADGE, DeclarationCreator<BadgeDeclaration> },
67 { DOM_NODE_TAG_BUTTON, DeclarationCreator<ButtonDeclaration> },
68 { DOM_NODE_TAG_CANVAS, DeclarationCreator<CanvasDeclaration> },
69 { DOM_NODE_TAG_CIRCLE, DeclarationCreator<SvgCircleDeclaration> },
70 { DOM_NODE_TAG_CLOCK, DeclarationCreator<ClockDeclaration> },
71 { DOM_NODE_TAG_ELLIPSE, DeclarationCreator<SvgEllipseDeclaration> },
72 { DOM_NODE_TAG_FE_COLORMATRIX, DeclarationCreator<SvgFeColorMatrixDeclaration> },
73 { DOM_NODE_TAG_FE_COMPOSITE, DeclarationCreator<SvgFeCompositeDeclaration> },
74 { DOM_NODE_TAG_FE_GAUSSIANBLUR, DeclarationCreator<SvgFeGaussianBlurDeclaration> },
75 { DOM_NODE_TAG_FE_OFFSET, DeclarationCreator<SvgFeOffsetDeclaration> },
76 { DOM_NODE_TAG_FILTER, DeclarationCreator<SvgFilterDeclaration> },
77 { DOM_NODE_TAG_G, DeclarationCreator<SvgDeclaration> },
78 { DOM_NODE_TAG_LINEAR_GRADIENT, DeclarationCreator<SvgGradientDeclaration> },
79 { DOM_NODE_TAG_IMAGE_ANIMATOR, DeclarationCreator<ImageAnimatorDeclaration> },
80 { DOM_NODE_TAG_LABEL, DeclarationCreator<TextDeclaration> },
81 { DOM_NODE_TAG_LINE, DeclarationCreator<SvgLineDeclaration> },
82 { DOM_NODE_TAG_MASK, DeclarationCreator<SvgMaskDeclaration> },
83 { DOM_NODE_TAG_PATH, DeclarationCreator<SvgPathDeclaration> },
84 { DOM_NODE_TAG_PIECE, DeclarationCreator<PieceDeclaration> },
85 { DOM_NODE_TAG_POLYGON, DeclarationCreator<SvgPolygonDeclaration> },
86 { DOM_NODE_TAG_POLYLINE, DeclarationCreator<SvgPolygonDeclaration> },
87 { DOM_NODE_TAG_QRCODE, DeclarationCreator<QrcodeDeclaration> },
88 { DOM_NODE_TAG_RADIAL_GRADIENT, DeclarationCreator<SvgGradientDeclaration> },
89 { DOM_NODE_TAG_RECT, DeclarationCreator<SvgRectDeclaration> },
90 { DOM_NODE_TAG_RICH_TEXT, DeclarationCreator<RichTextDeclaration> },
91 { DOM_NODE_TAG_SPAN, DeclarationCreator<SpanDeclaration> },
92 { DOM_NODE_TAG_STOP, DeclarationCreator<SvgStopDeclaration> },
93 { DOM_NODE_TAG_SVG, DeclarationCreator<SvgDeclaration> },
94 { DOM_NODE_TAG_SVG_TEXT, DeclarationCreator<SvgTextDeclaration> },
95 { DOM_NODE_TAG_SWIPER, DeclarationCreator<SwiperDeclaration> },
96 { DOM_NODE_TAG_TEXT, DeclarationCreator<TextDeclaration> },
97 { DOM_NODE_TAG_TEXT_PATH, DeclarationCreator<SvgTextPathDeclaration> },
98 { DOM_NODE_TAG_TSPAN, DeclarationCreator<SvgTextDeclaration> },
99 { DOM_NODE_TAG_USE, DeclarationCreator<SvgDeclaration> },
100 { DOM_NODE_TAG_WEB, DeclarationCreator<WebDeclaration> },
101 { DOM_NODE_TAG_XCOMPONENT, DeclarationCreator<XComponentDeclaration> },
102 };
103 int64_t creatorIndex = BinarySearchFindIndex(declarationCreators, ArraySize(declarationCreators), tag.c_str());
104 RefPtr<Declaration> declaration = AceType::MakeRefPtr<Declaration>();
105 if (creatorIndex >= 0) {
106 declaration = declarationCreators[creatorIndex].value(tag);
107 }
108 declaration->Init();
109 return declaration;
110 }
111
112 } // namespace OHOS::Ace
113